我试图读取一个文本文件,然后在其上应用一些数据验证规则。添加规则后,我然后将数据写回到excel文件中。
但是当尝试将其写回到excel文件时,出现此错误:
线程“主”中的异常 org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:失败 保存:保存软件包时发生错误:零件 /docProps/app.xml无法使用marshaller保存在流中 org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@74ad1f1f 在 org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:479) 在org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1414) 在org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:179)处 com.gbt.POC.TxtFileReader.main(TxtFileReader.java:359)由以下原因引起: org.apache.poi.openxml4j.exceptions.OpenXML4JException:该部分 /docProps/app.xml无法使用marshaller保存在流中 org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@74ad1f1f 在 org.apache.poi.openxml4j.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);
任何帮助都需要事先感谢。
答案 0 :(得分:0)
我复制并粘贴了您的代码以查看它是什么错误。 不幸的是,我看不到您的错误。 但是搜索错误后,我找到了一些答案。
发生此异常是因为Excel工作表的POI API写入 Excel工作表多次。并且每次需要一个新的 相同File对象上FileOutputStream的实例。
要解决此问题,请先将所有行写入工作簿实例,然后在 确实使用FileOutputStream写入工作簿。
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save
希望您能得到提示。
接下来, 我制作了txt文件,并像您所做的一样保存为excel, 可以,
@Entity
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_user")
private Long id;
@Column(name = "login", nullable = false, unique = true)
private String username;
@NotEmpty
private String password;
@NotEmpty
private String email;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_details")
private UserDetails details;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Set<UserRole> roles = new HashSet<>();
@OneToMany
private List<Order> orders;
public User() {
}
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public UserDetails getDetails() {
return details;
}
public void setDetails(UserDetails details) {
this.details = details;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public Set<UserRole> getRoles() {
return roles;
}
public void setRoles(Set<UserRole> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email
+ ", details=" + details + orders.size() + ", orders=" + orders + "]";
}
}
答案 1 :(得分:0)
您可以使用Xcelite library来简化生活:
首先创建一个小bean类
创建一个Xcelite对象并让它创建一个工作表
找作家
写你的豆子
写入OutputStream
character
import com.ebay.xcelite.annotations.Column;
public class Entity {
@Column
String empid1;
@Column
String fname;
@Column
String ccode1;
}
Xcelite xcelite = new Xcelite();
XceliteSheet sheet = xcelite.createSheet("Clean");
SheetWriter writer = sheet.getBeanWriter(Entity.class);
总结起来,这是修改后的代码:
writer.write(entities);
xcelite.write(outstream);