org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file
来源:互联网 发布:使用手册制作软件 编辑:程序博客网 时间:2024/05/01 18:56
http://fengsky491.iteye.com/blog/537458
环境和所需包:
1,JDK1.5
2,poi-3.5-FINAL-20090928.jar,
poi-contrib-3.5-FINAL-20090928.jar,
poi-ooxml-3.5-FINAL-20090928.jar,
poi-scratchpad-3.5-FINAL-20090928.jar,
log4j-1.2.13.jar,
commons-logging-1.1.jar,
junit-3.8.1.jar,
dom4j-1.6.1.jar,
geronimo-stax-api_1.0_spec-1.0.jar,
ooxml-schemas-1.0.jar,
xmlbeans-2.3.0.jar
注意 :
1,可能有些包不需要,没有测试,因为有些包项目中已经存在了
2,我开始少了最后2个包,报:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
Java代码:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- 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;
- public class POIExcelDemo {
- public static void read(String fileName) throws Exception {
- XSSFWorkbook wb = new XSSFWorkbook(fileName);
- read(wb);
- }
- public static void read(InputStream is) throws Exception {
- XSSFWorkbook wb = new XSSFWorkbook(is);
- read(wb);
- }
- public static void read(XSSFWorkbook xwb) throws Exception {
- try {
- for (int k = 0; k < xwb.getNumberOfSheets(); k++) {
- XSSFSheet sheet = xwb.getSheetAt(k);
- int rows = sheet.getPhysicalNumberOfRows();
- for (int r = 0; r < rows; r++) {
- // 定义 row
- XSSFRow row = sheet.getRow(r);
- if (row != null) {
- int cells = row.getPhysicalNumberOfCells();
- for (short c = 0; c < cells; c++) {
- XSSFCell cell = row.getCell(c);
- if (cell != null) {
- String value = null;
- switch (cell.getCellType()) {
- case XSSFCell.CELL_TYPE_FORMULA:
- value = "FORMULA ";
- break;
- case XSSFCell.CELL_TYPE_NUMERIC:
- if(HSSFDateUtil.isCellDateFormatted(cell)){
- value = "DATE value="
- + cell.getDateCellValue();
- }else{
- value = "NUMERIC value="
- + cell.getNumericCellValue();
- }
- break;
- case XSSFCell.CELL_TYPE_STRING:
- value = "STRING value="
- + cell.getStringCellValue();
- break;
- case XSSFCell.CELL_TYPE_BOOLEAN:
- value = "BOOLEAN value="
- + cell.getBooleanCellValue();
- cell.getDateCellValue();
- break;
- default:
- }
- System.out.println(value);
- }
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- File f = new File("d:/Test.xlsx");
- FileInputStream is = new FileInputStream(f);
- System.out.println(f.getName());
- read(is);
- }
- }
import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFDateUtil;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;public class POIExcelDemo {public static void read(String fileName) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(fileName); read(wb);}public static void read(InputStream is) throws Exception {XSSFWorkbook wb = new XSSFWorkbook(is);read(wb);}public static void read(XSSFWorkbook xwb) throws Exception {try {for (int k = 0; k < xwb.getNumberOfSheets(); k++) {XSSFSheet sheet = xwb.getSheetAt(k);int rows = sheet.getPhysicalNumberOfRows();for (int r = 0; r < rows; r++) {// 定义 rowXSSFRow row = sheet.getRow(r);if (row != null) {int cells = row.getPhysicalNumberOfCells();for (short c = 0; c < cells; c++) {XSSFCell cell = row.getCell(c);if (cell != null) {String value = null;switch (cell.getCellType()) {case XSSFCell.CELL_TYPE_FORMULA:value = "FORMULA ";break;case XSSFCell.CELL_TYPE_NUMERIC:if(HSSFDateUtil.isCellDateFormatted(cell)){value = "DATE value="+ cell.getDateCellValue();}else{value = "NUMERIC value="+ cell.getNumericCellValue();}break;case XSSFCell.CELL_TYPE_STRING:value = "STRING value="+ cell.getStringCellValue();break;case XSSFCell.CELL_TYPE_BOOLEAN:value = "BOOLEAN value="+ cell.getBooleanCellValue();cell.getDateCellValue();break;default:}System.out.println(value);}}}}}} catch (Exception e) {e.printStackTrace();}}/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubFile f = new File("d:/Test.xlsx");FileInputStream is = new FileInputStream(f);System.out.println(f.getName());read(is);}}
写完之后完把文件改成读取test.xls(Excel2003),发现出现Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'C:\DOCUME~1\CHENXI~1\LOCALS~1\Temp\poifiles\poi-ooxml-1966473540.tmp'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
所以为了兼容读取2003,我把代码给成如下:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- 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;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class POIExcelDemo {
- /**
- *
- * @param fileName 文件路径
- * @param flag 是2003还是2007 true:2003,false:2007
- * @throws Exception
- */
- public static void read(String fileName,boolean flag) throws Exception {
- Workbook wb = null;
- if(flag){//2003
- File f = new File(fileName);
- FileInputStream is = new FileInputStream(f);
- POIFSFileSystem fs = new POIFSFileSystem(is);
- wb = new HSSFWorkbook(fs);
- is.close();
- }else{//2007
- wb = new XSSFWorkbook(fileName);
- }
- read(wb);
- }
- /**
- *
- * @param is 输入流
- * @param flag 是2003还是2007 true:2003,false:2007
- * @throws Exception
- */
- public static void read(InputStream is,boolean flag) throws Exception {
- Workbook wb = null;
- if(flag){//2003
- wb = new HSSFWorkbook(is);
- }else{//2007
- wb = new XSSFWorkbook(is);
- }
- read(wb);
- }
- /**
- * 具体读取Excel
- * @param wb
- * @throws Exception
- */
- public static void read(Workbook wb) throws Exception {
- try {
- for (int k = 0; k < wb.getNumberOfSheets(); k++) {
- //sheet
- Sheet sheet = wb.getSheetAt(k);
- int rows = sheet.getPhysicalNumberOfRows();
- for (int r = 0; r < rows; r++) {
- // 定义 row
- Row row = sheet.getRow(r);
- if (row != null) {
- int cells = row.getPhysicalNumberOfCells();
- for (short c = 0; c < cells; c++) {
- Cell cell = row.getCell(c);
- if (cell != null) {
- String value = null;
- switch (cell.getCellType()) {
- case Cell.CELL_TYPE_FORMULA:
- value = "FORMULA value=" + cell.getCellFormula();
- break;
- case Cell.CELL_TYPE_NUMERIC:
- if(HSSFDateUtil.isCellDateFormatted(cell)){
- value = "DATE value="
- + cell.getDateCellValue();
- }else{
- value = "NUMERIC value="
- + cell.getNumericCellValue();
- }
- break;
- case Cell.CELL_TYPE_STRING:
- value = "STRING value="
- + cell.getStringCellValue();
- break;
- case Cell.CELL_TYPE_BOOLEAN:
- value = "BOOLEAN value="
- + cell.getBooleanCellValue();
- cell.getDateCellValue();
- break;
- default:
- }
- System.out.println(value);
- }
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- File f = new File("d:/test.xlsx");
- FileInputStream is = new FileInputStream(f);
- System.out.println(f.getName());
- read(is,false);
- }
- }
- org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file
- Unhandled exception: org.apache.poi.openxml4j.exceptions.InvalidFormatException
- POI 操作Excel 异常org.apache.poi.openxml4j.exceptions.invalidformatexception: package should contain a c
- Execption ......org.apache.poi.openxml4j.opc.PackageRelationshipCollectio
- monkeyrunner Can't open specified script file
- Can't open file 'folder/.svn/tmp/text-base/file.svn-base': The system cannot find the file specified的实用解决方法
- can't open the link
- Can't open file: '%s.MYI'
- Can't open chm file under Win7?
- svn can't open file 解决方法
- gawk: fatal: can't open source file
- vim Can't open file for writing
- MYSQL错误:Can’t open file: ‘×××...'
- can't open include file "model.ice"
- mfs can't open metadata file
- E212: Can't open file for writing
- apache + svn Can't open file '*/txn-current-lock': Permission denied
- Open the file under the specified path, unlimited file name
- 如何从一个对话框弹出单文档视图
- S3C2440对Nand Flash操作和电路原理(基于K9F2G08U0A)
- UINCODE环境下编写剪贴板程序代码
- eclipse 控制台输出ibatislog
- 创建与调用以axis实现的stub方式webservice服务
- org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file
- ERROR: unknown option journal
- Visual Studio 2010自动化Web应用测试实践
- MFC中Picture Control如何添加图片
- 利用 OpenScales+MapServer+PostGIS 快速构建 GIS 应用
- LINUX 查看硬件配置命令
- jquery查找元素方法示例
- Coded UI Test功能扩展
- 程序员专用经典语录