Native read POI introduced EXCEL

Recent projects have long time useless conflict so they used this 

Who knows what the future will not be used to directly throw it down the project in tune method on OK.

Record it. . . . I do not want to write something like this 


import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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;

 

public class ReadExcelUtil {
     private POIFSFileSystem fs;
        private HSSFWorkbook wb;
        private HSSFSheet sheet;
        private HSSFRow row;

        / **
         * read the contents of the header Excel spreadsheet
         * @param IS
         * @return array String header contents
         * /
        public String [] readExcelTitle (the InputStream IS) {
            the try {
                FS = new new POIFSFileSystem (IS);
                WB = new new HSSFWorkbook (FS);
            } the catch (IOException E) {
                e.printStackTrace ();
            }
            Sheet wb.getSheetAt = (0);
            // get the first row line
            row sheet.getRow = (0);
            // total number of columns of the title
            row.getPhysicalNumberOfCells colNum = int ();
            String [] = new new title String [colNum];
            for (int I = 0; I <colNum; I ++) {
                title[i] = getCellFormatValue(row.getCell((short) i));
            }
            return title;
        }

        / **
         * reads the content data Excel
         * @param IS
         the Map Object * @return Map data containing cell contents
         * /
        public the Map <Integer, String> readExcelContent (the InputStream IS) {
            the Map <Integer, String> the HashMap new new Content = <Integer, String> ();
            String STR = "";
            String strCell = "";
            the try {
                FS = new new POIFSFileSystem (IS);
                WB = new new HSSFWorkbook (FS);
            } the catch (IOException E) {
                e.printStackTrace () ;
            }
            Sheet wb.getSheetAt = (0);
            // get the number of rows
            int rowNum = sheet.getLastRowNum ();
            // Since the 0th row and the first row has been incorporated herein index beginning from 2
            Row = sheet.getRow (2);
            int colNum row.getPhysicalNumberOfCells = ();
            should begin // text content from a second row, first behavior header heading
            for (int I = 2; I <= rowNum; I ++) {
                Row = sheet.getRow (I);
                int J = 0;
                the while (J <colNum) {
                    strCell = getStringCellValue (row.getCell (J ));
                    STR = strCell + + "-";
                   // row.getCell STR + = ((Short) J) + "^";
                    J ++;
                }
                content.put (I, STR);
                STR = "";
            }
            return content;
        }

        / **
         * cell data acquiring content data of type string
         *
         * @param Cell Excel cell
         * @return String data content of the cell
         * /
        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 = ((int) 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;
        }

        / **
         * acquiring content data cell date values
         *
         * @param Cell
         * Excel cell
         * @return String data content of the cell
         * /
        Private String getDateCellValue (HSSFCell Cell) {
            String Result = "";
            the try {
                int = cell.getCellType celltype ();
                IF (celltype == HSSFCell.CELL_TYPE_NUMERIC) {
                    a Date cell.getDateCellValue = DATE ();
                    Result = (Date.getYear () + 1900) + "-" + (date.getMonth () + . 1)
                            + "-" + date.getDate ();
                } the 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("日期格式不正确!");
                e.printStackTrace();
            }
            return result;
        }

        / **
         * Set the type of data in accordance with HSSFCell
         * @param Cell
         * @return
         * /
        Private String getCellFormatValue (HSSFCell Cell) {
            String CellValue = "";
            IF (! Cell = null) {
                // this Cell is determined the Type
                Switch (Cell .getCellType ()) {
                    // if the current Cell Type to the NUMERIC
                    Case HSSFCell.CELL_TYPE_NUMERIC:
                    Case HSSFCell.CELL_TYPE_FORMULA: {
                        // determines whether the current cell is a Date
                        IF (HSSFDateUtil.isCellDateFormatted (cell)) {
                            a Date = DATE cell. getDateCellValue ();
                            = New new SDF the SimpleDateFormat the SimpleDateFormat ( "the MM-dd-YYYY");
                            CellValue = sdf.format (DATE);
                        }
                        // if the number is a pure
                        the else {
                            // get the current value Cell
                            cellvalue = String.valueOf (cell.getNumericCellValue ( ));
                        }
                        BREAK;
                    }
                    // if the current Cell Type is strin
                    Case HSSFCell.CELL_TYPE_STRING:
                        // get the current Cell string
                        . cellvalue = cell.getRichStringCellValue () getString ( );
                        break;
                    // 默认的Cell值
                    default:
                        cellvalue = " ";
                }
            } else {
                cellvalue = "";
            }
            return cellvalue;

        }

        static void main public (String [] args) {
            the try {
                // the read test Excel spreadsheet title
                IS = the InputStream new new the FileInputStream ( "D: \\ Test2.xls");
                ReadExcelUtil excelReader new new ReadExcelUtil = ();
                String [] = excelReader.readExcelTitle title (iS);
                System.out.println ( "headline Excel table:");
                for (String S: title) {
                    of System.out.print (S + "");
                }
                the System.out. println ();

                // to read Excel spreadsheet content test
                InputStream IS2 = new new FileInputStream ( "d: \\ Test2.xls");
                the Map <Integer, String> = excelReader.readExcelContent the Map (IS2);
                System.out.println ( "get Excel contents of the form: ");
                // here since xls merged cells require special handling index
                for (int I = 2; I <= map.size () +. 1; I ++) {
                    System.out.println (Map. GET (I));
                }

            The catch} (a FileNotFoundException E) {
                System.out.println ( "file specified path was not found!");
                E.printStackTrace ();
            }
        }

}
 
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10935211.html