Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java将数据写入excel时出现的问题_Java_Excel_Apache Poi - Fatal编程技术网

用java将数据写入excel时出现的问题

用java将数据写入excel时出现的问题,java,excel,apache-poi,Java,Excel,Apache Poi,我试图从SQL表中提取数据并将其写入Excel工作表 我目前正在创建一个HashMap-HashMap tblDataMap=newhashmap(),并将表中的所有数据写入HashMap 当我尝试将相同的内容写入excel时,只写入最后一列,其余列为空 下面是我要迭代的代码 public String writeDataToExcel(String fileLoc, String tblName) { try{ LisaDBUtil lisaDb

我试图从SQL表中提取数据并将其写入Excel工作表

我目前正在创建一个HashMap-
HashMap tblDataMap=newhashmap()
,并将表中的所有数据写入HashMap

当我尝试将相同的内容写入excel时,只写入最后一列,其余列为空

下面是我要迭代的代码

public String writeDataToExcel(String fileLoc, String tblName)
    {
        try{
            LisaDBUtil lisaDb = new LisaDBUtil();
            HashMap<Integer, ArrayList<String>> tblDataMap = lisaDb.getTableDetails(tblName);
            System.out.println("Map : " +tblDataMap.toString());
            XSSFWorkbook workbook = new XSSFWorkbook(); 
            XSSFSheet sheet = workbook.createSheet(tblName);

            Integer rowCount = 0;
            Integer key = 1;

            short col = 0;
            ArrayList<String> tmpArr = null;

            while(tblDataMap.containsKey(key))
            {

                tmpArr = tblDataMap.get(key);
                System.out.println("tmpArr : " +tmpArr.toString());
                for (String val : tmpArr)
                {
                    XSSFRow row = sheet.createRow(rowCount);
                    System.out.println("Cell value : " +val +"Col : " +col);
                    XSSFCell cell = row.createCell(col);
                    cell.setCellValue(val);
                    System.out.println("Cel Value set : " +cell.getColumnIndex() +cell.getRowIndex());
                    col++;
                    sheet.autoSizeColumn(col);
                }

                col = 0;
                rowCount++;
                key++;

                System.out.println("row value : " +rowCount);
            }
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //get current date time with Date()
            Date date = new Date();
            System.out.println(dateFormat.format(date));
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File(fileLoc+"/"+tblName+"_"+dateFormat.format(date)+".xlsx"));
            workbook.write(out);
            out.close();
            FILE_CREATION_STATUS = "success";
            System.out.println("Excel written successfully on disk.");
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return FILE_CREATION_STATUS;
    }

正确迭代单元格并正确设置值。但一旦生成excel,就只有一列中有值。有人请帮忙。

似乎只是个愚蠢的错误
XSSFRow row=sheet.createRow(rowCount)应该正好在for循环之前。一旦创建了行,就不需要为同一行的单元格一次又一次地创建。每次创建单元格时,只需将同一行的先前数据替换为新创建的空行。这就是为什么只出现最后一列

    while(tblDataMap.containsKey(key))
        {

            tmpArr = tblDataMap.get(key);
            System.out.println("tmpArr : " +tmpArr.toString());
            // for all the column of same row, you just need to create row only once
            XSSFRow row = sheet.createRow(rowCount);
            for (String val : tmpArr)
            {
                System.out.println("Cell value : " +val +"Col : " +col);
                XSSFCell cell = row.createCell(col);
                cell.setCellValue(val);
                System.out.println("Cel Value set : " +cell.getColumnIndex() +cell.getRowIndex());
                col++;
                sheet.autoSizeColumn(col);
            }

            col = 0;
            rowCount++;
            key++;

            System.out.println("row value : " +rowCount);
        }

您还应该将
工作表.autoSizeColumn
调用拉到末尾-这是一个昂贵的调用,您不希望在处理的最后一行执行它
    while(tblDataMap.containsKey(key))
        {

            tmpArr = tblDataMap.get(key);
            System.out.println("tmpArr : " +tmpArr.toString());
            // for all the column of same row, you just need to create row only once
            XSSFRow row = sheet.createRow(rowCount);
            for (String val : tmpArr)
            {
                System.out.println("Cell value : " +val +"Col : " +col);
                XSSFCell cell = row.createCell(col);
                cell.setCellValue(val);
                System.out.println("Cel Value set : " +cell.getColumnIndex() +cell.getRowIndex());
                col++;
                sheet.autoSizeColumn(col);
            }

            col = 0;
            rowCount++;
            key++;

            System.out.println("row value : " +rowCount);
        }