当前位置:  开发笔记 > 编程语言 > 正文

按列名apache poi excel获取数据

如何解决《按列名apachepoiexcel获取数据》经验,为你挑选了2个好方法。

我正在使用apache POI进行excel导入和解析.我必须通过传递列名来获取数据.

这是我的代码

JSONObject jo = new JSONObject();
        JSONArray dataCollection = new JSONArray();
        JSONObject data = null;
        try {
            String tempCampaignFilesPath = getSessionData("userPath") + System.getProperty("file.separator") + "tempCampaignFiles";
            File someFile = new File(tempCampaignFilesPath, fileName);

            /* read from this file */

            FileInputStream fileInputStream = new FileInputStream(someFile);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheet(sheetName);
            int rowNum = sheet.getLastRowNum() + 1;
            int colNum = sheet.getRow(0).getLastCellNum();
            Row row = null;
            Cell cell = null;

            for (int i = 1; i < rowNum; i++) {
                row = sheet.getRow(i);
                data = new JSONObject();

                for (int j = 0; j < colNum; j++) {
                    cell = row.getCell(j);
                    data.put(columnList.get(j), cellToString(cell));
                }
                dataCollection.put(data);
            }
            fileInputStream.close();
            // someFile.delete();
            jo.put("tableData", dataCollection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo;

列索引有一个规定,但我怎么能按列名做.

请帮我.



1> dlopatin..:

您必须将列名转换为索引:

int colIdx = CellReference.convertColStringToIndex(letter);
CellUtil.getCell(row, colIdx)

或者如果您需要将列索引转换为字符串:

String colName = CellReference.convertNumToColString(colIdx)


仅当您给单元格引用(例如“ B12”)作为参数而不是列名时,它才有效。它没有回答问题。

2> Pawan Pandey..:

请在下面找到代码的另一种解决方法.请参阅代码中的注释,以便更清楚我所做的事情.

        JSONObject jo = new JSONObject();
        JSONArray dataCollection = new JSONArray();
        JSONObject data = null;
        try {
            String tempCampaignFilesPath = getSessionData("userPath") + System.getProperty("file.separator") + "tempCampaignFiles";
            File someFile = new File(tempCampaignFilesPath, fileName);

            /* read from this file */

            FileInputStream fileInputStream = new FileInputStream(someFile);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheet(sheetName);

            int rowNum = sheet.getLastRowNum() + 1;
            int colNum = sheet.getRow(0).getLastCellNum();
            Row row = null;
            Cell cell = null;

            /* first row data for column names and index */

            Map colMapByName = new HashMap();
            if (sheet.getRow(0).cellIterator().hasNext()) {
                for (int j = 0; j < colNum; j++) {
                    colMapByName.put(cellToString(sheet.getRow(0).getCell(j)), j);
                }
            }
            System.out.println(colMapByName);//shows the indexes of columns populated by traversing first row
            /* first row data */

            for (int i = 1; i < rowNum; i++) {
                row = sheet.getRow(i);
                data = new JSONObject();
                //colMap consists the columnnames and alias name for it
                for (Entry colData : colMap.entrySet()) {
                    cell = row.getCell(colMapByName.get(colData.getValue()));//gives the index of column from  colMapByName Map by passing column name
                    data.put(colData.getKey(), cellToString(cell));//now the data passed to the alias for the column tobe used in application
                }
                dataCollection.put(data);
            }
            fileInputStream.close();
            someFile.delete();
            jo.put("tableData", dataCollection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo;

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有