[英]Java/Apache POI - Formula aren't setting on cell correctly
我有一個Java代碼,在其中創建了一個新的.xlsm文件,該文件創建了另一個空.xlsm文件的格式。 我的問題是,當在此新創建的.xlsm的特定單元格中設置公式時。 當我打開文件時,單元格顯示:#NAME?。 但是,當我在單元格中按Enter鍵時,公式將正確顯示。 當我下車時,公式就起作用了。
為什么會這樣?
Java代碼如下:
public static String main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/database" ,
"user" ,
"password"
);
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery("select * from `table`");
FileInputStream file = new FileInputStream(new File("C:\\Users\\Desktop\\Folder1.xlsm"));
XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(file));
XSSFSheet spreadsheet = wb.getSheet("Planilha1");
XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell;
int i = 1;
while(resultSet.next()) {
row = spreadsheet.createRow(i);
cell = row.createCell(1);
cell.setCellValue("");
cell = row.createCell(2);
cell.setCellValue(resultSet.getString("column1"));
i++;
}
XSSFRow linhacontador = spreadsheet.getRow(1);
if (linhacontador == null) {
linhacontador = spreadsheet.createRow(1);
}
XSSFCell colunacontador = linhacontador.getCell(34);
if (colunacontador == null) {
colunacontador = linhacontador.createCell(34);
}
colunacontador.setCellType(XSSFCell.CELL_TYPE_FORMULA);
colunacontador.setCellFormula("CONT.SE(AH:AH, \"<>\")");
CellRangeAddress range = new CellRangeAddress(1, i-1, 25, 25);
spreadsheet.addMergedRegion(range);
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Desktop\\exceldatabase.xlsm"));
wb.write(out);
out.close();
connect.close();
return "Worksheet Done!";
}
顯然我的問題在這里:
colunacontador.setCellType(XSSFCell.CELL_TYPE_FORMULA);
colunacontador.setCellFormula("CONT.SE(AH:AH, \"<>\")");
在這種情況下,應以英語書寫。 就像CONTSE一樣:COUNTIF。 SOMA將是SUM ..等等。 只需搜索英語版本的公式即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.