SQL Table에서 데이터를 가져 와서 Excel 시트에 쓰려고합니다.Java에서 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이 생성되면 한 열에 만 값이 있습니다. 누군가 제발 도와주세요.
또한 'sheet.autoSizeColumn' 호출을 끝까지 가져와야합니다. 값 비싼 호출이기 때문에 모든 행을 처리의 맨 마지막 단계에서 처리하고 싶지는 않습니다 – Gagravarr