如何在Excel文件中的Java空白单元格处理 [英] How deal with blank cells in excel files java

查看:324
本文介绍了如何在Excel文件中的Java空白单元格处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个计划,使我从Excel文件读取数据,并将其存储在表中。我已经使用Apache POI制作的节目和作品的罚款。但是,当文件有空白单元格作为一个在这里我有一些问题。该计划跳过空白,并读取下一个数据。谁能帮助我,我会怎么做呢?我知道有几个职位这个问题,但我还没有找到有用的东西给我。

在code从Excel文件读取数据是下面。正如你可以看到我有3个类型的数据。我怎么会给予空白单元格的选项?

  //创建一个ArrayList存储从Excel工作表中读取数据。
        清单sheetData =新的ArrayList();
        FIS的FileInputStream = NULL;
        尝试{
            //创建一个FileInputStream,将使用阅读
            // Excel文件。
            FIS =新的FileInputStream(strfullPath);
            //创建从文件系统的Excel工作簿
            HSSFWorkbook工作簿=新HSSFWorkbook(FIS);            //获取工作簿中的第一张。
            HSSFSheet片= workbook.getSheetAt(0);            //存储在一个ArrayList阅读,使我们可以打印的数据
            // excel的内容到控制台。
            迭代行= sheet.rowIterator();
            而(rows.hasNext()){
                HSSFRow行=(HSSFRow)rows.next();
                迭代器单元= row.cellIterator();                列表数据=新的ArrayList();
                而(cells.hasNext()){
                    HSSFCell细胞=(HSSFCell)cells.next();
                    data.add(细胞);
                }
                sheetData.add(数据);
            }        }赶上(IOException异常五){
            e.printStackTrace();
        } {最后
            如果(FIS!= NULL){
                fis.close();
            }
        }
