java - java中将数据写入excel的问题

标签 java excel apache-poi

我正在尝试从 SQL 表中提取数据并将其写入 Excel 工作表。

我目前正在创建一个 HashMap - HashMap<Integer, ArrayList<String>> tblDataMap = new HashMap<Integer, ArrayList<String>>() 并将所有数据从表写入 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;
    }

我可以看到单元格值设置正确。请在控制台日志下方找到。

Map : {1=[tdm_id, Account_No, Customer_Name, IACode, Supp_Account], 2=[1001, 9384938493, Sindhu, YW, 34545655667], 3=[1002, 9486958958, Ussanar, LLB, 656565576], 4=[1003, 8568632432, XYZ, YB, 45654654654]}
tmpArr : [tdm_id, Account_No, Customer_Name, IACode, Supp_Account]
Cell value : tdm_idCol : 0
Cel Value set : 00
Cell value : Account_NoCol : 1
Cel Value set : 10
Cell value : Customer_NameCol : 2
Cel Value set : 20
Cell value : IACodeCol : 3
Cel Value set : 30
Cell value : Supp_AccountCol : 4
Cel Value set : 40
row value : 1
tmpArr : [1001, 9384938493, Sindhu, YW, 34545655667]
Cell value : 1001Col : 0
Cel Value set : 01
Cell value : 9384938493Col : 1
Cel Value set : 11
Cell value : SindhuCol : 2
Cel Value set : 21
Cell value : YWCol : 3
Cel Value set : 31
Cell value : 34545655667Col : 4
Cel Value set : 41
row value : 2
tmpArr : [1002, 9486958958, Ussanar, LLB, 656565576]
Cell value : 1002Col : 0
Cel Value set : 02
Cell value : 9486958958Col : 1
Cel Value set : 12
Cell value : UssanarCol : 2
Cel Value set : 22
Cell value : LLBCol : 3
Cel Value set : 32
Cell value : 656565576Col : 4
Cel Value set : 42
row value : 3
tmpArr : [1003, 8568632432, XYZ, YB, 45654654654]
Cell value : 1003Col : 0
Cel Value set : 03
Cell value : 8568632432Col : 1
Cel Value set : 13
Cell value : XYZCol : 2
Cel Value set : 23
Cell value : YBCol : 3
Cel Value set : 33
Cell value : 45654654654Col : 4
Cel Value set : 43
row value : 4
2013-11-28
Excel written successfully on disk.
Status : success

单元格已正确迭代并且值已正确设置。但是一旦生成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);
        }

关于java - java中将数据写入excel的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20275312/

相关文章:

excel - 我需要一个与 Excel 中的 NETWORKDAYS 相反或相反的函数

C# 使用 Microsoft.Office.Interop.Excel 读取数据

sql-server - 如何将 SQL 查询导出到 Excel 文件尚不存在的 Excel?

java - 如果 Excel 文件打开,则 Java 程序无法访问 Excel 文件

java - 应用程序中的可修改列表使用什么数据结构?

java - 使用流的流

java - 将 Java 哈希码组合成 "master"哈希码

java - Intellij 源代码中的这个注解是什么意思?

java - 如何将excel数据读入 vector 表

java - POI 单元格不会变色