生活随笔
收集整理的這篇文章主要介紹了
java Poi导入exel表格的数据,入库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pom依賴 導入poi依賴
<dependency><groupId>org.apache.poi
</groupId><artifactId>poi
</artifactId><version>3.17
</version></dependency><dependency><groupId>org.apache.poi
</groupId><artifactId>poi-ooxml
</artifactId><version>3.17
</version></dependency>
工具類,里面也有main方法
注意:表格的列值,不同有的是整數,有的日期,有的是字符串‘
使用 row.getCell(0).setCellType(CellType.NUMERIC); 進行列格式設置
setCellType 自己點進去看類型把
package com
.sxjlrj
.common
.util
;import java
.io
.File
;
import java
.io
.FileInputStream
;
import java
.math
.BigDecimal
;
import java
.util
.*
;import com
.sxjlrj
.manager
.censusManager
.model
.DtStatisticBusiness
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFWorkbook
;
import org
.apache
.poi
.ss
.usermodel
.*
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFWorkbook
;public class ExcelUtil {public static void main(String
[] args
) {File file
= new File("C:\\Users\\gaoyuan\\Desktop\\2017年太原(1).xls");try {List
<DtStatisticBusiness> list
= importExcel(file
);} catch (Exception e
) {e
.printStackTrace();}}public static List
<DtStatisticBusiness> importExcel(File file
) throws Exception
{Workbook wb
= null
;String fileName
= file
.getName();Iterator
<Sheet> sheets
= null
;List
<DtStatisticBusiness> returnlist
= new ArrayList<DtStatisticBusiness>();try {if (fileName
.endsWith("xls")) {wb
= new HSSFWorkbook(new FileInputStream(file
));sheets
= wb
.iterator();} else if (fileName
.endsWith("xlsx")) {wb
= new XSSFWorkbook(new FileInputStream(file
));sheets
= wb
.iterator();}if (sheets
== null
) {throw new Exception("excel中不含有sheet工作表");}while (sheets
.hasNext()) {Sheet sheet
= sheets
.next();String sheetName
= sheet
.getSheetName();if(sheetName
.equals("2020")){returnlist
=getCellValue(sheet
);}}} catch (Exception ex
) {throw ex
;} finally {if (wb
!= null
) wb
.close();}return returnlist
;}private static List
<DtStatisticBusiness> getCellValue(Sheet sheet
) {List
<DtStatisticBusiness> list
= new ArrayList<DtStatisticBusiness>();for (int i
= (sheet
.getFirstRowNum()+1); i
< sheet
.getPhysicalNumberOfRows(); i
++) {Row row
= sheet
.getRow(i
);if (row
== null
) {continue;} else {if (row
.getLastCellNum() < 4) {continue;}if (row
.getCell(0).getCellTypeEnum().equals(CellType
.STRING
)){continue;}DtStatisticBusiness dtStatisticBusiness
= new DtStatisticBusiness();row
.getCell(0).setCellType(CellType
.NUMERIC
);Date dateCellValue
= row
.getCell(0).getDateCellValue();if (StringUtil
.isEmpty(dateCellValue
)||dateCellValue
.equals("總計")||dateCellValue
.equals("日期")) {continue;}dtStatisticBusiness
.setReportDate(dateCellValue
);row
.getCell(1).setCellType(CellType
.STRING
);String transportCell
= row
.getCell(1).getStringCellValue();if (StringUtil
.isEmpty(transportCell
)) {continue;}dtStatisticBusiness
.setTransportPre(new BigDecimal(transportCell
));dtStatisticBusiness
.setTransportRev(new BigDecimal(transportCell
));row
.getCell(2).setCellType(CellType
.STRING
);String passengerCell
= row
.getCell(2).getStringCellValue();if (StringUtil
.isEmpty(passengerCell
)) {continue;}BigDecimal passengerDivide
= new BigDecimal(passengerCell
).divide(new BigDecimal(10000));dtStatisticBusiness
.setPassengerPre(passengerDivide
);dtStatisticBusiness
.setPassengerRev(passengerDivide
);row
.getCell(3).setCellType(CellType
.STRING
);String cargoCell
= row
.getCell(3).getStringCellValue();if (StringUtil
.isEmpty(cargoCell
)) {continue;}BigDecimal cargoDivide
= new BigDecimal(cargoCell
).divide(new BigDecimal(1000));dtStatisticBusiness
.setCargoRev(cargoDivide
);dtStatisticBusiness
.setCargoPre(cargoDivide
);list
.add(dtStatisticBusiness
);}}return list
;}}
總結
以上是生活随笔為你收集整理的java Poi导入exel表格的数据,入库的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。