Read excel file content using POI

In the project, the content of the excel file is required to be read and converted into xml format. Commonly read excel documents generally use POI and JExcelAPI these two tools. Here we introduce the use of POI to read excel documents.

 

package edu.sjtu.erplab.poi;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

 

/**

 * Function class for manipulating Excel tables

 */

public class ExcelReader {

    private POIFSFileSystem fs;

    private HSSFWorkbook wb;

    private HSSFSheet sheet;

    private HSSFRow row;

 

    /**

     * Read the content of the Excel table header

     * @param InputStream

     * @return String array of header contents

     */

    public String[] readExcelTitle(InputStream is) {

        try {

            fs = new POIFSFileSystem(is);

            wb = new HSSFWorkbook(fs);

        } catch (IOException e) {

            e.printStackTrace ();

        }

        sheet = wb.getSheetAt(0);

        row = sheet.getRow(0);

        // total number of header columns

        int colNum = row.getPhysicalNumberOfCells();

        System.out.println("colNum:" + colNum);

        String[] title = new String[colNum];

        for (int i = 0; i < colNum; i++) {

            //title[i] = getStringCellValue(row.getCell((short) i));

            title[i] = getCellFormatValue(row.getCell((short) i));

        }

        return title;

    }

 

    /**

     * Read Excel data content

     * @param InputStream

     * @return Map Map object containing cell data content

     */

    public Map<Integer, String> readExcelContent(InputStream is) {

        Map<Integer, String> content = new HashMap<Integer, String>();

        String str = "";

        try {

            fs = new POIFSFileSystem(is);

            wb = new HSSFWorkbook(fs);

        } catch (IOException e) {

            e.printStackTrace ();

        }

        sheet = wb.getSheetAt(0);

        // get the total number of rows

        int rowNum = sheet.getLastRowNum();

        row = sheet.getRow(0);

        int colNum = row.getPhysicalNumberOfCells();

        // The body content should start from the second line, the first line is the title of the header

        for (int i = 1; i <= rowNum; i++) {

            row = sheet.getRow(i);

            int j = 0;

            while (j < colNum) {

                // The data content of each cell is separated by "-", and the replace() method of String class is used to restore the data when needed later

                // You can also set the data of each cell to the properties of a javabean, in this case you need to create a new javabean

                // str += getStringCellValue(row.getCell((short) j)).trim() +

                // "-";

                str += getCellFormatValue(row.getCell((short) j)).trim() + "    ";

                j++;

            }

            content.put(i, str);

            str = "";

        }

        return content;

    }

 

    /**

     * Get the data of the cell data content as string type

     * 

     * @param cell Excel cell

     * @return String cell data content

     */

    private String getStringCellValue(HSSFCell cell) {

        String strCell = "";

        switch (cell.getCellType()) {

        case HSSFCell.CELL_TYPE_STRING:

            strCell = cell.getStringCellValue();

            break;

        case HSSFCell.CELL_TYPE_NUMERIC:

            strCell = String.valueOf(cell.getNumericCellValue());

            break;

        case HSSFCell.CELL_TYPE_BOOLEAN:

            strCell = String.valueOf(cell.getBooleanCellValue());

            break;

        case HSSFCell.CELL_TYPE_BLANK:

            strCell = "";

            break;

        default:

            strCell = "";

            break;

        }

        if (strCell.equals("") || strCell == null) {

            return "";

        }

        if (cell == null) {

            return "";

        }

        return strCell;

    }

 

    /**

     * Get the data of the cell data content as date type

     * 

     * @param cell

     * Excel cell

     * @return String cell data content

     */

    private String getDateCellValue(HSSFCell cell) {

        String result = "";

        try {

            int cellType = cell.getCellType ();

            if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {

                Date date = cell.getDateCellValue();

                result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)

                        + "-" + date.getDate();

            } else if (cellType == HSSFCell.CELL_TYPE_STRING) {

                String date = getStringCellValue(cell);

                result = date.replaceAll("[年月]", "-").replace("日", "").trim();

            } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {

                result = "";

            }

        } catch (Exception e) {

            System.out.println("Date format is incorrect!");

            e.printStackTrace ();

        }

        return result;

    }

 

    /**

     * Set data according to HSSFCell type

     * @param cell

     * @return

     */

    private String getCellFormatValue(HSSFCell cell) {

        String cellvalue = "";

        if (cell != null) {

            // Determine the Type of the current Cell

            switch (cell.getCellType()) {

            // If the Type of the current Cell is NUMERIC

            case HSSFCell.CELL_TYPE_NUMERIC:

            case HSSFCell.CELL_TYPE_FORMULA: {

                // Determine whether the current cell is a Date

                if (HSSFDateUtil.isCellDateFormatted(cell)) {

                    // If it is a Date type, convert it to Data format

                    

                    //Method 1: The data format like this is with hours, minutes and seconds: 2011-10-12 0:00:00

                    //cellvalue = cell.getDateCellValue().toLocaleString();

                    

                    //Method 2: This data format is without hours, minutes and seconds: 2011-10-12

                    Date date = cell.getDateCellValue();

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    cellvalue = sdf.format(date);

                    

                }

                // if it's a pure number

                else {

                    // Get the value of the current Cell

                    cellvalue = String.valueOf(cell.getNumericCellValue());

                }

                break;

            }

            // If the Type of the current Cell is STRIN

            case HSSFCell.CELL_TYPE_STRING:

                // Get the current Cell string

                cellvalue = cell.getRichStringCellValue().getString();

                break;

            // Default Cell value

            default:

                cellvalue = " ";

            }

        } else {

            cellvalue = "";

        }

        return cellvalue;

 

    }

 

    public static void main(String[] args) {

        try {

            // Test for reading the title of the Excel table

            InputStream is = new FileInputStream("d:\\test2.xls");

            ExcelReader excelReader = new ExcelReader();

            String[] title = excelReader.readExcelTitle(is);

            System.out.println("Get the title of the Excel table:");

            for (String s : title) {

                System.out.print(s + " ");

            }

 

            // Test for reading Excel table content

            InputStream is2 = new FileInputStream("d:\\test2.xls");

            Map<Integer, String> map = excelReader.readExcelContent(is2);

            System.out.println("Get the content of the Excel table:");

            for (int i = 1; i <= map.size(); i++) {

                System.out.println(map.get(i));

            }

 

        } catch (FileNotFoundException e) {

            System.out.println("The file at the specified path was not found!");

            e.printStackTrace ();

        }

    }

}

 

 

Because the content in excel cells often has a certain format, such as date type, number type, and string type, it is necessary to judge the format when reading, otherwise an error will occur. It is common that the date cannot be read normally. There is a method in the code instance:

getCellFormatValue(HSSFCell cell)

Passing an excel cell into this method will recognize the cell format and convert it to the correct format.

 

ps:2012-2-23

There is a piece of code in the code example:

int colNum = row.getPhysicalNumberOfCells();

Among them, HSSFRow.getPhysicalNumberOfCells(); This method is used to obtain the number of cells in a row. The official API of POI gives an explanation of the getPhysicalNumberOfCells method

getPhysicalNumberOfCells

public int getPhysicalNumberOfCells()
gets the number of defined cells (NOT number of cells in the actual row!).  That is to say if only columns 0,4,5 have values then there would be 3.
Specified by:
getPhysicalNumberOfCells  in interface  Row
Returns:
int representing the number of defined cells in the row.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326951796&siteId=291194637