簡體   English   中英

使用Apache POI在與復雜公式相關聯的excel文件中獲取確切的最后填充行號

[英]Get exact last filled row number in an excel file that has is associated with a complex formula using Apache POI

一個復雜的公式會應用於一列直到Excel表格的末尾。但是當我使用Sheet.getLastRowNum()檢索最后一行時,盡管excel表格只有10行,但它返回的是表格的最后一行。即使公式應用於Excel工作表的末尾,有什么方法可以在Excel文件中獲得確切的最后填充行?

來自http://affy.blogspot.cz/2004/04/poi-optimization-eliminate-trailing.html
檢索所需的公式,然后使用此代碼。 它將刪除所有這些空行,然后您可以使用數據檢索代碼獲得所需的數據。

List yourlist = new ArrayList();
        String value = null;
        FileInputStream inputStreams1 = null;
        inputStreams1 = new FileInputStream(FileNameWithPath);
        org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(inputStreams1);
        HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();

        boolean stop = false;
        boolean nonBlankRowFound;
        short c;
        HSSFRow lastRow = null;
        HSSFCell cell = null;
        HSSFRow FirsttRow = null;

        while (stop == false) {
            nonBlankRowFound = false;
            lastRow = (HSSFRow) sheet.getRow(sheet.getLastRowNum());
            for (c = lastRow.getFirstCellNum(); c <= lastRow.getLastCellNum(); c++) {
                cell = lastRow.getCell(c);
                if (cell != null && lastRow.getCell(c).getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    nonBlankRowFound = true;
                }
            }
            if (nonBlankRowFound == true) {
                stop = true;

                yourlist =  yourMethod(FileNameWithPath, sheet); //updated HSSFSheet object which will have 10 rows
                /*use this code to remove all those empty rows then use hsheet object. now use this hsheet object will
                 have only non empty rows(in your case 10 rows)*/

            } else {
                sheet.removeRow(lastRow);


            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM