Jiaju Zhuang
4 years ago
committed by
GitHub
39 changed files with 771 additions and 174 deletions
@ -0,0 +1,81 @@
|
||||
package com.alibaba.excel.metadata.format; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.math.MathContext; |
||||
import java.math.RoundingMode; |
||||
import java.text.DecimalFormat; |
||||
import java.text.DecimalFormatSymbols; |
||||
import java.text.FieldPosition; |
||||
import java.text.Format; |
||||
import java.text.ParsePosition; |
||||
import java.util.Locale; |
||||
|
||||
import org.apache.poi.ss.usermodel.DataFormatter; |
||||
|
||||
/** |
||||
* Written with reference to {@link org.apache.poi.ss.usermodel.ExcelGeneralNumberFormat }. |
||||
* <p> |
||||
* Supported Do not use scientific notation. |
||||
* |
||||
* @author JiaJu Zhuang |
||||
**/ |
||||
public class ExcelGeneralNumberFormat extends Format { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
private static final MathContext TO_10_SF = new MathContext(10, RoundingMode.HALF_UP); |
||||
|
||||
private final DecimalFormatSymbols decimalSymbols; |
||||
private final DecimalFormat integerFormat; |
||||
private final DecimalFormat decimalFormat; |
||||
private final DecimalFormat scientificFormat; |
||||
|
||||
public ExcelGeneralNumberFormat(final Locale locale, final boolean useScientificFormat) { |
||||
decimalSymbols = DecimalFormatSymbols.getInstance(locale); |
||||
// Supported Do not use scientific notation.
|
||||
if (useScientificFormat) { |
||||
scientificFormat = new DecimalFormat("0.#####E0", decimalSymbols); |
||||
} else { |
||||
scientificFormat = new DecimalFormat("#", decimalSymbols); |
||||
} |
||||
org.apache.poi.ss.usermodel.DataFormatter.setExcelStyleRoundingMode(scientificFormat); |
||||
integerFormat = new DecimalFormat("#", decimalSymbols); |
||||
org.apache.poi.ss.usermodel.DataFormatter.setExcelStyleRoundingMode(integerFormat); |
||||
decimalFormat = new DecimalFormat("#.##########", decimalSymbols); |
||||
DataFormatter.setExcelStyleRoundingMode(decimalFormat); |
||||
} |
||||
|
||||
@Override |
||||
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) { |
||||
final double value; |
||||
if (number instanceof Number) { |
||||
value = ((Number) number).doubleValue(); |
||||
if (Double.isInfinite(value) || Double.isNaN(value)) { |
||||
return integerFormat.format(number, toAppendTo, pos); |
||||
} |
||||
} else { |
||||
// testBug54786 gets here with a date, so retain previous behaviour
|
||||
return integerFormat.format(number, toAppendTo, pos); |
||||
} |
||||
|
||||
final double abs = Math.abs(value); |
||||
if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) { |
||||
return scientificFormat.format(number, toAppendTo, pos); |
||||
} else if (Math.floor(value) == value || abs >= 1E10) { |
||||
// integer, or integer portion uses all 11 allowed digits
|
||||
return integerFormat.format(number, toAppendTo, pos); |
||||
} |
||||
// Non-integers of non-scientific magnitude are formatted as "up to 11
|
||||
// numeric characters, with the decimal point counting as a numeric
|
||||
// character". We know there is a decimal point, so limit to 10 digits.
|
||||
// https://support.microsoft.com/en-us/kb/65903
|
||||
final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue(); |
||||
return decimalFormat.format(rounded, toAppendTo, pos); |
||||
} |
||||
|
||||
@Override |
||||
public Object parseObject(String source, ParsePosition pos) { |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,56 @@
|
||||
package com.alibaba.excel.write.merge; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.write.handler.CellWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
||||
|
||||
/** |
||||
* Merge strategy |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public abstract class AbstractMergeStrategy implements CellWriteHandler { |
||||
@Override |
||||
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, |
||||
Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, |
||||
Head head, Integer relativeRowIndex, Boolean isHead) {} |
||||
|
||||
@Override |
||||
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, |
||||
WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, |
||||
Boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, |
||||
List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
if (isHead) { |
||||
return; |
||||
} |
||||
merge(writeSheetHolder.getSheet(), cell, head, relativeRowIndex); |
||||
} |
||||
|
||||
/** |
||||
* merge |
||||
* |
||||
* @param sheet |
||||
* @param cell |
||||
* @param head |
||||
* @param relativeRowIndex |
||||
*/ |
||||
protected abstract void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex); |
||||
} |
@ -0,0 +1,108 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.util.ArrayList; |
||||
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.demo.write.DemoData; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.write.metadata.WriteSheet; |
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class WriteLargeTest { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WriteLargeTest.class); |
||||
private int i = 0; |
||||
|
||||
@Test |
||||
public void test() throws Exception { |
||||
// 方法2 如果写到不同的sheet 同一个对象
|
||||
String fileName = TestFileUtil.getPath() + "large" + System.currentTimeMillis() + ".xlsx"; |
||||
// 头的策略
|
||||
WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
||||
// 背景设置为红色
|
||||
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); |
||||
WriteFont headWriteFont = new WriteFont(); |
||||
headWriteFont.setFontHeightInPoints((short)20); |
||||
headWriteCellStyle.setWriteFont(headWriteFont); |
||||
// 内容的策略
|
||||
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); |
||||
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
|
||||
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
||||
// 背景绿色
|
||||
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); |
||||
WriteFont contentWriteFont = new WriteFont(); |
||||
// 字体大小
|
||||
contentWriteFont.setFontHeightInPoints((short)20); |
||||
contentWriteCellStyle.setWriteFont(contentWriteFont); |
||||
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
|
||||
HorizontalCellStyleStrategy horizontalCellStyleStrategy = |
||||
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(); |
||||
|
||||
} |
||||
|
||||
|
||||
private List<LargeData> data() { |
||||
List<LargeData> list = new ArrayList<LargeData>(); |
||||
int size = i + 5000; |
||||
for (; i < size; i++) { |
||||
LargeData largeData = new LargeData(); |
||||
list.add(largeData); |
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,67 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import org.apache.poi.hssf.util.CellReference; |
||||
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; |
||||
|
||||
import com.alibaba.easyexcel.test.demo.write.DemoData; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; |
||||
import com.alibaba.excel.write.metadata.WriteSheet; |
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class WriteV33Test { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WriteV33Test.class); |
||||
|
||||
@Test |
||||
public void test() throws Exception { |
||||
// 方法2 如果写到不同的sheet 同一个对象
|
||||
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; |
||||
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1); |
||||
|
||||
// 这里 指定文件
|
||||
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).registerWriteHandler(onceAbsoluteMergeStrategy).build(); |
||||
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "模板1").build(); |
||||
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build(); |
||||
WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "模板3").build(); |
||||
excelWriter.write(data(2), writeSheet2); |
||||
excelWriter.write(data(3), writeSheet3); |
||||
excelWriter.write(data(1), writeSheet1); |
||||
excelWriter.write(data(3), writeSheet3); |
||||
excelWriter.write(data(1), writeSheet1); |
||||
/// 千万别忘记finish 会帮忙关闭流
|
||||
excelWriter.finish(); |
||||
} |
||||
|
||||
private List<DemoData> data(int no) { |
||||
List<DemoData> list = new ArrayList<DemoData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
DemoData data = new DemoData(); |
||||
data.setString("字符串" + no + "---" + i); |
||||
list.add(data); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,91 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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; |
||||
|
||||
import com.alibaba.easyexcel.test.demo.write.DemoData; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.write.metadata.WriteSheet; |
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class WriteV34Test { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WriteV34Test.class); |
||||
|
||||
@Test |
||||
public void test() throws Exception { |
||||
String fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx"; |
||||
// 头的策略
|
||||
WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
||||
// 背景设置为红色
|
||||
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); |
||||
WriteFont headWriteFont = new WriteFont(); |
||||
headWriteFont.setFontHeightInPoints((short)20); |
||||
headWriteCellStyle.setWriteFont(headWriteFont); |
||||
// 内容的策略
|
||||
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); |
||||
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
|
||||
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
||||
// 背景绿色
|
||||
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); |
||||
WriteFont contentWriteFont = new WriteFont(); |
||||
// 字体大小
|
||||
contentWriteFont.setFontHeightInPoints((short)20); |
||||
contentWriteCellStyle.setWriteFont(contentWriteFont); |
||||
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
|
||||
HorizontalCellStyleStrategy horizontalCellStyleStrategy = |
||||
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); |
||||
|
||||
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
|
||||
EasyExcel.write(fileName,DemoData.class).head(head()).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板") |
||||
.doWrite(data(1)); |
||||
} |
||||
|
||||
|
||||
private List<List<String>> head() { |
||||
List<List<String>> list = new ArrayList<List<String>>(); |
||||
List<String> head0 = new ArrayList<String>(); |
||||
head0.add("字符串" + System.currentTimeMillis()); |
||||
head0.add("再找找"); |
||||
List<String> head1 = new ArrayList<String>(); |
||||
head1.add("数字" + System.currentTimeMillis()); |
||||
List<String> head2 = new ArrayList<String>(); |
||||
head2.add("日期" + System.currentTimeMillis()); |
||||
List<String> head3 = new ArrayList<String>(); |
||||
head3.add("日期" + System.currentTimeMillis()); |
||||
list.add(head0); |
||||
list.add(head1); |
||||
list.add(head2); list.add(head3); |
||||
|
||||
|
||||
return list; |
||||
} |
||||
|
||||
private List<DemoData> data(int no) { |
||||
List<DemoData> list = new ArrayList<DemoData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
DemoData data = new DemoData(); |
||||
data.setString("字符串" + no + "---" + i); |
||||
list.add(data); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue