java讀取Excel,實測無問題
先匯入包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
匯入兩個包後可能會出現有三個maven的jar包依賴報錯,此時需要記錄下報錯的包,然後再maven中央倉庫中搜索對應的三個包,將jar包下載到本地,可以用eclipse手動匯入到本地maven中,匯入方式 ,在eclipse左側,Inport >maven>Install or deploy an artifact to maven install >next >填上對應資訊,版本號,打包方式jar
package com.zhongan.xd.upreport.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.collect.Lists;
import com.zhongan.xd.upreport.domain.ExcelDataDO;
/**
* Description: Excel操作
*/
public class ExcelUtil {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
/**
* 判斷Excel的版本,獲取Workbook
*
* @param in
* @param filename
* @return
* @throws IOException
*/
public static Workbook getWorkbok(InputStream in, File file) throws IOException {
Workbook wb = null;
if (file.getName().endsWith(EXCEL_XLS)) { //Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}
/**
* 判斷檔案是否是excel
*
* @throws Exception
*/
public static void checkExcelVaild(File file) throws Exception {
if (!file.exists()) {
throw new Exception("檔案不存在");
}
if (!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))) {
throw new Exception("檔案不是Excel");
}
}
/**
* 讀取Excel測試,相容 Excel 2003/2007/2010
*
* @throws Exception
*/
public static List<ExcelDataDO> readexcel(File excelFilea) throws Exception {
List<ExcelDataDO> list = Lists.newArrayList();
try {
// 同時支援Excel 2003、2007
File excelFile = excelFilea; // 建立檔案物件
FileInputStream in = new FileInputStream(excelFile); // 檔案流
checkExcelVaild(excelFile);
Workbook workbook = getWorkbok(in, excelFile);
//Workbook workbook = WorkbookFactory.create(is); // 這種方式 Excel2003/2007/2010都是可以處理的
int sheetCount = workbook.getNumberOfSheets(); // Sheet的數量
/**
* 設定當前excel中sheet的下標:0開始
*/
Sheet sheet = workbook.getSheetAt(0); // 遍歷第一個Sheet
// Sheet sheet = workbook.getSheetAt(2); // 遍歷第三個Sheet
//獲取總行數
System.out.println("總行數=" + sheet.getLastRowNum());
// i為從第幾行開始讀取 0為文件第一行
for (int i = 3; i < sheet.getLastRowNum() - 1; i++) {
Row row = sheet.getRow(i);
// for (Row row : sheet) {
try {
// 跳過第一和第二行的目錄
//如果當前行沒有資料,繼續下一行
if (row.getCell(0).toString().equals("")) {
continue;
}
//獲取總列數(空格的不計算)
int columnTotalNum = row.getPhysicalNumberOfCells();
System.out.println("總列數:" + columnTotalNum);
System.out.println("最大列數:" + row.getLastCellNum());
//for迴圈的,不掃描空格的列
// for (Cell cell : row) {
// System.out.println(cell);
// }
int end = row.getLastCellNum();
for (int i1 = 0; i1 < end; i1++) {
//0為第一列
Cell cell = row.getCell(i1);
Object obj = getValue(cell);
System.out.println(i1 + "列的資料為" + obj);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// }
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static Object getValue(Cell cell) {
Object obj = null;
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case ERROR:
obj = cell.getErrorCellValue();
break;
case NUMERIC:
obj = String.valueOf(cell.getNumericCellValue());
// obj = (int) cell.getNumericCellValue();
break;
case STRING:
obj = cell.getStringCellValue();
break;
default:
break;
}
return obj;
}
public Object dosome(Cell cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 數字
System.out.print(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING: // 字串
System.out.print(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知型別 ");
break;
}
return null;
}
public static void main(String[] args) throws ParseException {
String date = "20180925";
String y = date.substring(0, 4);
String m = date.substring(4, 6);
String d = date.substring(6, 8);
System.out.println(y + "-" + m + "-" + d);
}
}