showExcelData(sheetData);
}
私有静态无效showExcelData(名单sheetData){
        // LinkedHashMap的<字符串,字符串> tableFields =新LinkedHashMap的();
        的for(int i = 0; I< sheetData.size();我++){
            列表列表=(列表)sheetData.get(I)
            对于(INT J = 0; J<则为list.size(); J ++){
                细胞;细胞=(Cell)的list.get(J);
                如果(cell.getCellType()== Cell.CELL_TYPE_NUMERIC){
                    System.out.print(cell.getNumericCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_STRING){
                    System.out.print(cell.getRichStringCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_BOOLEAN){
                    System.out.print(cell.getBooleanCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_BLANK){
                    System.out.print(cell.toString());
                }
                如果(J&下;则为list.size() - 1){
                    System.out.print(,);
                }
            }
            的System.out.println();
        }    }
}

此外,我读过关于 workbook.setMissingCellPolicy(HSSFRow.RETURN_NULL_AND_BLANK); 。难道这帮助我与我的问题?


解决方案

  INT maxNumOfCells = sheet.getRow(0).getLastCellNum(); //列的最大数量
            迭代行= sheet.rowIterator();
            而(rows.hasNext()){
                HSSFRow行=(HSSFRow)rows.next();
                迭代器单元= row.cellIterator();                列表数据=新的ArrayList();
                对于(INT cellCounter = 0
                        ; cellCounter< maxNumOfCells
                        ; cellCounter ++){//循环细胞                    HSSFCell细胞;                    如果(row.getCell(cellCounter)== NULL){
                        电池= row.createCell(cellCounter);
                    }其他{
                        电池= row.getCell(cellCounter);
                    }                    data.add(细胞);                }                sheetData.add(数据);

你的方法:

 公共静态无效showExcelData(名单sheetData){        // LinkedHashMap的<字符串,字符串> tableFields =新LinkedHashMap的();
        的for(int i = 0; I< sheetData.size();我++){
            列表列表=(列表)sheetData.get(I)
            对于(INT J = 0; J<则为list.size(); J ++){
                细胞;细胞=(Cell)的list.get(J);
                如果(cell.getCellType()== Cell.CELL_TYPE_NUMERIC){
                    System.out.print(cell.getNumericCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_STRING){
                    System.out.print(cell.getRichStringCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_BOOLEAN){
                    System.out.print(cell.getBooleanCellValue());
                }否则如果(cell.getCellType()== Cell.CELL_TYPE_BLANK){
                    System.out.print(这是BLANK);
                }
                如果(J&下;则为list.size() - 1){
                    System.out.print(,);
                }
            }
            的System.out.println();
        }    }

说明:

INT maxNumOfCells = sheet.getRow(0).getLastCellNum(); - 这条线将确保你能够得到的列数。在接下来的行中使用行的方法 .getLastCellNum()将导致意外的数字。例如您在您的S preadsheet第3行,该方法将返回2,因为接下来的值为null。搜索结果

 的for(int cellCounter = 0
                    ; cellCounter< maxNumOfCells
                    ; cellCounter ++){//循环细胞                HSSFCell细胞;                如果(row.getCell(cellCounter)== NULL){
                    电池= row.createCell(cellCounter);
                }其他{
                    电池= row.getCell(cellCounter);
                }                data.add(细胞);            }

通过细胞循环。从细胞0(基0)到最后单元号。如果发现细胞,基本上,它会创建一个空白值的单元格。最后,添加细胞到列表中。

I am making a program where I am reading data from excel files and store them in tables. I have made the program using Apache POI and works fine. But when files have blank cells as the one here I have some problems. The program skip the blanks and read the next data. Could anyone help me how I would do it? I know that there are several posts for this issue but I have not found something useful for me.

The code for reading the data from excel file is the below. As you can see I have 3 types of data. How i would give the option for BLANK CELL?

// Create an ArrayList to store the data read from excel sheet.
        List sheetData = new ArrayList();
        FileInputStream fis = null;
        try {
            // Create a FileInputStream that will be use to read the
            // excel file.
            fis = new FileInputStream(strfullPath);
            // Create an excel workbook from the file system
            HSSFWorkbook workbook = new HSSFWorkbook(fis);

            // Get the first sheet on the workbook.
            HSSFSheet sheet = workbook.getSheetAt(0);

            // store the data read on an ArrayList so that we can printed the
            // content of the excel to the console.
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }
                sheetData.add(data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
showExcelData(sheetData);
}
private static void showExcelData(List sheetData) {
        // LinkedHashMap<String, String> tableFields = new LinkedHashMap();
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(cell.getBooleanCellValue());
                } else if (cell.getCellType()== Cell.CELL_TYPE_BLANK ){
                    System.out.print(cell.toString());
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }

    }
}

Also I have read about workbook.setMissingCellPolicy(HSSFRow.RETURN_NULL_AND_BLANK);. Could this help me with my problem?

解决方案

            int maxNumOfCells = sheet.getRow(0).getLastCellNum(); // The the maximum number of columns
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                for( int cellCounter = 0
                        ; cellCounter < maxNumOfCells
                        ; cellCounter ++){ // Loop through cells

                    HSSFCell cell;

                    if( row.getCell(cellCounter ) == null ){
                        cell = row.createCell(cellCounter);
                    } else {
                        cell = row.getCell(cellCounter);
                    }

                    data.add(cell);

                }

                sheetData.add(data);

YOUR METHOD:

public static void showExcelData(List sheetData) {

        // LinkedHashMap<String, String> tableFields = new LinkedHashMap();
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(cell.getBooleanCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    System.out.print("THIS IS BLANK");
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }

    }

Explanation:

int maxNumOfCells = sheet.getRow(0).getLastCellNum(); - This line will make sure that you were able to get the number of columns. Using the Row's method .getLastCellNum() on the next rows will result to unexpected number. Example on your on row 3 of your spreadsheet, the method will return 2 since the next value is null.

            for( int cellCounter = 0
                    ; cellCounter < maxNumOfCells
                    ; cellCounter ++){ // Loop through cells

                HSSFCell cell;

                if( row.getCell(cellCounter ) == null ){
                    cell = row.createCell(cellCounter);
                } else {
                    cell = row.getCell(cellCounter);
                }

                data.add(cell);

            }

Looping through the cells. From cell 0 (Base 0) to the last cell number. If the cell was found null, basically, it would create the cell with a blank value. Lastly, adding the cell to your List.

这篇关于如何在Excel文件中的Java空白单元格处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