我正在寻找使用 apache poi 在 excel 表最后显示总计行的代码。
为了简化问题,我实现了示例代码,在 Excel 工作表中生成一个包含一些虚拟数据的表格。
代码正在运行,但是当我打开生成的 Excel 工作簿时,我收到如下所示的警告消息。
代码有问题吗?
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
public class CreateTableExample {
public static void main(String[] args) {
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
// Create headers
Row headerRow = sheet.createRow(0);
for (int i = 0; i < 3; i++) {
Cell headerCell = headerRow.createCell(i);
headerCell.setCellValue("Header " + (i + 1));
}
// Create sample data
for (int i = 1; i <= 10; i++) {
Row dataRow = sheet.createRow(i);
for (int j = 0; j < 3; j++) {
Cell dataCell = dataRow.createCell(j);
dataCell.setCellValue( (j + 1));
}
}
Row totalRow = sheet.createRow(11);
totalRow.createCell(0).setCellValue("Total");;
// Define the data range for the table
AreaReference areaReference = new AreaReference("A1:C12", SpreadsheetVersion.EXCEL2007);
// Create the table
XSSFTable table = ((XSSFSheet) sheet).createTable(areaReference);
String tableName = "MyTable";
table.setName(tableName);
CTTable ctTable = table.getCTTable();
ctTable.setDisplayName(tableName);
ctTable.setId(1);
ctTable.setTotalsRowShown(true);
ctTable.setTotalsRowCount(1);
// Set the table style
table.getCTTable().addNewTableStyleInfo();
table.getCTTable().getTableStyleInfo().setName("TableStyleMedium9");
String format = String.format("SUBTOTAL(109,%s[%s])",tableName, "Header 2");
totalRow.createCell(1).setCellFormula(String.format("SUBTOTAL(109,%s[%s])",tableName, "Header 2"));
totalRow.createCell(2).setCellFormula(String.format("SUBTOTAL(109,%s[%s])",tableName, "Header 3"));
// Save the workbook
try (FileOutputStream fileOut = new FileOutputStream("workbook_with_table.xlsx")) {
workbook.write(fileOut);
}
Desktop.getDesktop().open(new File("workbook_with_table.xlsx"));
System.out.println("Excel file with table created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
欢迎来到 Microsoft Office Open XML 的勇敢世界。对于桌子来说,两者总是需要的。一旦设置在表 XML 中,然后设置在相应的工作表中。
就总计行而言,即第一个表格列的总计行标签和工作表中相应的字符串单元格值以及表格列的总计行函数和工作表中的相应公式。
...
// add totals row to table
Row totalsRow = sheet.createRow(11);
// set totals row label for table column 0
table.getCTTable().getTableColumns().getTableColumnList().get(0).setTotalsRowLabel("Totals");
// set sheet cell value for this
totalsRow.createCell(0).setCellValue("Totals");;
table.getCTTable().setTotalsRowShown(true);
table.getCTTable().setTotalsRowCount(1);
// set totals row function for table column
table.getCTTable().getTableColumns().getTableColumnList().get(1).setTotalsRowFunction(
org.openxmlformats.schemas.spreadsheetml.x2006.main.STTotalsRowFunction.SUM);
// set sheet cell formula for this
totalsRow.createCell(1).setCellFormula(String.format("SUBTOTAL(109,%s[%s])",tableName, "Header 2"));
// set totals row function for table column
table.getCTTable().getTableColumns().getTableColumnList().get(2).setTotalsRowFunction(
org.openxmlformats.schemas.spreadsheetml.x2006.main.STTotalsRowFunction.SUM);
// set sheet cell formula for this
totalsRow.createCell(2).setCellFormula(String.format("SUBTOTAL(109,%s[%s])",tableName, "Header 3"));
...