java read excel spreadsheet content

First, add dependencies

<dependency>
<the groupId> org.apache.poi </ the groupId>
<the artifactId> POI </ the artifactId>
<Version> 3.8 </ Version>
</ dependency>
<dependency>
<the groupId> org.apache.poi </ the groupId>
<the artifactId> POI-OOXML </ the artifactId>
<Version> 3.8 </ Version>
</ dependency>
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
II tools
determines 2003 || 2007 || 2010 according to the file format suffix.

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


public class ExcelUtils {

private static Workbook wb;
private static Sheet sheet;
private static Row row;

private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";


/**
* 读取表头
*
* @param inputStream inputStream
* @param suffix file suffix
* @return map <index,value>
*/
public static Map<Integer, String> readExcelTitle(InputStream inputStream, String suffix) {
getWorkbook(inputStream, suffix);
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < colNum; i++) {
map.put(i, row.getCell(i).getStringCellValue());
}
return map;
}

/ **
* Read excel content
*
* @param inputStream file
* @return Map <row, Map <subscript, Object >>
* /
public static the Map <Integer, Map <Integer, String >> (InputStream inputStream readExcelContent, String suffix) {
GetWorkbook (inputStream, suffix);
the Map <Integer, the Map <Integer, String Content >> new new = the HashMap <> ();
Sheet wb.getSheetAt = (0);
// get the number of rows
int rowNum = sheet.getLastRowNum ();
row = sheet.getRow (0);
int colNum row.getPhysicalNumberOfCells = ();
// body content should start from the second row, the first row header heading
for (int i = 1; i <= rowNum ; I ++) {
Row = sheet.getRow (I);
int J = 0;
the Map <Integer, String> = new new CellValue the HashMap <> ();
while (j < colNum) {
String obj = getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue);
}
return content;
}

private static String getCellFormatValue(Cell cell) {
String cellValue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case Cell.CELL_TYPE_NUMERIC:
case Cell.CELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (DateUtil.isCellDateFormatted(cell)) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
Instant instant = cell.getDateCellValue().toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
cellValue = dateTimeFormatter.format(localDateTime);
} else {
// 如果是纯数字
// get the current value Cell
CellValue = String.valueOf (cell.getNumericCellValue ());
}
BREAK;
}
// if the current Cell Type is STRING
Case Cell.CELL_TYPE_STRING:
// get the current Cell String
cellValue = cell. . getRichStringCellValue () the getString ();
BREAK;
default:
// default value Cell
CellValue = "";
}
}
return CellValue;
}


private static void getWorkbook(InputStream inputStream, String suffix) {

try {
//2003
if (EXCEL_XLS.equals(suffix)) {
wb = new HSSFWorkbook(inputStream);
//2007/2010
} else if (EXCEL_XLSX.equals(suffix)) {
wb = new XSSFWorkbook(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
---------------------
Author: Break all
Source: CSDN
Original: https: //blog.csdn.net/weixin_40467684/article/details/91883896
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/douyinlianmeng/p/11203873.html