public class ExcelImport {
/**
* 消息模板的表头
*/
public static String[] S = {\"用户ID\",\"消息标题\",\"消息内容\"};
public static List<String[]> getExcelData(MultipartFile file) {
List<String[]> list = new ArrayList<String[]>();
try {
POIFSFileSystem pois = new POIFSFileSystem(file.getInputStream());
// 新建WorkBook
HSSFWorkbook wb = new HSSFWorkbook(pois);
// 获取Sheet(工作薄)总个数
int sheetNumber = wb.getNumberOfSheets();
for (int i = 0; i < sheetNumber; i++) {
// 获取Sheet(工作薄)
HSSFSheet sheet = wb.getSheetAt(i);
// 开始行数
int firstRow = sheet.getFirstRowNum();
// 结束行数
int lastRow = sheet.getLastRowNum();
// 判断该Sheet(工作薄)是否为空
boolean isEmpty = false;
if (firstRow == lastRow) {
isEmpty = true;
}
if (!isEmpty) {
for (int j = firstRow + 1; j <= lastRow; j++) {
// 获取一行
HSSFRow row = sheet.getRow(j);
// 开始列数
int firstCell = row.getFirstCellNum();
// 结束列数
int lastCell = row.getLastCellNum();
// 判断该行是否为空
String[] value = new String[lastCell];
if (firstCell != lastCell) {
for (int k = firstCell; k < lastCell; k++) {
// 获取一个单元格
HSSFCell cell = row.getCell(k);
str = null;
// 获取单元格,值的类型
int cellType = cell.getCellType();
if (cellType == 0) {
java.text.DecimalFormat df = new DecimalFormat(\"########\");
str = df.format(cell.getNumericCellValue());
} else if (cellType == 1) {
str = cell.getStringCellValue();
} else if (cellType == 2) {
} else if (cellType == 4) {
str = cell.getBooleanCellValue();
}
value[k] = (String) str;
}
}
// 每一行循环完对应的就是一个用户故事的所有属性全部拿到
list.add(value);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
*
* @param file
* @param startRow 开始行 0开始
* @param endRow 结束行 0开始 不包含
* @param startCell 开始列 0开始
* @param endCell 结束列 0开始 不包含
* @param sheetAt 第一个sheet 0开始
* @return
*/
public static List<String[]> getExcelData(MultipartFile file, Integer startRow, Integer endRow, Integer startCell, Integer endCell, int sheetAt) {
List<String[]> list = new ArrayList<String[]>();
try {
Workbook wb = WorkbookFactory.create(file.getInputStream());//适配针对excel 2003 和 excel 2007
// 获取Sheet(工作薄)
Sheet sheet = wb.getSheetAt(sheetAt);
// 开始行数
int firstRow = sheet.getFirstRowNum();
if(startRow != null){
firstRow = startRow;
}
// 结束行数
int lastRow = sheet.getLastRowNum()+1;
if(endRow != null){
lastRow = endRow;
}
// 判断该Sheet(工作薄)是否为空
boolean isEmpty = false;
if (firstRow == lastRow) {
isEmpty = true;
}
if (!isEmpty) {
for (int j = firstRow; j < lastRow; j++) {
// 获取一行
// XSSFRow row = sheet.getRow(j);
Row row=sheet.getRow(j);
if(row == null) {
throw new Exception(\"第\" + (j + 1) + \"行为空\");
}
// 开始列数
int firstCell = row.getFirstCellNum();
if(startCell != null){
firstCell = startCell;
}
// 结束列数
int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();
if(endCell != null){
coloumNum = endCell;
}
int lastCell = row.getLastCellNum();
// 判断该行是否为空
String[] value = new String[coloumNum];
if (firstCell != lastCell) {
for (int k = firstCell; k < coloumNum; k++) {
// 获取一个单元格
Cell cell = row.getCell(k);
if(cell==null){
value[k] = \"\";
continue;
}
cell.setCellType(Cell.CELL_TYPE_STRING);
value[k] = cell.getStringCellValue();
}
}
// 每一行循环完对应的就是一个用户故事的所有属性全部拿到
list.add(value);
}
}
} catch (IOException e) {
try {
throw new Exception(\"非法模板{表头和模板表头不一致}\") ;
} catch (Exception e1) {
//e1.printStackTrace();
log.error(\"非法模板{表头和模板表头不一致\");
}
}catch (Exception e){
try {
throw new Exception(\"模板不符合要求,请下载模板\");
} catch (Exception e1) {
//e1.printStackTrace();
log.error(\"模板不符合要求,请下载模板\");
}
}
return list;
}
private static boolean isValid (Row row) {
// 开始列数
int firstCell = row.getFirstCellNum();
int lastCell = row.getLastCellNum();
// 判断该行是否为空
if (firstCell != lastCell) {
for (int i = firstCell; i < lastCell; i++) {
// 获取一个单元格
Cell cell = row.getCell(i);
if(cell == null){
return false;
}
str = null;
// 获取单元格,值的类型
int cellType = cell.getCellType();
if (cellType == 1) {
str = cell.getStringCellValue();
}
if(!StringUtils.equalsIgnoreCase( S[i], str.toString().trim())) {
return false;
}
}
return true;
} else {
return false;
}
}
} 继续阅读与本文标签相同的文章
上一篇 :
《算法技术手册》一1.3.1 贪心算法
下一篇 :
《算法技术手册》一1.3 高明做法
-
特斯拉自动驾驶系统涨价遭质疑 马斯克:我们不能一直亏钱
2026-05-14栏目: 教程
-
首个二类资源区平价光伏电站正式并网发电
2026-05-14栏目: 教程
-
AI+5G科技创新 视频行业呈现轻应用化趋势
2026-05-14栏目: 教程
-
1.98亿滴滴用户添加了紧急联系人 每天百万个订单行程分享给亲友
2026-05-14栏目: 教程
-
工程院院士刘韵洁:5G前景很大,但主要是行业应用
2026-05-14栏目: 教程
