gpt4 book ai didi

java - 使用 Apache POI 添加自定义 XML 部分

转载 作者:行者123 更新时间:2023-11-29 03:05:06 28 4
gpt4 key购买 nike

使用 Apache POI 将自定义 XML 部分添加到 XLSX 文件的正确方法是什么?

我尝试使用下面的代码创建包部分并添加与工作簿的关系,但我新添加的部分被添加为空白文件,因为工作簿清除了 POIXMLDocument#prepareForCommit() 中的包部分。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class AddCustomXmlPart {

public static void main(String[] args) {
String outputFileName = System.getProperty("user.home") + "/Documents/test-updated.xlsx";

try {
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.createSheet("Test");
addCustomXmlPart(workbook);

workbook.write(new FileOutputStream(outputFileName));

} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}

private static void addCustomXmlPart(XSSFWorkbook workbook) throws IOException, InvalidFormatException {
final OPCPackage opcPackage = workbook.getPackage();
final PackagePartName partName = PackagingURIHelper.createPartName("/customXml/item1.xml");
final PackagePart part = opcPackage.createPart(partName, ContentTypes.PLAIN_OLD_XML);
final OutputStream outputStream = part.getOutputStream();
outputStream.write("<test>A</test>".getBytes());
outputStream.close();

final PackageRelationship packageRelationship = part.addRelationship(
partName, TargetMode.INTERNAL, PackageRelationshipTypes.CUSTOM_XML);

final POIXMLDocumentPart documentPart = new POIXMLDocumentPart(workbook, part, packageRelationship);
workbook.addRelation(packageRelationship.getId(), documentPart);
}
}

最佳答案

正如 Gagravarr 所建议的:

import java.io.*;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class AddCustomXmlUsingOpc {
public static final String PARENT_PATH = System.getProperty("user.home") + "/Documents/";
public static void main(String[] args) {

String outputFileName1 = PARENT_PATH + "01.xlsx";
String outputFileName2 = PARENT_PATH + "02.xlsx";

try {
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.createSheet("Test");
workbook.write(new FileOutputStream(outputFileName1));

final OPCPackage opcPackage = OPCPackage.open(new File(outputFileName1));
addCustomXmlPart(opcPackage);
opcPackage.save(new File(outputFileName2));

} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}

private static void addCustomXmlPart(OPCPackage opcPackage) throws IOException, InvalidFormatException {
final PackagePartName partName = PackagingURIHelper.createPartName("/customXml/item1.xml");
final PackagePart part = opcPackage.createPart(partName, ContentTypes.PLAIN_OLD_XML);
final OutputStream outputStream = part.getOutputStream();
outputStream.write("<test>A</test>".getBytes());
outputStream.close();

part.addRelationship(partName, TargetMode.INTERNAL, PackageRelationshipTypes.CUSTOM_XML);

final PackagePartName workbookName = PackagingURIHelper.createPartName("/xl/workbook.xml");
final PackagePart workbookPart = opcPackage.getPart(workbookName);
workbookPart.addRelationship(partName, TargetMode.INTERNAL, PackageRelationshipTypes.CUSTOM_XML);
}

}

关于java - 使用 Apache POI 添加自定义 XML 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32561366/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com