Java 讀取Excel表格式資料日期類型轉換__Java

來源:互聯網
上載者:User

Java 讀取Excel表格日期類型資料的時候,讀出來的是這樣的  13-十二月-2017,而Excel中輸入的是 2017/12/13 或 2017-12-13

還有Excel中輸入的是整型 5,java 讀取出來的是5.0

這可怎麼整。

解決方案:

日期轉換

import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
 
Workbook workBook = (Workbook)ModelUtils.getRequestContext().getActionContext().getParameter("excel");
Sheet sheet = workBook.getSheetAt(0);
int fr = sheet.getPhysicalNumberOfRows() - 4;
for(int i=0;i<fr;i++){
    int r = 4+i;
    org.apache.poi.ss.usermodel.Row rowDetail = sheet.getRow(r);
    String guarantee_date = "";
         Cell dateCell = rowDetail.getCell(2);

         //判斷是否為日期類型
         if(0==dateCell.getCellType()){  
        if(DateUtil.isCellDateFormatted(dateCell)){
        //用於轉化為日期格式
        Date d = dateCell.getDateCellValue();
        DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        guarantee_date = formater.format(d);
            }
         }
         System.out.println(guarantee_date);


         //------------整型數量格式處理--------------
         String countStr = rowDetail.getCell(3).toString();
         Integer count = 0;
         System.out.println("入庫數量:"+countStr);
         if(countStr.indexOf(".")>=0){
        System.out.println(". 位元: "+ countStr.indexOf("."));
        countStr = countStr.substring(0, countStr.indexOf("."));
        count = Integer.valueOf(countStr);
         }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.