关于导入APACHE POI包之后的错误

来源:互联网 发布:信赖域反射算法 编辑:程序博客网 时间:2024/06/03 23:43

今天初次使用java来制作excel,用java语言编写制作excel的时候,我导入了apach官网上最新的apache poi的poi-bin-3.13-20150929这个包,然后编写了如下程序:

package excel;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CreatFirstExcel {
public static String outputFile = 
"D:\\workSpace\\XSSFDemo\\Excel\\created\\first.xls";
public static void main(String[] args){
try{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet1 = workbook.createSheet("First Sheet");
HSSFRow row = sheet1.createRow((short)0);
HSSFCell cell1 = row.createCell((short)0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellValue("单元格内容");
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.close();
System.out.println("文件生成.OK\n"
+ "文件位置:D:\\workSpace\\XSSFDemo\\Excel\\created\\first.xls");
}catch(Exception e){
System.out.println("已运行xlCreate():"+e);
}
}
}

并没有报错
然后尝试用java来查看本地的excel文件,编写了如下程序:
public class ReadExcel {
public static void main(String[] args) throws FileNotFoundException,EncryptedDocumentException, InvalidFormatException, IOException{
InputStream excelSJBD035 = 
new FileInputStream(new File(
文件的地址));
Workbook wb =
WorkbookFactory.create(excelSJBD035);
Sheet sheet = wb.getSheetAt(0);
for (Row row:sheet){
for (Cell cell:row){
switch (cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(
cell.getBooleanCellValue()+"");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)){
System.out.println(
cell.getDateCellValue()+"");
}else{
System.out.println(
cell.getNumericCellValue()+"");
}break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(
cell.getCellFormula()+"");
break;
case Cell.CELL_TYPE_STRING:
System.out.println(
cell.getRichStringCellValue().toString()+"");
break;
}
}
System.out.println();
}
}
}
导入WorkbookFactory这个类的时候,只能手动import,但是程序运行之后就报了如下错误:

java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlOptions

org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument

通过网上搜索,试了很多方法都不对

自己感觉是apache poi的版本问题,可能新的版本还没有完善

于是下载了poi-bin-3.12-20150511这个12版本,然后把里面所有的jar包都导入项目里面

程序就运行成功了

很高兴,第一次写总结

大家一起进步

0 0
原创粉丝点击