2
我正在使用此代碼使用Apache POI重寫xlsm文件。我必須從結果集重寫Sheet1上的數據,此代碼創建模板xlsm文件的副本並執行所有處理。如何使用Apache POI重寫.xlsm文件?
但是當我打開創建XLSM文件就說明我這個消息:
我們發現一個問題,在「FileName.xlsm」一些內容。你想讓我們儘可能多地恢復嗎?如果您信任此工作簿的來源,請單擊是。
這是我的代碼,請建議我應該做什麼。
public void dbConnect(String driver_connect_string, String db_connect_string, String db_userid, String db_password){
try{
Class.forName(driver_connect_string);
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
Properties propq = new Properties();
FileInputStream fisq = new FileInputStream("query.properties");
propq.load(fisq);
String queryString = propq.getProperty("myQueryString");
ResultSet rs = statement.executeQuery(queryString);
Properties propf2 = new Properties();
FileInputStream fisf2 = new FileInputStream("file.properties");
propf2.load(fisf2);
OPCPackage pkg = OPCPackage.open(new File(propf2.getProperty("sourceFile")));
XSSFWorkbook wb_template;
wb_template = new XSSFWorkbook(pkg);
System.out.println("package loaded");
SXSSFWorkbook wb = new SXSSFWorkbook(wb_template);
wb.setCompressTempFiles(true);
Sheet sheet = wb.getSheetAt(0);
Row rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("EmpId");
rowhead.createCell((short) 1).setCellValue("EmaName");
rowhead.createCell((short) 2).setCellValue("Department");
rowhead.createCell((short) 3).setCellValue("Job Title");
rowhead.createCell((short) 4).setCellValue("DOB");
int index = 1;
while (rs.next()) {
Row row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(rs.getString(1));
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getString(4));
row.createCell((short) 4).setCellValue(rs.getString(5));
index++;
}
FileOutputStream out = new FileOutputStream(new File(propf2.getProperty("destFile")));
System.out.println("XLSM created Successfully");
wb.write(out);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}