Excel读取工具类
发布时间:2024年01月12日
原生的读取,仅供参考,主要在switch方法那里:
public void testReadTeyp(FileInputStream inputStream) throws Exception {
inputStream = new FileInputStream(PATH + "test.xlsx");
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Row rowTitle = sheet.getRow(0);
if (rowTitle != null) {
int cells = rowTitle.getPhysicalNumberOfCells();
for (int i = 0; i < cells; i++) {
Cell cell = rowTitle.getCell(i);
if (cell != null) {
int cellType = cell.getCellType();
String cellValue = cell.getStringCellValue();
System.out.print(cellValue + " | ");
}
}
}
int rowCounts = sheet.getPhysicalNumberOfRows();
for (int rowNum = 1; rowNum < rowCounts; rowNum++) {
Row row = sheet.getRow(rowNum);
if (row != null) {
int columns = rowTitle.getPhysicalNumberOfCells();
for (int cellColumn = 0; cellColumn < columns; cellColumn++) {
System.out.print("[" + rowNum + "-" + cellColumn + "]");
Cell cell = row.getCell(cellColumn);
if (cell != null) {
int cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
System.out.print("[String]");
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.print("[BOOLEAN]");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
System.out.print("[BLANK]");
break;
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.print("[NUMERIC]");
if (HSSFDateUtil.isCellDateFormatted(cell)) {
System.out.print("[日期]");
Date time = cell.getDateCellValue();
cellValue = new DateTime(time).toString("yyyy-MM-dd HH:mm:ss");
} else {
System.out.print("[转换为字符串输出]");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellValue = cell.toString();
}
break;
case HSSFCell.CELL_TYPE_ERROR:
System.out.print("[数据类型错误]");
break;
}
System.out.println(cellValue);
}
}
}
}
inputStream.close();
}
封装好的,通过模板导入。
文章来源:https://blog.csdn.net/fghjhdrf/article/details/133814771
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!