Browse Source

修复`CellData`可能不返回行列号

pull/1863/head
是仪 4 years ago
parent
commit
aae5ff0fdb
  1. 2
      src/main/java/com/alibaba/excel/metadata/CellData.java
  2. 75
      src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java
  3. 26
      src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java
  4. 24
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  5. 39
      src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteCellHandler.java
  6. 4
      src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java

2
src/main/java/com/alibaba/excel/metadata/CellData.java

@ -53,6 +53,8 @@ public class CellData<T> extends AbstractCell {
this.dataFormat = other.dataFormat; this.dataFormat = other.dataFormat;
this.dataFormatString = other.dataFormatString; this.dataFormatString = other.dataFormatString;
this.data = other.data; this.data = other.data;
setRowIndex(other.getRowIndex());
setColumnIndex(other.getColumnIndex());
} }
public CellData() {} public CellData() {}

75
src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java

@ -3,18 +3,7 @@ package com.alibaba.easyexcel.test.temp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeData; import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
@ -23,6 +12,13 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* 临时测试 * 临时测试
* *
@ -59,8 +55,23 @@ public class WriteLargeTest {
HorizontalCellStyleStrategy horizontalCellStyleStrategy = HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
ExcelWriter excelWriter = EasyExcel.write(fileName, LargeData.class).registerWriteHandler(
horizontalCellStyleStrategy).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 100; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} fill success.", j);
}
excelWriter.finish();
}
ExcelWriter excelWriter = EasyExcel.write(fileName,LargeData.class).registerWriteHandler(horizontalCellStyleStrategy).build(); @Test
public void test2() throws Exception {
// 方法2 如果写到不同的sheet 同一个对象
String fileName = TestFileUtil.getPath() + "large" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName, LargeData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 100; j++) { for (int j = 0; j < 100; j++) {
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet);
@ -70,39 +81,17 @@ public class WriteLargeTest {
} }
private List<List<String>> data() {
List<List<String>> list = new ArrayList<>();
private List<LargeData> data() { for (int j = 0; j < 10000; j++) {
List<LargeData> list = new ArrayList<LargeData>(); List<String> oneRow = new ArrayList<>();
int size = i + 5000; for (int i = 0; i < 150; i++) {
for (; i < size; i++) { oneRow.add("这是测试字段" + i);
LargeData largeData = new LargeData(); }
list.add(largeData); list.add(oneRow);
largeData.setStr1("str1-" + i);
largeData.setStr2("str2-" + i);
largeData.setStr3("str3-" + i);
largeData.setStr4("str4-" + i);
largeData.setStr5("str5-" + i);
largeData.setStr6("str6-" + i);
largeData.setStr7("str7-" + i);
largeData.setStr8("str8-" + i);
largeData.setStr9("str9-" + i);
largeData.setStr10("str10-" + i);
largeData.setStr11("str11-" + i);
largeData.setStr12("str12-" + i);
largeData.setStr13("str13-" + i);
largeData.setStr14("str14-" + i);
largeData.setStr15("str15-" + i);
largeData.setStr16("str16-" + i);
largeData.setStr17("str17-" + i);
largeData.setStr18("str18-" + i);
largeData.setStr19("str19-" + i);
largeData.setStr20("str20-" + i);
largeData.setStr21("str21-" + i);
largeData.setStr22("str22-" + i);
largeData.setStr23("str23-" + i);
largeData.setStr24("str24-" + i);
largeData.setStr25("str25-" + i);
} }
return list; return list;
} }
} }

26
src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java

@ -1,16 +1,19 @@
package com.alibaba.easyexcel.test.temp.large; package com.alibaba.easyexcel.test.temp.large;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.lang.reflect.Field;
import com.alibaba.easyexcel.test.core.large.LargeDataTest;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeDataTest;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@ -29,9 +32,24 @@ public class TempLargeDataTest {
@Test @Test
public void noModelRead() throws Exception { public void noModelRead() throws Exception {
ZipSecureFile.setMaxEntrySize(Long.MAX_VALUE);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.readUserHomeFile("test/ld.xlsx"), new NoModelLargeDataListener()) EasyExcel.read(TestFileUtil.readUserHomeFile("test/ld.xlsx"), new NoModelLargeDataListener())
.sheet().doRead(); .sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
} }
@Test
public void noModelRead2() throws Exception {
Field field = ZipSecureFile.class.getDeclaredField("MAX_ENTRY_SIZE");
field.setAccessible(true);
field.set(null, Long.MAX_VALUE);
long start = System.currentTimeMillis();
EasyExcel.read(
new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/target/test-classes/large1617887262709.xlsx"),
new NoModelLargeDataListener())
.sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
}
} }

24
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -5,11 +5,6 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeData; import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
@ -20,6 +15,10 @@ import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* 测试poi * 测试poi
@ -56,7 +55,18 @@ public class Wirte {
String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, WriteData.class).sheet("模板").registerWriteHandler(new WriteHandler()).doWrite(data1()); EasyExcel.write(fileName, WriteData.class).sheet("模板").registerWriteHandler(new WriteHandler()).doWrite(
data1());
}
@Test
public void simpleWrite3() {
// 写法1
String fileName = TestFileUtil.getPath() + "t33" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName).head(head()).inMemory(true).sheet("模板").registerWriteHandler(new WriteCellHandler()).doWrite(
data1());
} }
@Test @Test
@ -125,7 +135,7 @@ public class Wirte {
List<WriteData> list = new ArrayList<WriteData>(); List<WriteData> list = new ArrayList<WriteData>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
WriteData data = new WriteData(); WriteData data = new WriteData();
data.setF(300.35f); data.setDd(new Date());
data.setF1(33f); data.setF1(33f);
list.add(data); list.add(data);
} }

39
src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteCellHandler.java

@ -0,0 +1,39 @@
package com.alibaba.easyexcel.test.temp.simple;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* @author Jiaju Zhuang
*/
@Slf4j
public class WriteCellHandler extends AbstractCellWriteHandler {
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
CellData cellData, Cell cell, Head head, Integer integer, Boolean isHead) {
if (!isHead) {
CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
if (cellStyle != null) {
DataFormat dataFormat = createHelper.createDataFormat();
cellStyle.setWrapText(true);
cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
cellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
cellStyle.setDataFormat(dataFormat.getFormat("yyyy-MM-dd"));
cell.setCellStyle(cellStyle);
}
}
}
}

4
src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java

@ -1,5 +1,7 @@
package com.alibaba.easyexcel.test.temp.simple; package com.alibaba.easyexcel.test.temp.simple;
import java.util.Date;
import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.annotation.write.style.ContentStyle;
import lombok.Data; import lombok.Data;
@ -12,7 +14,7 @@ import lombok.Data;
@Data @Data
public class WriteData { public class WriteData {
// @ContentStyle(locked = true) // @ContentStyle(locked = true)
private float f; private Date dd;
// @ContentStyle(locked = false) // @ContentStyle(locked = false)
private float f1; private float f1;
} }

Loading…
Cancel
Save