poi解析excel,poiexcel

來源:互聯網
上載者:User

poi解析excel,poiexcel

一.遇見的問題:

  當儲存格設定為日期類型時,cell.getCellStyle().getDataFormat()返回的值都為176。

  poi jar包3.14以上不支援用cell.getCellType()判斷類型的方法。

  使用poi解析技術需要匯入poi以及poi-ooxml兩個jar包。

二.儲存格的類型cell.getCellType():

  HSSFCell.CELL_TYPE_NUMERIC:數字類型(包含日期)

  CELL_TYPE_STRING:字串

  CELL_TYPE_BOOLEAN:布爾類型

  CELL_TYPE_BLANK:沒有值

  CELL_TYPE_FORMULA:公式取值

三.儲存格為自訂類型的時候,cell.getCellStyle().getDataFormat()值:

  yyyy-MM-dd---->14

  yyyy年m月d日--->31

  yyyy年m月------>57

  m月d日  -------->58

  HH:mm--------->20

  h時mm分  ------>32

四.最終代碼:

package cn.trashman.excel;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;  public class PoiExcel {      public static void main(String[] args) {          poiExcel("D:/1.xlsx");      }      @SuppressWarnings("resource")    public static void poiExcel(String fileName){          boolean is2007 = true;    //判斷是否是excel2007格式          if(fileName.endsWith("xlsx"))              is2007 = false;        try {              InputStream input = new FileInputStream(fileName);  //建立輸入資料流              Workbook wb  = null;            //根據檔案格式(2003或者2007)來初始化              if(is2007)                  wb = new HSSFWorkbook(input);              else                  wb = new XSSFWorkbook(input);             Sheet sheet = wb.getSheetAt(0);            int lastRowNum = sheet.getLastRowNum();            for (int i = 0; i <= lastRowNum; i++) {                //具體到某一行                Row row = sheet.getRow(i);                //儲存格的數量                int lastCellNum = row.getLastCellNum();                for (int j = 0; j < lastCellNum; j++) {                    Cell cell = row.getCell(j);                    //根據cell中的類型來輸出資料                      switch (cell.getCellType()) {                      case HSSFCell.CELL_TYPE_NUMERIC:                        Object result = cell.getNumericCellValue();//                        // 判斷儲存格是否屬於日期格式  //                        if(HSSFDateUtil.isCellDateFormatted(cell)){//                            //java.util.Date類型  //                            result = cell.getDateCellValue();//                        }                        short format = cell.getCellStyle().getDataFormat();                        SimpleDateFormat sdf = null;                         if(format != 0){//                            format == 176時是當指定儲存格格式為日期是都是176                            if(format == 14 || format == 31 || format == 57 || format == 58||format == 176){                                  //日期                                  sdf = new SimpleDateFormat("yyyy-MM-dd");                              }else if (format == 20 || format == 32) {                                  //時間                                  sdf = new SimpleDateFormat("HH:mm");                              }                            double value = cell.getNumericCellValue();                              Date date = DateUtil.getJavaDate(value);                              result = sdf.format(date);                        }                        System.out.println(result);                          break;                      case HSSFCell.CELL_TYPE_STRING:                          System.out.println(cell.getStringCellValue());                          break;                      case HSSFCell.CELL_TYPE_BOOLEAN:                          System.out.println(cell.getBooleanCellValue());                          break;                      case HSSFCell.CELL_TYPE_FORMULA:                          System.out.println(cell.getCellFormula());                          break;                      case HSSFCell.CELL_TYPE_BLANK:                          System.out.println("為空白");                          break;                       default:                          System.out.println("unsuported sell type");                      break;                      }                  }                System.out.println();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.