我有两个具有不同数量元素的数组列表(X和Y)。现在,我想将它们自己导出到excel表中。 X和Y应该并排拥有自己的列。
示例:X = [2,6,0],Y = [5,8,3]
X | Y
------
2 | 5
6 | 8
0 | 3
每次运行程序时,Arrays都会获得新的元素。在我的程序结束时,这些必须保存在excel表中,并且每次都保存在新表中。当我打开我的pogramm五次时,我应该有五张纸(我的纸张名称是实际的时间和日期 - 它保存在一个名为date的变量中)。
我的问题: 1.如何将ArrayLists放入自己的列中? 2.我应该如何创建新工作表,不要覆盖旧工作表?
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(date); //everytime, the sheet will be overwritten, but i need a new sheed. something like workbook.addSheet(date) would be perfect
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("1", new Object[] {"column 1", "column 2"});
//maybe some kind of for-loop for my ArrayLists? Dont know
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof Date)
cell.setCellValue((Date)obj);
else if(obj instanceof Boolean)
cell.setCellValue((Boolean)obj);
else if(obj instanceof String)
cell.setCellValue((String)obj);
else if(obj instanceof Double)
cell.setCellValue((Double)obj);
}
}
try {
FileOutputStream out =
new FileOutputStream(new File(".//src//excel-table.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
}