APACHE POI创建损坏的文件 - 无论我做了什么更改



我正在尝试创建带有某些值重合的表格的Excel文件。 问题是无论我做什么,Java都会创建损坏的Excel文件(.xlsx,.xlsm)。

更改现有表列的名称 - 文件已损坏,如果不修复它就无法打开它。名称已更改,但由于某种原因必须修复文件。 创建新表或为现有表创建新列似乎会损坏文件,但我不知道为什么。

这是我的整个代码:

package pl.TiHerbatka.Ti.ExcelWriteReadTi.Main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFTableColumn;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main {
public static void main(String[] args) throws InvalidFormatException, IOException {
File deleteOldFile = new File("E:\Users\PLACEHOLDER\ExcelTestowy#2 — kopia.xlsx");
if (deleteOldFile.exists()) {
deleteOldFile.delete();
System.out.println("Deleted!");
}
File fT = new File("E:\Users\PLACEHOLDER\ExcelTestowy#2.xlsx");
FileInputStream fTI = new FileInputStream(fT);
if (deleteOldFile.exists()) {
System.out.println("File exists!n--------------");
} else {
System.out.println("File doesn't existst");
}
/////////////////////////////////
XSSFWorkbook wb = new XSSFWorkbook(fTI);
XSSFSheet sh = wb.getSheetAt(0);
XSSFTable tb = sh.getTables().get(0);
tb.setDisplayName("ExampleTableName"); //Works
tb.getColumns().get(0).setName("ExampleColumnName"); // Doesn't work
tb.createColumn("ExampleAdditionalColumnINeed"); //Doesn't work too.

File fTO = new File("E:\Users\PLACEHOLDER\ExcelTestowy#2 — kopia.xlsx");
FileOutputStream fTOO = new FileOutputStream(fTO);
wb.write(fTOO);

fTOO.flush();
fTOO.close();
wb.close();
}
}

PS:我是Java的初学者 - 我在java中休息了3年。我为我的语言道歉。

XSSFTable中更改表格标题后,我们还需要更改工作表中的单元格值,该值是表格标题的表示形式。XSSFTableXSSFSheetXSSFWorkbook文档部分是不同的,即使在当前apache poi 4.1.2中,apache poi也无法保持两个部分的更新。

以下内容适用于我使用apache poi 4.1.2

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFTable;
public class ExcelRenameTable {
public static void main(String[] args) throws InvalidFormatException, IOException {
File fT = new File("./ExcelTestowy.xlsm");
FileInputStream fTI = new FileInputStream(fT);
XSSFWorkbook wb = new XSSFWorkbook(fTI);
XSSFSheet sh = wb.getSheetAt(0);
XSSFTable tb = sh.getTables().get(0);
tb.setDisplayName("ExampleTableName");
tb.getColumns().get(0).setName("ExampleColumnName");
//we also need changing the cell value in the sheet which is the representation of the table header
int tbStartRow = tb.getStartRowIndex(); // start row of table = header row
int tbStartCol = tb.getStartColIndex(); // start col of table = first col
sh.getRow(tbStartRow).getCell(tbStartCol).setCellValue("ExampleColumnName");
tb.createColumn("ExampleAdditionalColumnINeed");
//we also need changing the cell value in the sheet which is the representation of the table header
int tbEndCol = tb.getEndColIndex(); // end col of table = new created column
XSSFCell cell = sh.getRow(tbStartRow).getCell(tbEndCol);
//maybe we need creating the cell first since the column is new
if (cell == null) cell = sh.getRow(tbStartRow).createCell(tbEndCol);
cell.setCellValue("ExampleAdditionalColumnINeed");
File fTO = new File("./ExcelTestowy — kopia.xlsm");
FileOutputStream fTOO = new FileOutputStream(fTO);
wb.write(fTOO);
fTOO.close();
wb.close();
}
}

Apache POI不能创建.xlsm——就像在带有宏的Excel文件中一样。

请参阅:使用 apache poi 写入 xlsm (Excel 2007)

最新更新