java - 尝试将数据插入回 Excel 文件时出错
问题描述
我正在尝试读取文本文件,然后对其应用一些数据验证规则。添加规则后,我将数据写回excel文件。
但是,当尝试将其写回 excel 文件时,我收到此错误:
线程“main” org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException 中的异常:保存失败:保存包时出错:部分 /docProps/app.xml 无法使用编组器 org.apache 保存在流中.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@74ad1f1f at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:479) at org.apache.poi.openxml4j.opc.OPCPackage.save( OPCPackage.java:1414) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:179) at com.gbt.POC.TxtFileReader.main(TxtFileReader.java:359) 原因:org.apache.poi.openxml4j .exceptions.OpenXML4JException:/docProps/app.xml 部分无法保存在 org.apache.poi.openxml4j 的 marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@74ad1f1f 的流中。opc.ZipPackage.saveImpl(ZipPackage.java:470) ... 还有 3 个
这是我到目前为止所拥有的:
LinkedList < String[] > llist = new LinkedList < > ();
String[] data;
XSSFWorkbook workBook = new XSSFWorkbook();
FileOutputStream outstream = new FileOutputStream("data.xls");
XSSFSheet spreadSheet = workBook.createSheet("Clean");
for (int i = 0; i < llist.size(); i++) {
if (i == 0) {
System.out.println("Hello World!");
} else {
data = llist.get(i);
String empid1 = data[0];
String fname = data[1];
String ccode1 = data[2];
if (data[2].equals("IND")) {
replace = data[2].replaceAll("IND", "IN");
ccode1 = replace;
} else if (data[2].equals("USA")) {
replace = data[2].replaceAll("USA", "US");
ccode1 = replace;
} else {
ccode1 = data[2];
}
//String newData=empid1+","+fname+","+ccode1;
XSSFRow row = spreadSheet.createRow(i);
XSSFCell cell = row.createCell(0);
cell.setCellValue(empid1);
cell = row.createCell(1);
cell.setCellValue(fname);
cell = row.createCell(2);
cell.setCellValue(ccode1);
}
}
workBook.write(outstream);
提前感谢任何帮助。
解决方案
I copied and pasted your code to see what error is it. Unfortunately I couldn't see your error. But after searching the error I found some answers..
This exception occurred because the POI API for excel sheet writes to the excel sheet multiple times. And each time it requires a new instance of FileOutputStream over same File object.
To resolve this, wrote all Rows to the workbook instance first, and at one did write to the workbook using FileOutputStream.
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save
Hope you get a hint.
Next, I made my txt file and save to excel like what you did and It works,
package excelTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class readExcel {
public static void main(String[] args) throws IOException {
LinkedList<String> llist = new LinkedList<>();
String[] data = null;
String replace = "";
XSSFWorkbook workBook = new XSSFWorkbook();
FileOutputStream outstream = new FileOutputStream( "C:\\Users\\Desktop\\data.xls");
XSSFSheet spreadSheet = workBook.createSheet("Clean");
/*
* MyFile.txt
----------------------
0
IND
USA
LOVE
----------------------
*/
try (BufferedReader br = new BufferedReader(new FileReader( "C:\\Users\\Desktop\\myFile.txt"))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
llist.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
String empid1 = "";
String fname = "";
String ccode1 = "";
System.out.println("llist.get(i)"+llist.get(0)+" "+llist.get(1));
for (int i = 0; i < llist.size(); i++){
System.out.println(llist.get(i));
}
empid1 = llist.get(1); //IND
fname = llist.get(2); //USA
ccode1 =llist.get(3); //LOVE
if (fname.equals("IND")) {
replace = fname.replaceAll("IND", "IN");
ccode1 = replace;
} else if (fname.equals("USA")) {
replace = fname.replaceAll("USA", "US");
ccode1 = replace; //'LOVE' CHANGE TO US. (IF YOU WANT TO CHANGE TO US IN HERE-> fname=fname.replaceAll("USA","US");
} else {
ccode1 = fname; //'LOVE' CHANGE TO USA AGAIN.
}
XSSFRow row = spreadSheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue(empid1);
cell = row.createCell(1);
cell.setCellValue(fname);
cell = row.createCell(2);
cell.setCellValue(ccode1);
workBook.write(outstream);
}
}
推荐阅读
- .net - .NET 5 无法正确显示 PropertyGrid
- amazon-web-services - 有什么方法可以避免破坏和重新创建子网,同时使用 count 方法创建子网而不是一个一个地创建子网?
- javascript - 删除网格中行下方的空白
- python - 难以让 google-coral example-object-detection 正常工作
- c# - 根据一个字符或另一个字符提取部分文本
- elasticsearch - Kibana:显示带有特定标签的日志
- java - 如何解决 org.apache.camel.FailedToCreateProducerException CaughtExceptionMessage: Failed to create Producer for endpoint?
- python - 有没有像 Django 的拱单元这样的库?
- java - Android Java Alarm Manager Pending Intent 无法删除
- nginx - Nginx 检查 Cloudflare 是转发还是直接 IP 并相应限制