1、添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>
2、支持只读一个sheet的工具类
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ExcelReader {
public static List<List<String>> readExcel(String path) {
String fileType = path.substring(path.lastIndexOf(".") + 1);
List<List<String>> lists = new ArrayList<List<String>>();
InputStream is = null;
try {
is = new FileInputStream(path);
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(is);
} else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(is);
} else {
return null;
}
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
ArrayList<String> list = new ArrayList<String>();
int flag = 0;
System.out.println(">>>>>>>>>>>"+row.getLastCellNum());
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if(cell == null){
list.add("");
}else if(cell.getCellType() ==3){
list.add("");
}else if(cell.getCellType() ==0){
list.add(handleNumber(String.valueOf(cell.getNumericCellValue())));
flag = 1;
}else if(cell.getCellType() ==1){
cell.setCellType(Cell.CELL_TYPE_STRING);
list.add(cell.getStringCellValue());
flag = 1;
}else {
list.add("");
}
}
if(list.size() != 0 && flag == 1){
lists.add(list);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lists;
}
public static String handleNumber(String number){
if(number.contains(".") && number.substring(number.indexOf(".")+1,number.length()).length() == 16){
if(number.substring(number.length()-1,number.length()).equals("1")){
number = number.substring(0,number.length()-1);
}else if(number.substring(number.length()-1,number.length()).equals("9")){
Double tem = Double.valueOf(number) + 0.0000000000000002;
number = tem.toString();
}
}
if(!number.contains(".")){
return number;
}else if(number.lastIndexOf(".") == number.length()-1){
number = number.substring(0,number.length()-1);
return number;
}else if(number.substring(number.length()-1,number.length()).equals("0")){
number = number.substring(0,number.length()-1);
number = handleNumber(number);
}else {
return number;
}
return number;
}
public static Workbook creatExcel(List<List<String>> lists, String[] titles, String name) throws IOException {
System.out.println(lists);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(name);
for(int i=0;i<titles.length;i++){
sheet.setColumnWidth((short) i, (short) (35.7 * 150));
}
Row row = sheet.createRow((short) 0);
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
Font f = wb.createFont();
Font f2 = wb.createFont();
f.setFontHeightInPoints((short) 10);
f.setColor(IndexedColors.BLACK.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
f2.setFontHeightInPoints((short) 10);
f2.setColor(IndexedColors.BLACK.getIndex());
cs.setFont(f);
cs.setBorderLeft(CellStyle.BORDER_THIN);
cs.setBorderRight(CellStyle.BORDER_THIN);
cs.setBorderTop(CellStyle.BORDER_THIN);
cs.setBorderBottom(CellStyle.BORDER_THIN);
cs.setAlignment(CellStyle.ALIGN_CENTER);
cs2.setFont(f2);
cs2.setBorderLeft(CellStyle.BORDER_THIN);
cs2.setBorderRight(CellStyle.BORDER_THIN);
cs2.setBorderTop(CellStyle.BORDER_THIN);
cs2.setBorderBottom(CellStyle.BORDER_THIN);
cs2.setAlignment(CellStyle.ALIGN_CENTER);
for(int i=0;i<titles.length;i++){
Cell cell = row.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(cs);
}
if(lists == null || lists.size() == 0){
return wb;
}
for (short i = 1; i <= lists.size(); i++) {
Row row1 = sheet.createRow((short)i);
for(short j=0;j<titles.length;j++){
Cell cell = row1.createCell(j);
cell.setCellValue(lists.get(i-1).get(j));
cell.setCellStyle(cs2);
}
}
return wb;
}
public static void main(String[] args) {
double ii = (double)10/(double)3;
System.out.println(">>>>ii>>>>"+ii);
String path = "D:/111.xls";
List<List<String>> lists = readExcel(path);
for (List<String> list : lists) {
System.out.println(">>"+list);
System.out.println("--------------------------");
for (String strs : list) {
System.out.println(strs);
}
}
}
}
3、支持多个sheet数据读取工具类
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ExcelReaderPlus {
private static HSSFWorkbook wb;
private static HSSFSheet sheet;
private static HSSFRow row;
private static XSSFWorkbook wbx;
private static XSSFSheet sheetx;
private static XSSFRow rowx;
public ExcelData readExcel(String filePath) {
InputStream is = null;
File file = new File(filePath);
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ExcelData excelData = new ExcelData();
try {
if(filePath.substring(filePath.length()-5,filePath.length()).equals(".xlsx")){
wbx = new XSSFWorkbook(is);
return readExcelx(wbx,file.getName());
}
wb = new HSSFWorkbook(is);
} catch (IOException e) {
e.printStackTrace();
}
Integer sheetNum = wb.getNumberOfSheets();
excelData.setSheetSum(sheetNum);
excelData.setFileName(file.getName());
List<ExcelSheetData> sheetDatas = new ArrayList<>();
for (int i = 0; i < sheetNum; i++) {
ExcelSheetData sheetData = new ExcelSheetData();
sheet = wb.getSheetAt(i);
sheetData.setLineSum(sheet.getPhysicalNumberOfRows());
sheetData.setSheetName(sheet.getSheetName());
List<ExcelLineData> lineDatas = readExcelContentBySheet(sheet);
sheetData.setLineData(lineDatas);
sheetDatas.add(sheetData);
}
excelData.setSheetData(sheetDatas);
return excelData;
}
private ExcelData readExcelx(XSSFWorkbook wbx,String fileName) {
ExcelData excelData = new ExcelData();
Integer sheetNum = wbx.getNumberOfSheets();
excelData.setSheetSum(sheetNum);
excelData.setFileName(fileName);
List<ExcelSheetData> sheetDatas = new ArrayList<>();
for (int i = 0; i < sheetNum; i++) {
ExcelSheetData sheetData = new ExcelSheetData();
sheetx = wbx.getSheetAt(i);
sheetData.setSheetName(sheetx.getSheetName());
sheetData.setLineSum(sheetx.getPhysicalNumberOfRows());
List<ExcelLineData> lineDatas = readExcelContentBySheetx(sheetx);
sheetData.setLineData(lineDatas);
sheetDatas.add(sheetData);
}
excelData.setSheetData(sheetDatas);
return excelData;
}
private List<ExcelLineData> readExcelContentBySheet(HSSFSheet sheet) {
List<ExcelLineData> lineDatas = new ArrayList<>();
int rowNum = sheet.getLastRowNum();
for (int i = 0; i <= rowNum; i++) {
int j = 0;
row = sheet.getRow(i);
if (Objects.isNull(row)) {
continue;
}
int colNum = row.getLastCellNum();
ExcelLineData lineData = new ExcelLineData();
List<String> colData = new ArrayList<>();
lineData.setColSum(colNum);
while (j < colNum) {
String value = getCellValue(row.getCell(j)).trim();
colData.add(value);
j++;
}
lineData.setColData(colData);
lineDatas.add(lineData);
}
return lineDatas;
}
private List<ExcelLineData> readExcelContentBySheetx(XSSFSheet sheetx) {
List<ExcelLineData> lineDatas = new ArrayList<>();
int rowNum = sheetx.getLastRowNum();
for (int i = 0; i <= rowNum; i++) {
int j = 0;
rowx = sheetx.getRow(i);
if (Objects.isNull(rowx)) {
continue;
}
int colNum = rowx.getLastCellNum();
ExcelLineData lineData = new ExcelLineData();
List<String> colData = new ArrayList<>();
lineData.setColSum(colNum);
while (j < colNum) {
String value = getCellValuex(rowx.getCell(j)).trim();
colData.add(value);
j++;
}
lineData.setColData(colData);
lineDatas.add(lineData);
}
return lineDatas;
}
private String getCellValue(HSSFCell cell) {
if (Objects.isNull(cell)) {
return "";
}
String value = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
break;
} else {
value = new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
value = cell.getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "非法字符";
break;
default:
value = "未知类型";
break;
}
return value;
}
private String getCellValuex(XSSFCell cellx) {
if (Objects.isNull(cellx)) {
return "";
}
String value = "";
switch (cellx.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cellx)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
value = sdf.format(HSSFDateUtil.getJavaDate(cellx.getNumericCellValue())).toString();
break;
} else {
value = new DecimalFormat("0").format(cellx.getNumericCellValue());
}
break;
case XSSFCell.CELL_TYPE_STRING:
value = cellx.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = cellx.getBooleanCellValue() + "";
break;
case XSSFCell.CELL_TYPE_FORMULA:
value = cellx.getCellFormula() + "";
break;
case XSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case XSSFCell.CELL_TYPE_ERROR:
value = "非法字符";
break;
default:
value = "未知类型";
break;
}
return value;
}
public static void main(String[] args) {
ExcelReaderPlus excelReader = new ExcelReaderPlus();
ExcelData excelData = excelReader.readExcel("D:\\111.xls");
System.out.println(excelData.toString());
for(ExcelSheetData excelSheetData:excelData.getSheetData()){
System.out.println("**********************"+excelSheetData.getSheetName());
for(ExcelLineData excelLineData :excelSheetData.getLineData()){
System.out.println("----------------"+excelLineData.getColSum());
System.out.println(">>>>>>>"+excelLineData.getColData());
}
}
}
public class ExcelData {
private int sheetSum;
private String fileName;
private List<ExcelSheetData> sheetData;
public int getSheetSum() {
return sheetSum;
}
public void setSheetSum(int sheetSum) {
this.sheetSum = sheetSum;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List<ExcelSheetData> getSheetData() {
return sheetData;
}
public void setSheetData(List<ExcelSheetData> sheetData) {
this.sheetData = sheetData;
}
}
public class ExcelSheetData {
private String sheetName;
private int lineSum;
private List<ExcelLineData> lineData;
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public int getLineSum() {
return lineSum;
}
public void setLineSum(int lineSum) {
this.lineSum = lineSum;
}
public List<ExcelLineData> getLineData() {
return lineData;
}
public void setLineData(List<ExcelLineData> lineData) {
this.lineData = lineData;
}
}
public class ExcelLineData {
private int lineNumber;
private int colSum;
private List<String> colData;
public int getLineNumber() {
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
public int getColSum() {
return colSum;
}
public void setColSum(int colSum) {
this.colSum = colSum;
}
public List<String> getColData() {
return colData;
}
public void setColData(List<String> colData) {
this.colData = colData;
}
}
}