需求
- 解析这个excel 空格填写e ,单元格合并的拆分后填写合并前的值
id 啊 哦 额
1 2 2 3
2 e e 3
3 4 4 3
4 4 4 3
5 0 0 2019-09-01
code
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
代码
package com.huifer.springsource.uuc;
class ExcelDataObject {
private String cellData;
private int regionId;
ExcelDataObject(String cellData, int regionId) {
this.cellData = cellData;
this.regionId = regionId;
}
public String getCellData() {
return cellData;
}
public void setCellData(String cellData) {
this.cellData = cellData;
}
public int getRegionId() {
return regionId;
}
public void setRegionId(int regionId) {
this.regionId = regionId;
}
}
package com.huifer.springsource.uuc;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
class ExcelReaderImpl implements ExcelReader {
private XSSFWorkbook excelWBook;
private XSSFSheet excelWSheet;
private List<CellRangeAddress> mergedRegions;
private Logger log = Logger.getLogger(this.getClass().getName());
private static String getValue(Cell cell) {
if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
String pattern;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(date);
} else {
return NumberToTextConverter.toText(cell.getNumericCellValue());
}
} else {
return String.valueOf(cell.getStringCellValue());
}
}
@Override
public void setExcelFile(String path) {
try {
FileInputStream ExcelFile = new FileInputStream(path);
excelWBook = new XSSFWorkbook(ExcelFile);
ExcelFile.close();
switchToSheet(0);
} catch (Exception e) {
throw new RuntimeException("文件有问题开不开 " + e.getMessage(), e);
}
}
@Override
public void switchToSheet(String sheetName) throws RuntimeException {
int sheetIndex = excelWBook.getSheetIndex(sheetName);
switchToSheet(sheetIndex);
}
@Override
public void switchToSheet(int number) throws RuntimeException {
excelWSheet = excelWBook.getSheetAt(number);
mergedRegions = excelWSheet.getMergedRegions();
}
@Override
public ExcelDataObject[][] getCurrentSheetData() {
int usedRows = getRowsUsed();
ExcelDataObject[][] data = new ExcelDataObject[usedRows][];
for (int i = 0; i < usedRows; i++) {
data[i] = getRowData(i);
}
return data;
}
@Override
public ExcelDataObject[][] getSheetData(String sheetName) {
switchToSheet(sheetName);
int usedRows = getRowsUsed();
ExcelDataObject[][] data = new ExcelDataObject[usedRows][];
for (int i = 0; i <= usedRows; i++) {
data[i] = getRowData(i);
}
return data;
}
@Override
public ExcelDataObject[] getRowData(int rowNo) {
int usedColumns = getColumnsUsed(rowNo);
ExcelDataObject[] rowData = new ExcelDataObject[usedColumns];
for (int i = 0; i < usedColumns; i++) {
rowData[i] = getCellData(rowNo, i);
}
return rowData;
}
@Override
public ExcelDataObject getCellData(int rowNum, int colNum) {
int region = getMergedRegion(rowNum, colNum);
XSSFCell cell;
try {
cell = region >= 0 ? getMergedRegionStringValue(rowNum, colNum) :
excelWSheet.getRow(rowNum).getCell(colNum);
return getStringValueFromCell(region, cell);
} catch (Exception e) {
return new ExcelDataObject("e", region);
}
}
private ExcelDataObject getStringValueFromCell(int region, XSSFCell cell) {
return new ExcelDataObject(getValue(cell), region);
}
private int getRowsUsed() {
if (excelWSheet == null) {
return 0;
}
return excelWSheet.getLastRowNum();
}
public int getRols() {
if (excelWSheet == null) {
return 0;
}
return excelWSheet.getLastRowNum() + 1;
}
private int getColumnsUsed(int rowNo) {
if (excelWSheet == null) {
return 0;
}
return excelWSheet.getRow(rowNo).getPhysicalNumberOfCells();
}
public int getCols() {
return excelWSheet.getRow(0).getPhysicalNumberOfCells();
}
@Override
public List<List<ExcelDataObject>> getSheet(String path) {
setExcelFile(path);
int rowsUsed = getRols();
int cols = getCols();
List<List<ExcelDataObject>> sheet = new ArrayList<>();
for (int i = 0; i < rowsUsed; i++) {
List<ExcelDataObject> row = new ArrayList<>();
for (int j = 0; j < cols; j++) {
Object cellData = getCellData(i, j);
ExcelDataObject data = (ExcelDataObject) cellData;
row.add(data);
String cellData1 = data.getCellData();
System.out.print(cellData1 + "\t");
}
sheet.add(row);
System.out.println();
}
return sheet;
}
private int getMergedRegion(int rowNum, int colNum) {
for (int i = 0; i < mergedRegions.size(); i++) {
if (mergedRegions.get(i).isInRange(rowNum, colNum)) {
return i;
}
}
return -1;
}
private XSSFCell getMergedRegionStringValue(int row, int column) {
int mergedRegionNumber = getMergedRegion(row, column);
CellRangeAddress region = excelWSheet.getMergedRegion(mergedRegionNumber);
int firstRegionColumn = region.getFirstColumn();
int firstRegionRow = region.getFirstRow();
return excelWSheet.getRow(firstRegionRow).getCell(firstRegionColumn);
}
}
package com.huifer.springsource.uuc;
import java.util.ArrayList;
import java.util.List;
public class ExcelReaderTest {
public static void main(String[] args) {
getSheet();
}
private static void getSheet() {
ExcelReader excelReader = new ExcelReaderImpl();
excelReader.setExcelFile("E:\\w_pro\\tt\\demo\\spring-source\\src\\main\\resources\\work01.xlsx");
int rowsUsed = excelReader.getRols();
int cols = excelReader.getCols();
List<List<ExcelDataObject>> sheet = new ArrayList<>();
for (int i = 0; i < rowsUsed; i++) {
List<ExcelDataObject> row = new ArrayList<>();
for (int j = 0; j < cols; j++) {
Object cellData = excelReader.getCellData(i, j);
ExcelDataObject data = (ExcelDataObject) cellData;
row.add(data);
String cellData1 = data.getCellData();
System.out.print(cellData1 + "\t");
}
sheet.add(row);
System.out.println();
}
}
}