解决冲突

This commit is contained in:
totalo
2020-11-16 13:37:32 +08:00
80 changed files with 2465 additions and 578 deletions

View File

@@ -3,6 +3,7 @@ package cn.hutool.poi.excel;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.File;
@@ -122,6 +123,24 @@ public class BigExcelWriter extends ExcelWriter {
// -------------------------------------------------------------------------- Constructor end
@Override
public BigExcelWriter autoSizeColumn(int columnIndex) {
final SXSSFSheet sheet = (SXSSFSheet)this.sheet;
sheet.trackColumnForAutoSizing(columnIndex);
super.autoSizeColumn(columnIndex);
sheet.untrackColumnForAutoSizing(columnIndex);
return this;
}
@Override
public BigExcelWriter autoSizeColumnAll() {
final SXSSFSheet sheet = (SXSSFSheet)this.sheet;
sheet.trackAllColumnsForAutoSizing();
super.autoSizeColumnAll();
sheet.untrackAllColumnsForAutoSizing();
return this;
}
@Override
public ExcelWriter flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
if(false == isFlushed){

View File

@@ -73,7 +73,7 @@ public class RowUtil {
Object cellValue;
boolean isAllNull = true;
for (int i = startCellNumInclude; i < size; i++) {
cellValue = CellUtil.getCellValue(row.getCell(i), cellEditor);
cellValue = CellUtil.getCellValue(CellUtil.getCell(row, i), cellEditor);
isAllNull &= StrUtil.isEmptyIfStr(cellValue);
cellValues.add(cellValue);
}

View File

@@ -74,10 +74,7 @@ public class CellUtil {
* @return 值类型可能为Date、Double、Boolean、String
*/
public static Object getCellValue(Cell cell, CellEditor cellEditor) {
if (null == cell) {
return null;
}
return getCellValue(cell, cell.getCellTypeEnum(), cellEditor);
return getCellValue(cell, null, cellEditor);
}
/**
@@ -105,6 +102,9 @@ public class CellUtil {
if (null == cell) {
return null;
}
if(cell instanceof NullCell){
return null == cellEditor ? null : cellEditor.edit(cell, null);
}
if (null == cellType) {
cellType = cell.getCellTypeEnum();
}
@@ -235,7 +235,23 @@ public class CellUtil {
}
/**
* 获取已有行或创建新行
*获取单元格,如果单元格不存在,返回{@link NullCell}
*
* @param row Excel表的行
* @param cellIndex 列号
* @return {@link Row}
* @since 5.5.0
*/
public static Cell getCell(Row row, int cellIndex) {
Cell cell = row.getCell(cellIndex);
if (null == cell) {
return new NullCell(row, cellIndex);
}
return cell;
}
/**
* 获取已有单元格或创建新单元格
*
* @param row Excel表的行
* @param cellIndex 列号

View File

@@ -0,0 +1,240 @@
package cn.hutool.poi.excel.cell;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
/**
* 当单元格不存在时使用此对象表示得到的值都为null,此对象只用于标注单元格所在位置信息。
*
* @author looly
* @since 5.5.0
*/
public class NullCell implements Cell {
private final Row row;
private final int columnIndex;
/**
* 构造
*
* @param row 行
* @param columnIndex 列号从0开始
*/
public NullCell(Row row, int columnIndex) {
this.row = row;
this.columnIndex = columnIndex;
}
@Override
public int getColumnIndex() {
return this.columnIndex;
}
@Override
public int getRowIndex() {
return getRow().getRowNum();
}
@Override
public Sheet getSheet() {
return getRow().getSheet();
}
@Override
public Row getRow() {
return this.row;
}
@Override
public void setCellType(CellType cellType) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setBlank() {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public CellType getCellType() {
return null;
}
@Override
public CellType getCellTypeEnum() {
return null;
}
@Override
public CellType getCachedFormulaResultType() {
return null;
}
@Override
public CellType getCachedFormulaResultTypeEnum() {
return null;
}
@Override
public void setCellValue(double value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellValue(Date value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellValue(LocalDateTime value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellValue(Calendar value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellValue(RichTextString value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellValue(String value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellFormula(String formula) throws FormulaParseException, IllegalStateException {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void removeFormula() throws IllegalStateException {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public String getCellFormula() {
return null;
}
@Override
public double getNumericCellValue() {
throw new UnsupportedOperationException("Cell value is null!");
}
@Override
public Date getDateCellValue() {
return null;
}
@Override
public LocalDateTime getLocalDateTimeCellValue() {
return null;
}
@Override
public RichTextString getRichStringCellValue() {
return null;
}
@Override
public String getStringCellValue() {
return null;
}
@Override
public void setCellValue(boolean value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void setCellErrorValue(byte value) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public boolean getBooleanCellValue() {
throw new UnsupportedOperationException("Cell value is null!");
}
@Override
public byte getErrorCellValue() {
throw new UnsupportedOperationException("Cell value is null!");
}
@Override
public void setCellStyle(CellStyle style) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public CellStyle getCellStyle() {
return null;
}
@Override
public void setAsActiveCell() {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public CellAddress getAddress() {
return null;
}
@Override
public void setCellComment(Comment comment) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public Comment getCellComment() {
return null;
}
@Override
public void removeCellComment() {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public Hyperlink getHyperlink() {
return null;
}
@Override
public void setHyperlink(Hyperlink link) {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public void removeHyperlink() {
throw new UnsupportedOperationException("Can not set any thing to null cell!");
}
@Override
public CellRangeAddress getArrayFormulaRange() {
return null;
}
@Override
public boolean isPartOfArrayFormulaGroup() {
throw new UnsupportedOperationException("Cell value is null!");
}
}

View File

@@ -16,7 +16,12 @@ import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 写出Excel单元测试
@@ -218,17 +223,24 @@ public class BigExcelWriteTest {
@Ignore
public void issue1210() {
// 通过工具类创建writer
String path = "e:/issue1210.xlsx";
String path = "d:/test/issue1210.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
writer.addHeaderAlias("id", "SN");
writer.addHeaderAlias("userName", "User Name");
List<Map<String, Object>> list = new ArrayList<>();
list.add(new HashMap<String, Object>() {{
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 1);
put("userName", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}});
list.add(new HashMap<String, Object>() {{
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 2);
put("userName", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}});

View File

@@ -3,6 +3,7 @@ package cn.hutool.poi.excel.test;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.Data;
@@ -186,6 +187,7 @@ public class ExcelReadTest {
}
@Test
@Ignore
public void readCellsTest() {
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
reader.read((cell, value)-> Console.log("{}, {} {}", cell.getRowIndex(), cell.getColumnIndex(), value));
@@ -201,4 +203,20 @@ public class ExcelReadTest {
Console.log(list);
}
}
@Test
public void nullValueEditTest(){
final ExcelReader reader = ExcelUtil.getReader("null_cell_test.xlsx");
reader.setCellEditor((cell, value)-> ObjectUtil.defaultIfNull(value, "#"));
final List<List<Object>> read = reader.read();
// 对于任意一个单元格有值的情况下之前的单元格值按照null处理
Assert.assertEquals(1, read.get(1).size());
Assert.assertEquals(2, read.get(2).size());
Assert.assertEquals(3, read.get(3).size());
Assert.assertEquals("#", read.get(2).get(0));
Assert.assertEquals("#", read.get(3).get(0));
Assert.assertEquals("#", read.get(3).get(1));
}
}

Binary file not shown.