我正在从以下代码创建报告,并使用quartz scheduler对其进行计划,但是当保存时,它会损坏。
public class GenerateReport implements Job{
XSSFWorkbook report = new XSSFWorkbook();
MyData myData = new MyData();
public XSSFWorkbook createReport() {
Map<Integer, String> data = new HashMap<Integer, String>();
myData.setData(1652, "Abcs");
myData.setData(1682, "ksaos");
myData.setData(1152, "oass");
myData.setData(1962, "Aajas");
data = myData.getData();
XSSFSheet sheet = report.createSheet("Employee data");
XSSFCellStyle headerStyle = report.createCellStyle();
XSSFFont headerFont = report.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
XSSFCell cell = null;
XSSFRow row;
int rowNum = 0;
int colNum = 0;
row = sheet.createRow(rowNum++);
row.createCell(colNum++).setCellValue("ID");
row.createCell(colNum).setCellValue("Name");
for(Map.Entry<Integer, String> entry: data.entrySet()) {
colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum++);
cell.setCellValue(entry.getKey());
cell = row.createCell(colNum);
cell.setCellValue(entry.getValue());
}
return report;
}
public void saveReport() {
XSSFWorkbook finalWorkbook = new XSSFWorkbook();
try {
FileOutputStream fos = new FileOutputStream("C:\\Users\\ra185586\\Desktop\\written.xlsx");
finalWorkbook.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Report generated");
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
createReport();
saveReport();
System.out.println(" and saved");
}
}
你们能帮助我解决问题,以便在保存报告时不会损坏报告。
谢谢!
最佳答案
您的方法createReport()返回XSSFWorkbook。并且您需要将此XSSFWorkbook转移到saveReport。
-在execute
的saveReport(createReport());
两行中更改
-在saveReport
XSSFWorkbook finalWorkbook = new XSSFWorkbook();
中删除行
-在saveReport
saveReport(XSSFWorkbook finalWorkbook)
中添加参数