我需要按顺序将数据写入Excel文件。
我写过这个函数:
private void writeXlFile()
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Map<String, List<String>> empData = new HashMap<String, List<String>>();
List<String> name = new ArrayList<String>();
name.add("Employee ID");
name.add("Employee Name");
name.add("Salary");
empData.put("1", name);
name = new ArrayList<String>();
name.add("01");
name.add("bala");
name.add("100000");
empData.put("2", name);
name = new ArrayList<String>();
name.add("02");
name.add("Ganesh");
name.add("100000");
empData.put("3", name);
name = new ArrayList<String>();
name.add("03");
name.add("Krish");
name.add("100000");
empData.put("4", name);
Set<String> keyset = empData.keySet();
int rownum = 0;
for (String key : keyset)
{
Row row = sheet.createRow(rownum++);
List<String> nameList = empData.get(key);
int cellnum = 0;
for (Object obj : nameList)
{
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
{
String path = separator + "home" + separator + "santhanam" + separator
+ "Desktop" + separator + "write.xls";
FileOutputStream out =
new FileOutputStream(new File(path));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException :" + e);
}
catch (IOException e)
{
System.out.println("IOException :" + e);
}
}
它正在运作,但结果显示如下:
02 Ganesh 100000
01 bala 100000
Employee ID Employee Name Salary
03 Kumar 100000
我想将员工ID和姓名写为工作表的第一行。
我需要做什么?
答案 0 :(得分:0)
请使用Integer而不是String。
Map<Integer, List<String>> empData = new HashMap<Integer , List<String>>();
等等。它会解决你的问题。
由于 比拼