【发布时间】:2017-11-12 22:25:51
【问题描述】:
我是 java 的初学者,我正在尝试做一个小的 java 程序来读取一个 excel 文件,并在最后添加一个新行来更新文件。然后再次读取并使用新行 ecc、ecc.. 更新。
这是我阅读的文件(boh.xls):
当他转到“workBook.write()”时它会抛出这个错误:
> XLS FILE -----> Start reading...
0
CELL: 0 [ Hi ] CELL: 1 [ empty cell ] CELL: 2 [ Roby ] 1
CELL: 0 [ Ciao ] CELL: 1 [ bobbi ] 2
CELL: 0 [ empty cell ] CELL: 1 [ Great ] CELL: 2 [ Job ]
3
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/bidimap/TreeBidiMap
at org.apache.poi.hpsf.Section.<init>(Section.java:178)
at org.apache.poi.hpsf.MutableSection.<init>(MutableSection.java:41)
at org.apache.poi.hpsf.PropertySet.init(PropertySet.java:494)
at org.apache.poi.hpsf.PropertySet.<init>(PropertySet.java:196)
at org.apache.poi.hpsf.MutablePropertySet.<init>(MutablePropertySet.java:44)
at org.apache.poi.hpsf.SpecialPropertySet.<init>(SpecialPropertySet.java:47)
at org.apache.poi.hpsf.DocumentSummaryInformation.<init>(DocumentSummaryInformation.java:99)
at org.apache.poi.hpsf.PropertySetFactory.create(PropertySetFactory.java:116)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:236)
at org.apache.poi.POIDocument.getPropertySet(POIDocument.java:197)
at org.apache.poi.POIDocument.readPropertySet(POIDocument.java:175)
at org.apache.poi.POIDocument.readProperties(POIDocument.java:158)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.updateEncryptionInfo(HSSFWorkbook.java:2295)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1506)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1428)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1414)
at excel_01.Read_03.readExcel_03(Read_03.java:91)
at excel_01.Read_03.main(Read_03.java:108)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.bidimap.TreeBidiMap
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more
这是我写的代码:
1) 首先我读取文件 2)然后我检查最后一行是否为空(图像中的第四行) 3) 最后,我尝试使用对象“Bau”更新新行(第 4 行)。但它不起作用。
请问有什么建议吗?谢谢各位!
package excel_01;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class Read_03 {
private static final String FILE_NAME = "/EXCEL_PER_ECLIPSE/boh.xls";
public void readExcel_03() {
try {
FileInputStream input = new FileInputStream(FILE_NAME);
// Workbook workBook = WorkbookFactory.create(input);
Workbook workBook = new HSSFWorkbook(input);
System.out.println("XLS FILE" + " -----> " + "Start reading...");
Sheet sheet = workBook.getSheetAt(0);
System.out.println(sheet.getPhysicalNumberOfRows());
Row row = null;
for ( int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
row = sheet.getRow(j);
System.out.println(row.getRowNum());
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i) != null) {
Cell cell = row.getCell(i/*, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK*/);
System.out.print(" CELL: " + i + " [ " + cell.toString() + " ] ");
}
else {
System.out.print(" CELL: " + i + " [ empty cell ] ");
}
}
}
int physicalRow = sheet.getPhysicalNumberOfRows();
Row lastPhysicalRow = sheet.getRow(physicalRow);
int lastRow = sheet.getLastRowNum();
//Row rowprova1 = sheet.getRow(prova1);
if (physicalRow > lastRow && lastPhysicalRow == null /*here I check if the last row is null*/) {
System.out.println();
System.out.println(physicalRow);
Object[][] bookData = {
{"Bau"},
};
int rowCount = sheet.getPhysicalNumberOfRows();
for (Object[] newElement : bookData) {
Row newRow = sheet.createRow(++rowCount);
int columnCount = 0;
Cell newCell = newRow.createCell(columnCount);
newCell.setCellValue(rowCount);
for (Object field : newElement) {
newCell = newRow.createCell(++columnCount);
if (field instanceof String) {
newCell.setCellValue((String) field);
}
}
}
}
input.close();
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workBook.write(outputStream);
workBook.close();
outputStream.close();
System.out.println("End reading!");
}
catch (Exception e) {
System.out.println("Error");
e.printStackTrace();
}
}
public static void main(String[] args) {
Read_03 read_03 = new Read_03();
read_03.readExcel_03();
}
}
【问题讨论】:
-
我假设您已经确认 Apache Commons Collections 在您的类路径中?
-
我使用 POI 3.17,但我没有使用 Apache Commons Collectons
-
我已经添加了 apache commons 集合,现在它似乎可以工作了!谢谢!!
-
酷。在这种情况下,请考虑投票并接受此作为答案。
-
完成 ;) 但投票没有出现