package util;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelOperateUtil {
/**
* Excel后缀为.xlsx的版本调用此方法
*
* @param tempFile
* @return
*/
public static List<Map<String, Object>> readDataByExcelXLSX(File tempFile, boolean flag) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();// 号码集合
try {
// 创建对Excel工作簿文件的引用
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(tempFile));
XSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
Map<String, Object> resultMap = null;
String[] keys = new String[]{"column1","column2"};
Pattern pattern = Pattern.compile("[0-9]*");
for (int i = 1; i < rows; i++) {
resultMap = new HashMap<String, Object>();
Row row = sheet.getRow(i);
if (row != null) {
int cells = row.getPhysicalNumberOfCells()+1;
for (int j = 0; j < cells; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
if(flag){
if(HSSFCell.CELL_TYPE_STRING == cell.getCellType() && pattern.matcher(cell.getStringCellValue()).matches()){
resultMap.put(keys[0], cell.getStringCellValue());
}else if(HSSFCell.CELL_TYPE_STRING == cell.getCellType()){
resultMap.put(keys[1], cell.getStringCellValue());
}
}else{
if(HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)){
resultMap.put(keys[0], new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()));
}else if(HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()){
resultMap.put(keys[1], cell.getNumericCellValue());
}
}
}
}
resultList.add(resultMap);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return resultList;
}
/**
* 读取Excel后缀为.xls之下的版本调用此方法
*
* @param tempFile
* @return
*/
// public static List<String> readDataByExcelXLS(File tempFile) {
// List<String> phoneList = new ArrayList<String>();// 号码集合
// try {
// // 创建对Excel工作簿文件的引用
// HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(tempFile));
// HSSFSheet sheet = workbook.getSheetAt(0);
// int rows = sheet.getPhysicalNumberOfRows();
// for (int i = 0; i < rows; i++) {
// HSSFRow row = sheet.getRow(i);
// if (row != null) {
// int cells = row.getPhysicalNumberOfCells()+1;
//
// for (int j = 0; j < cells; j++) {
// String value = "";
// HSSFCell cell = row.getCell(j);
// if (cell != null) {
// switch (cell.getCellType()) {
// case HSSFCell.CELL_TYPE_NUMERIC:
// value = new DecimalFormat("0").format(cell.getNumericCellValue());
// phoneList.add(value);
// break;
// default:
// break;
// }
// }
// }
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return phoneList;
// }
// public static void main(String[] args) {
//
File file = new File("C:\\Users\\dzd-000\\Desktop\\111.xls");
List<String> readDataByExcelXLS = readDataByExcelXLS(file);
//
// File file1 = new File("C:\\Users\\dzd-000\\Desktop\\000.xlsx");
// List<Map<String, Object>> readDataByExcelXLSX = readDataByExcelXLSX(file1);
// System.out.println(readDataByExcelXLSX.size());
// }
}
java Excel读取
最新推荐文章于 2024-04-17 11:25:48 发布