随笔 - 581  文章 - 0 评论 - 48 阅读 - 131万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

前提:需要引入POI的jar包。

 

public class TemplateFileSheetParseVO {

/**
* 表名称
*/
private String tableName;

/**
* 列名称
*/
private List<String> columnNames;
}
private TemplateFileSheetParseVO extractContent(File file, Integer sheetIndex) throws IOException {

Workbook wb = WorkbookFactory.create(new FileInputStream(file));
try {
Sheet sheet = wb.getSheetAt(sheetIndex);
String sheetName = sheet.getSheetName();
Row headerRow = sheet.getRow(0);
if(headerRow == null){
throw new BusinessException("第" + (sheetIndex + 1) + "个sheet,不存在表头");
}
int columnCount = sheet.getRow(0).getLastCellNum();
if(columnCount <= 0){
throw new BusinessException("表头不存在");
}
List<String> headers = new ArrayList<>();

for (int i = 0; i < columnCount; i++) {
Cell cell = headerRow.getCell(i);
headers.add(cell.getStringCellValue());
}

TemplateFileSheetParseVO vo = new TemplateFileSheetParseVO();
vo.setTableName(sheetName);
vo.setColumnNames(headers);
return vo;
} catch (BusinessException e) {
throw e;
}catch (Exception e) {
log.error("", e);
throw new BusinessException("-1", e.getMessage());
} finally {
try {
if (wb != null) {
wb.close();
}
} catch (IOException e) {
log.info(e.getMessage());
}
}
}
posted on   毛会懂  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示