我有一个表格数据。其中包含日期列。我正在做的是,我将选定的日期传递给servlet。使用那个日期我从表中得到了一些记录。然后我使用Apache POI创建一个名为Microsoft Excel文件的工作簿,并将该数据存储到excel文件中。
这是我的代码:
String reports_date = request.getParameter("reports_date");
//CreateJobOrderBean joborderbean = new CreateJobOrderBean();
BSUtils bsutils = new BSUtils();
//Received Date format: 09-Dec-2015(dd-MMM-yyyy)
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String formatteddate = null;
try {
date = formatter.parse(reports_date);
formatteddate = formatter1.format(date);
date = formatter1.parse(formatteddate);
} catch (ParseException e) {
e.printStackTrace();
}
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("JobOrders Data");
XSSFRow row=spreadsheet.createRow(1);
XSSFCell cell;
cell=row.createCell(1);
cell.setCellValue("owner_name");
cell=row.createCell(2);
cell.setCellValue("owner_tel1");
cell=row.createCell(3);
cell.setCellValue("location");
cell=row.createCell(4);
cell.setCellValue("postcode");
cell=row.createCell(5);
cell.setCellValue("nature_of_case");
cell=row.createCell(6);
cell.setCellValue("cause");
cell=row.createCell(7);
cell.setCellValue("assign_to_mktg");
cell=row.createCell(8);
cell.setCellValue("assign_to_qs");
cell=row.createCell(9);
cell.setCellValue("job_created_on");
int i=2;
List<CreateJobOrderBean> reports_data = bsutils.getReportsDate(date);
System.out.println(reports_data.size());
for (int j = 0; j < reports_data.size(); j++) {
row=spreadsheet.createRow(i);
cell=row.createCell(1);
cell.setCellValue(reports_data.get(j).getOwnername());
cell=row.createCell(2);
cell.setCellValue(reports_data.get(j).getTel1());
cell=row.createCell(3);
cell.setCellValue(reports_data.get(j).getLocation());
cell=row.createCell(4);
cell.setCellValue(reports_data.get(j).getPostcode());
cell=row.createCell(5);
cell.setCellValue(reports_data.get(j).getNatureofcase());
cell=row.createCell(6);
cell.setCellValue(reports_data.get(j).getCause());
cell=row.createCell(7);
cell.setCellValue(reports_data.get(j).getAssignedmarketingemail());
cell=row.createCell(8);
cell.setCellValue(reports_data.get(j).getAssignedQs());
cell=row.createCell(9);
cell.setCellValue(reports_data.get(j).getJobCreatesOnasString());
i++;
}
FileOutputStream out = new FileOutputStream(new File("D:/joborders_database.xlsx"));
workbook.write(out);
out.close();
此代码成功运行,但xlsx文件未生成。我不知道我在哪里犯了错误。