如何在java中阅读大的.xls文件?

时间:2015-01-21 06:56:53

标签: java excel apache-poi

10在java中读取xls文件 但是,当我要阅读20 MB以上的大.xls文件时,它会给我错误 我的代码正在运行正确的小.xls文件,但为大.xls文件提供java堆错误。 Java代码 -

public static void main(String[] args) throws IOException {
        ArrayList<ArrayList<String>> Temp = new ArrayList<ArrayList<String>>();
        ArrayList<String> Temp1 = new ArrayList<String>();
        int row = 0;
        String fname = "D:/Vijay/xls/vijay/bookTest.xls";
        try {
            InputStream fis = new FileInputStream(fname);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);
            FormulaEvaluator formulaEval = workbook.getCreationHelper().createFormulaEvaluator();
            int rowEnd = sheet.getLastRowNum();
            int rowStart = sheet.getFirstRowNum();
            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
                Row r = sheet.getRow(rowNum);
                int lastColumn = r.getLastCellNum();

                int cols = 0;
                Temp1 = new ArrayList<String>();
                for (int cn = 0; cn < lastColumn; cn++) {
                    String cellvalue = "";
                    Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
                    if (c == null) {
                        cellvalue = "";
                    } else {
                        if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_STRING) {
                            cellvalue = r.getCell(cn).getStringCellValue();
                        } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            if (HSSFDateUtil.isCellDateFormatted(r.getCell(cn))) {
                                DateFormat formatter = new SimpleDateFormat(
                                        "E MMM dd HH:mm:ss Z yyyy");
                                Date date = (Date) formatter.parse(r
                                        .getCell(cn).getDateCellValue()
                                        .toString());
                                Calendar cal = Calendar.getInstance();
                                cal.setTime(date);
                                cellvalue = cal.get(Calendar.DATE) + "/"
                                        + (cal.get(Calendar.MONTH) + 1) + "/"
                                        + cal.get(Calendar.YEAR);
                            } else {
                                r.getCell(cn).setCellType(
                                        r.getCell(cn).CELL_TYPE_STRING);
                                cellvalue = ""
                                        + r.getCell(cn).getStringCellValue();
                            }
                        } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                            cellvalue = ""
                                    + r.getCell(cn).getBooleanCellValue();
                        } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                            cellvalue = ""
                                    + formulaEval.evaluate(r.getCell(cn))
                                            .formatAsString();
                        }

                    }
                    Temp1.add(cols, cellvalue);
                    cols++;
                }
                if (Temp1.size() > 0) {
                    Temp.add(row, Temp1);
                    row++;
                }
            }
             for (ArrayList al : Temp) {
             System.out.println("Contents of temp " + al);
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

错误 -

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.LinkedHashMap.createEntry(Unknown Source)
    at java.util.LinkedHashMap.addEntry(Unknown Source)
    at java.util.HashMap.put(Unknown Source)
    at sun.util.resources.OpenListResourceBundle.loadLookup(Unknown Source)
    at sun.util.resources.OpenListResourceBundle.loadLookupTablesIfNecessary(Unknown Source)
    at sun.util.resources.OpenListResourceBundle.handleGetObject(Unknown Source)
    at sun.util.resources.TimeZoneNamesBundle.handleGetObject(Unknown Source)
    at java.util.ResourceBundle.getObject(Unknown Source)
    at java.util.ResourceBundle.getObject(Unknown Source)
    at java.util.ResourceBundle.getStringArray(Unknown Source)
    at sun.util.TimeZoneNameUtility.retrieveDisplayNames(Unknown Source)
    at sun.util.TimeZoneNameUtility.retrieveDisplayNames(Unknown Source)
    at java.util.TimeZone.getDisplayNames(Unknown Source)
    at java.util.TimeZone.getDisplayName(Unknown Source)
    at java.util.Date.toString(Unknown Source)
    at com.test.arrayList.ValidateXls.main(ValidateXls.java:69)

请帮我解决这个问题,或者请教我另一种在java中读取.xls文件的方法 提前谢谢。

1 个答案:

答案 0 :(得分:4)

我认为您需要尝试的第一件事是增加java默认堆空间。      例如:-Xms256m -Xmx512m -XX:PermSize = 64M -XX:MaxPermSize = 1000M

也  你需要根据poi文档更改文件加载(如下)(WorkbookFactory.create(新文件(“MyExcel.xls”)))看到这个链接

http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream

Files vs InputStreams

打开工作簿(.xls HSSFWorkbook或.xlsx XSSFWorkbook)时,可以从File或InputStream加载工作簿。使用File对象可以降低内存消耗,而InputStream需要更多内存,因为它必须缓冲整个文件。

如果使用WorkbookFactory,则很容易使用其中一个:

//使用文件   工作簿wb = WorkbookFactory.create(新文件(“MyExcel.xls”));

//使用InputStream,需要更多内存   工作簿wb = WorkbookFactory.create(new FileInputStream(“MyExcel.xlsx”));

如果您仍然面临相同的异常,请尝试使用

XSSF和SAX(事件API)

http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api