我遇到了apache poi的问题。 我们正在尝试使用excel中的数据库创建一个字典,因此我们使用apache poi将Java与excel连接起来。当我们保存它时,当我们尝试向excel添加新行时,我们会收到错误。这是代码部分,如果需要更多编码,我们会给出错误。
public void cvsToExcel() throws IOException, InvalidFormatException{
try{
CSVReader reader = new CSVReader(new FileReader("table.csv"),'$');
String[] line;
CreationHelper helper = ExcelBase.workbook.getCreationHelper();
Sheet firstSheet = ExcelBase.workbook.getSheetAt(0);
CellStyle cellStyle = ExcelBase.workbook.createCellStyle();
cellStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy.mm.dd"));
int r = 0;
line = reader.readNext();
while(line != null){
org.apache.poi.ss.usermodel.Row row = firstSheet.createRow(r++);
for(int i = 0; i < line.length; i++){
if(i == 0 || i == 11){
try{
Cell c = row.createCell(i);
c.setCellType(CellType.NUMERIC);
c.setCellValue(Integer.parseInt(line[i]));
//row.createCell(i).setCellValue(Integer.parseInt(line[i]));
}catch(Exception e){
System.out.println(e);
}
}else if(i == 1){
try{
String[] strTmp = line[i].split("-");
Date tmpDate = new Date(Integer.parseInt(strTmp[0])-1900,Integer.parseInt(strTmp[1]),Integer.parseInt(strTmp[2]));
Cell c = row.createCell(i);
c.setCellValue(HSSFDateUtil.getExcelDate(tmpDate));
c.setCellStyle(cellStyle);
}catch(Exception e){
System.out.println(e);
}
}
else{
System.out.println("Row: " + row.getRowNum() + " Cell:" + i);
row.createCell(i).setCellValue(line[i]);
}
}
line = reader.readNext();
}
saveToXLSX();
}catch(Exception e){
e.printStackTrace();
}
}
每次导入都不是问题所在。 感谢您的帮助,我希望你们能帮助我:D
编辑:我忘记了错误:java.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32,767 characters
at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:436)
at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:417)
at hu.itsh.gyakorlat.szotar.io.excel.SaveActions.cvsToExcel(SaveActions.java:138)
at hu.itsh.gyakorlat.szotar.io.excel.SaveActions.fullSave(SaveActions.java:166)
at hu.itsh.gyakorlat.szotar.ui.actions.ActionSaveDb$1.doInBackground(ActionSaveDb.java:36)
at hu.itsh.gyakorlat.szotar.ui.actions.ActionSaveDb$1.doInBackground(ActionSaveDb.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)