diff --git a/img/readme/quickstart/write/customHandlerWrite.png b/img/readme/quickstart/write/customHandlerWrite.png new file mode 100644 index 0000000..41916c3 Binary files /dev/null and b/img/readme/quickstart/write/customHandlerWrite.png differ diff --git a/quickstart.md b/quickstart.md index b9a7d0d..6312800 100644 --- a/quickstart.md +++ b/quickstart.md @@ -24,6 +24,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja * [使用table去写入](#tableWrite) * [动态头,实时生成头写入](#dynamicHeadWrite) * [自动列宽(不太精确)](#longestMatchColumnWidthWrite) +* [自定义拦截器(下拉,超链接等上面几点都不符合但是要对单元格进行操作的参照这个)](#customHandlerWrite) * [web中的写](#webWrite) ## 读excel样例 @@ -810,7 +811,7 @@ public class LongestMatchColumnWidthData { ``` ##### 代码 ```java - /** + /** * 自动列宽(不太精确) *
* 这个目前不是很好用,比如有数字就会导致换行。而且长度也不是刚好和实际长度一致。 所以需要精确到刚好列宽的慎用。 当然也可以自己参照 @@ -818,16 +819,20 @@ public class LongestMatchColumnWidthData { *
* poi 自带{@link SXSSFSheet#autoSizeColumn(int)} 对中文支持也不太好。目前没找到很好的算法。 有的话可以推荐下。 * - *
1. 创建excel对应的实体对象 参照{@link DemoData} - *
3. 注册策略{@link LongestMatchColumnWidthStyleStrategy} - *
2. 直接写即可 + *
+ * 1. 创建excel对应的实体对象 参照{@link LongestMatchColumnWidthData} + *
+ * 2. 注册策略{@link LongestMatchColumnWidthStyleStrategy} + *
+ * 3. 直接写即可
*/
@Test
public void longestMatchColumnWidthWrite() {
String fileName =
TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
- EasyExcel.write(fileName, LongestMatchColumnWidthData.class).sheet("模板").doWrite(dataLong());
+ EasyExcel.write(fileName, LongestMatchColumnWidthData.class)
+ .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong());
}
private List
+ * demo这里实现2点。1. 对第一行第一列的头超链接到:https://github.com/alibaba/easyexcel 2. 对第一列第一行和第二行的数据新增下拉框,显示 测试1 测试2
+ *
+ * 1. 创建excel对应的实体对象 参照{@link DemoData}
+ *
+ * 2. 注册拦截器 {@link CustomCellWriteHandler} {@link CustomSheetWriteHandler}
+ *
+ * 2. 直接写即可
+ */
+ @Test
+ public void customHandlerWrite() {
+ String fileName = TestFileUtil.getPath() + "customHandlerWrite" + System.currentTimeMillis() + ".xlsx";
+ // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
+ EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CustomSheetWriteHandler())
+ .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());
+ }
+```
+
### web中的写
##### 示例代码
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java)
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomCellWriteHandler.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomCellWriteHandler.java
new file mode 100644
index 0000000..bff544a
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomCellWriteHandler.java
@@ -0,0 +1,45 @@
+package com.alibaba.easyexcel.test.demo.write;
+
+import org.apache.poi.common.usermodel.HyperlinkType;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.usermodel.Row;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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;
+
+/**
+ * 自定义拦截器。对第一行第一列的头超链接到:https://github.com/alibaba/easyexcel
+ *
+ * @author Jiaju Zhuang
+ */
+public class CustomCellWriteHandler implements CellWriteHandler {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(CustomCellWriteHandler.class);
+
+ @Override
+ public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
+ Head head, int relativeRowIndex, boolean isHead) {
+
+ }
+
+ @Override
+ public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData,
+ Cell cell, Head head, int relativeRowIndex, boolean isHead) {
+ // 这里可以对cell进行任何操作
+ LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
+ if (isHead && cell.getColumnIndex() == 0) {
+ CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
+ Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
+ hyperlink.setAddress("https://github.com/alibaba/easyexcel");
+ cell.setHyperlink(hyperlink);
+ }
+ }
+
+}
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomSheetWriteHandler.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomSheetWriteHandler.java
new file mode 100644
index 0000000..fe29045
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomSheetWriteHandler.java
@@ -0,0 +1,39 @@
+package com.alibaba.easyexcel.test.demo.write;
+
+import org.apache.poi.ss.usermodel.DataValidation;
+import org.apache.poi.ss.usermodel.DataValidationConstraint;
+import org.apache.poi.ss.usermodel.DataValidationHelper;
+import org.apache.poi.ss.util.CellRangeAddressList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.alibaba.excel.write.handler.SheetWriteHandler;
+import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
+import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
+
+/**
+ * 自定义拦截器.对第一列第一行和第二行的数据新增下拉框,显示 测试1 测试2
+ *
+ * @author Jiaju Zhuang
+ */
+public class CustomSheetWriteHandler implements SheetWriteHandler {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(CustomSheetWriteHandler.class);
+
+ @Override
+ public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
+
+ }
+
+ @Override
+ public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
+ LOGGER.info("第{}个Sheet写入成功。", writeSheetHolder.getSheetNo());
+
+ // 区间设置 第一列第一行和第二行的数据。由于第一行是头,所以第一、二行的数据实际上是第二三行
+ CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 2, 0, 0);
+ DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
+ DataValidationConstraint constraint = helper.createExplicitListConstraint(new String[] {"测试1", "测试2"});
+ DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
+ writeSheetHolder.getSheet().addValidationData(dataValidation);
+ }
+}
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
index 482f8f7..e6bc998 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
@@ -311,11 +311,11 @@ public class WriteTest {
* poi 自带{@link SXSSFSheet#autoSizeColumn(int)} 对中文支持也不太好。目前没找到很好的算法。 有的话可以推荐下。
*
*
- * 1. 创建excel对应的实体对象 参照{@link DemoData}
+ * 1. 创建excel对应的实体对象 参照{@link LongestMatchColumnWidthData}
*
- * 3. 注册策略{@link LongestMatchColumnWidthStyleStrategy}
+ * 2. 注册策略{@link LongestMatchColumnWidthStyleStrategy}
*
- * 2. 直接写即可
+ * 3. 直接写即可
*/
@Test
public void longestMatchColumnWidthWrite() {
@@ -326,6 +326,25 @@ public class WriteTest {
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong());
}
+ /**
+ * 下拉,超链接等自定义拦截器(上面几点都不符合但是要对单元格进行操作的参照这个)
+ *
+ * demo这里实现2点。1. 对第一行第一列的头超链接到:https://github.com/alibaba/easyexcel 2. 对第一列第一行和第二行的数据新增下拉框,显示 测试1 测试2
+ *
+ * 1. 创建excel对应的实体对象 参照{@link DemoData}
+ *
+ * 2. 注册拦截器 {@link CustomCellWriteHandler} {@link CustomSheetWriteHandler}
+ *
+ * 2. 直接写即可
+ */
+ @Test
+ public void customHandlerWrite() {
+ String fileName = TestFileUtil.getPath() + "customHandlerWrite" + System.currentTimeMillis() + ".xlsx";
+ // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
+ EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CustomSheetWriteHandler())
+ .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());
+ }
+
private List