简体   繁体   English

是否可以使用usermodel编写Excel文件并使用Apache POI中的eventmodel再次读取同一文件

[英]Is it possible to write an Excel file using usermodel and read the same file again using eventmodel in apache poi

I tried writing to a new Excel file using the following code (uses usermodel) 我尝试使用以下代码写入新的Excel文件(使用usermodel)

    private static void writeToExecelFileUsingUserModel() throws InvalidFormatException, IOException {
    String[] header = {"","A","B","C", "D","E","F","G","I","J"};
    String[] dataSet = {"1","2","3","4","5","6","7","8","9","10"};
    HSSFWorkbook workbook = new HSSFWorkbook();   
    HSSFSheet sheet = (HSSFSheet) workbook.createSheet("testSheet");
    HSSFRow row = sheet.createRow(0);
    for(int i= 0; i <header.length; i++ ){
        HSSFCell cell = row.createCell(i);
        cell.setCellValue(header[i]);
    }
    HSSFRow row2 = sheet.createRow(1);
    for(int i= 0; i <dataSet.length; i++ ){
        HSSFCell cell = row2.createCell(i);
        cell.setCellValue(dataSet[i]);
    }

    try {
        FileOutputStream fos = new FileOutputStream("C:\\Test.xls");
        workbook.write(fos);
        System.out.println("write complete");
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Then, I used the same file and tried reading using eventmodel using the code below. 然后,我使用了相同的文件,并尝试使用下面的代码使用eventmodel进行读取。 It gave the error: 它给出了错误:

Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:199) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:665) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274) at com.benz.test.ReadFromExcel.readUsingEventModel(ReadFromExcel.java:34) at com.benz.test.ReadFromExcel.main(ReadFromExcel.java:24) 线程“主”中的异常org.apache.poi.openxml4j.exceptions.InvalidFormatException:包应在org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:199处包含内容类型部分[M1.13] )在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)在org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)在org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:665)位于com.benz.test.ReadFromExcel.main(ReadFromExcel.java:24)的ReadFromExcel.readUsingEventModel(ReadFromExcel.java:34)

//code for reading from previously generated xls file using eventmodel //使用eventmodel从先前生成的xls文件读取的代码

    private static void readUsingEventModel() throws IOException, OpenXML4JException {

    InputStream excelStream = null;
    OPCPackage pkg = null;
    System.out.println("reading using event model");
    try {
        FileInputStream myxls = new FileInputStream("C:\\Test.xls");
        pkg = OPCPackage.open(myxls);
        XSSFReader xssfReader = new XSSFReader(pkg);
        XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        String sheetName = iter.getSheetName();
        System.out.println("sheet name is"+sheetName);
    } finally {
        if (excelStream != null) {
            excelStream.close();
        }
        if (pkg != null) {
            pkg.close();
        }
    }

}

Also the same scenario (ie writing using usermodel and reading using event model) works fine for xlsx file but does not work for xls files. 同样的情况(即使用usermodel进行写入和使用事件模型进行读取)对于xlsx文件也可以,但不适用于xls文件。 Also I cannot use usermodel for reading as it is giving performance issues Any help would be greatly appreciated.Thanks 我也不能使用usermodel进行读取,因为它会导致性能问题。任何帮助将不胜感激。

Your first set of code is all HSSF, which only works for .xls files: 您的第一组代码是全部HSSF, 适用于.xls文件:

HSSFWorkbook workbook = new HSSFWorkbook();  

Then, a little later, you're suddenly trying to use the XSSF code which only works for .xlsx files: 然后,稍后,您突然尝试使用适用于.xlsx文件的XSSF代码:

OPCPackage pkg = null;
XSSFReader xssfReader = new XSSFReader(pkg);

You have two choices. 您有两种选择。 Firstly, you can change your initial code to be XSSF, using XSSFWorkbook and friends. 首先,您可以使用XSSFWorkbook和朋友将初始代码更改为XSSF。 If you generate your Excel file with XSSF as a .xlsx , then you can read it with XSSF code. 如果使用XSSF作为.xlsx生成Excel文件,则可以使用XSSF代码读取它。 Alternately, if you really want to be using HSSF / .xls for generation, and you want to use low-memory reading, then you need to use the HSSF Event API to do your read 或者,如果您确实要使用HSSF / .xls进行生成,并且要使用低内存读取,则需要使用HSSF Event API进行读取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM