【问题标题】:Tracking row numbers to display partial excel Apache POI跟踪行号以显示部分 excel Apache POI
【发布时间】:2019-11-07 16:07:28
【问题描述】:

我的要求是将excel表作为超级数据集读取,并根据开始和结束文本显示其中的一部分作为某一列中的值。我能够遍历整个工作表,但无法跟踪我可以在以后迭代以迭代部分工作表的行号。

下面是我的 JSP 代码

<%@page import="org.apache.poi.ss.util.CellReference"%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFCell"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.ss.usermodel.DateUtil"%>
<%@page import="org.apache.poi.ss.usermodel.CellType"%>
<%@page import="org.apache.poi.ss.usermodel.Cell"%>
<%@page import="org.apache.poi.ss.usermodel.Row"%>
<%@page import="org.apache.poi.ss.usermodel.Sheet"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
<%@page import="org.apache.poi.ss.usermodel.Workbook"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <table border="4">
            <%
                short a = 0;
                short b = 1;
                short c = 2;
                short d = 3;
                short ee = 4;
                short f = 5;
                short g = 6;
                short h = 7;
                short l = 8;
                short m = 9;
                int i = 0;
                List<Integer> rowCountList = new ArrayList<Integer>();
                String value1 = "", value2 = "", value3 = " ", value4 = "", value5 = "", value6 = "", value7 = "", value8 = "", value9 = "", value10 = "";
                String filename = "Temp.xlsx";
                if (filename != null && !filename.equals(
                        "")) {
                    try {
                        FileInputStream fs = new FileInputStream(new File(filename));
                        Workbook wb = new XSSFWorkbook(fs);
                        for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                            int j = i + 1;
                            Sheet sheet = wb.getSheetAt(k);
                            int rows = sheet.getPhysicalNumberOfRows();
                            System.out.println("Rows:" + rows);
                            for (int r = 0; r < rows; r++) {
                                value1 = "";
                                value2 = "";
                                value3 = " ";
                                value4 = "";
                                value5 = "";
                                value6 = "";
                                value7 = "";
                                value8 = "";
                                value9 = "";
                                value10 = "";
                                System.out.println("row no:" + r);
                                Row row = sheet.getRow(r);
//                                int cells = row.getPhysicalNumberOfCells();
//                                System.out.println(cells);
                                out.write("<br>");
                                Cell cell1 = row.getCell(a, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                if (cell1.getCellType() != CellType.BLANK) {
                                    switch (cell1.getCellType()) {
                                        case STRING:
                                            value1 = cell1.getStringCellValue();
                                            break;
                                        case NUMERIC:
                                            value1 = " " + cell1.getNumericCellValue();
                                            break;
                                    }
                                }
                                Cell cell2 = row.getCell(b, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                if (cell2.getCellType() != CellType.BLANK) {   
                                switch (cell2.getCellType()) {
                                    case STRING:
                                        value2 = cell2.getStringCellValue();
                                        break;

                                    case NUMERIC:
                                        if (DateUtil.isCellDateFormatted(cell2)) {
                                            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                                            value2 = "" + dateFormat.format(cell2.getDateCellValue());
                                        } else {
                                            value2 = " " + cell2.getNumericCellValue();
                                        }
                                        break;
                                }
                            }
                                Cell cell3 = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell3.getCellType()) {
                                    case STRING:
                                        value3 = cell3.getStringCellValue();
                                        break;

                                    case NUMERIC:
                                        value3 = " " + cell3.getNumericCellValue();
                                        break;
                                }
                                Cell cell4 = row.getCell(d, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell4.getCellType()) {
                                    case STRING:
                                        value4 = cell4.getStringCellValue();
                                        break;

                                    case NUMERIC:
                                        value4 = " " + cell4.getNumericCellValue();
                                        break;
                                }
                                Cell cell5 = row.getCell(ee, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell5.getCellType()) {
                                    case STRING:
                                        value5 = cell5.getStringCellValue();
                                        break;

                                    case NUMERIC:
                                        value5 = " " + cell5.getNumericCellValue();
                                        break;
                                }
                                Cell cell6 = row.getCell(f, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell6.getCellType()) {
                                    case STRING:
                                        value6 = cell6.getStringCellValue();
                                        break;

                                    case NUMERIC:
                                        value6 = " " + cell6.getNumericCellValue();
                                        break;
                                }

                                Cell cell7 = row.getCell(g, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell7.getCellType()) {
                                    case STRING:
                                        value7 = cell7.getRichStringCellValue().getString();
                                        break;

                                    case NUMERIC:
                                        value7 = " " + cell7.getNumericCellValue();
                                        break;
                                }

                                Cell cell8 = row.getCell(h, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//                                    if (cell8.getCellType() != CellType.BLANK) { 
                                switch (cell8.getCellType()) {
                                    case STRING:
                                        value8 = cell8.getRichStringCellValue().getString();
                                        **if (value8.equalsIgnoreCase("Execution Start")) {
                                            System.out.println(row.getRowNum());
                                            for (int x = row.getRowNum(); x < sheet.getLastRowNum(); x++) {
                                                Row rowx = sheet.getRow(x);
                                                Cell cellx = rowx.getCell(h, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                                if (!cellx.getRichStringCellValue().getString().equalsIgnoreCase("Execution End")) {
                                                    rowCountList.add(x);**
                                                }
                                            }

                                        }
//                                        System.out.println(value8);
                                        break;

                                    case NUMERIC:
                                        value8 = " " + cell8.getNumericCellValue();
                                        break;
                                }
                                Cell cell9 = row.getCell(l, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell9.getCellType()) {
                                    case STRING:
                                        value9 = cell9.getRichStringCellValue().getString();
                                        break;

                                    case NUMERIC:
                                        value9 = " " + cell9.getNumericCellValue();
                                        break;
                                }
                                Cell cell10 = row.getCell(m, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                switch (cell10.getCellType()) {
                                    case STRING:
                                        value10 = cell10.getRichStringCellValue().getString();
                                        break;

                                    case NUMERIC:
                                        value10 = " " + cell10.getNumericCellValue();
                                        break;
                                }

            %>
            <tr><td><%=value1%></td><td><%=value2%></td><td><%=value3%></td><td><%=value4%></td><td><%=value5%></td><td><%=value6%></td><td><%=value7%></td>
                <td><%=value8%></td><td><%=value9%></td><td><%=value10%></td></tr>
            <%

                    i++;
                }
        System.out.println(rowCountList);

                            }
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                    }
                %>
        </table>

    </body>
</html>

value8 基础我需要提取某些行并显示,我无法跟踪行号,因为我在 Cell cellx = rowx.getCell(h, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 行上获得了 NPE;

请指教

【问题讨论】:

    标签: apache-poi


    【解决方案1】:

    我通过两个标志来实现对行的跟踪,这些标志划分了我需要提取的行数据的开始和结束,使用这些行号我再次遍历工作表并显示提取的数据

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多