-
Notifications
You must be signed in to change notification settings - Fork 441
feat: Supports obtaining current row data object during writing #598 #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
cc09e79
68d238b
d4fc9e4
d309218
3d15c69
0663e89
e0a804c
0fa17f2
21901c5
064ee86
064c578
c62d79e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.excel.demo.write; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fesod.excel.util.BooleanUtils; | ||
| import org.apache.fesod.excel.write.handler.CellWriteHandler; | ||
| import org.apache.fesod.excel.write.handler.context.CellWriteHandlerContext; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
|
|
||
| /** | ||
| * 拦截器中单元格上下文可以获取到行数据 | ||
| */ | ||
| @Slf4j | ||
| public class RecordCellWriteHandler implements CellWriteHandler { | ||
|
|
||
| @Override | ||
| public void afterCellDispose(CellWriteHandlerContext context) { | ||
| Cell cell = context.getCell(); | ||
|
|
||
| if (BooleanUtils.isFalse(context.getHead()) && cell.getColumnIndex() != 0) { | ||
| RecordData record = context.getOriginalRecord(); | ||
| log.info("写入数据:{}", record); | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| package org.apache.fesod.excel.demo.write; | ||||||
|
|
||||||
| import lombok.EqualsAndHashCode; | ||||||
| import lombok.Getter; | ||||||
| import lombok.Setter; | ||||||
| import lombok.ToString; | ||||||
| import org.apache.fesod.excel.annotation.ExcelProperty; | ||||||
|
|
||||||
| import java.util.Date; | ||||||
|
|
||||||
| /** | ||||||
| * Basic data class | ||||||
| * | ||||||
| * | ||||||
| **/ | ||||||
| @Getter | ||||||
| @Setter | ||||||
| @ToString | ||||||
| @EqualsAndHashCode | ||||||
| public class RecordData { | ||||||
| /** | ||||||
| * String Title | ||||||
| */ | ||||||
| @ExcelProperty("String Title") | ||||||
| private String string; | ||||||
|
|
||||||
| /** | ||||||
| * Date Title | ||||||
| */ | ||||||
| @ExcelProperty("Date Title") | ||||||
| private Date date; | ||||||
|
|
||||||
| /** | ||||||
| * Number Title | ||||||
| */ | ||||||
| @ExcelProperty("Number Title") | ||||||
| private Double doubleData; | ||||||
|
|
||||||
| /** | ||||||
| * 通过自定义转换器转换 | ||||||
|
||||||
| * 通过自定义转换器转换 | |
| * Converted by a custom converter |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| package org.apache.fesod.excel.demo.write; | ||||||
|
|
||||||
| import lombok.extern.slf4j.Slf4j; | ||||||
| import org.apache.fesod.excel.converters.Converter; | ||||||
| import org.apache.fesod.excel.converters.ReadConverterContext; | ||||||
| import org.apache.fesod.excel.converters.WriteConverterContext; | ||||||
| import org.apache.fesod.excel.enums.CellDataTypeEnum; | ||||||
| import org.apache.fesod.excel.metadata.data.WriteCellData; | ||||||
|
|
||||||
| /** | ||||||
| * converter to the original data record | ||||||
| */ | ||||||
| @Slf4j | ||||||
| public class RecordStringStringConverter implements Converter<String> { | ||||||
| @Override | ||||||
| public Class<?> supportJavaTypeKey() { | ||||||
| return String.class; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public CellDataTypeEnum supportExcelTypeKey() { | ||||||
| return CellDataTypeEnum.STRING; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String convertToJavaData(ReadConverterContext<?> context) { | ||||||
| return context.getReadCellData().getStringValue(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * 这里是写的时候会可以访问到原始数据,你可以进行其他业务处理,然后进行转换 | ||||||
|
||||||
| * 这里是写的时候会可以访问到原始数据,你可以进行其他业务处理,然后进行转换 | |
| * When writing, you can access the original data here. You can perform other business processing before conversion. |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,14 +19,6 @@ | |||||||||||
|
|
||||||||||||
| package org.apache.fesod.excel.demo.write; | ||||||||||||
|
|
||||||||||||
| import java.io.File; | ||||||||||||
| import java.io.InputStream; | ||||||||||||
| import java.net.URL; | ||||||||||||
| import java.util.ArrayList; | ||||||||||||
| import java.util.Date; | ||||||||||||
| import java.util.HashSet; | ||||||||||||
| import java.util.List; | ||||||||||||
| import java.util.Set; | ||||||||||||
| import org.apache.fesod.excel.ExcelWriter; | ||||||||||||
| import org.apache.fesod.excel.FastExcel; | ||||||||||||
| import org.apache.fesod.excel.annotation.ExcelProperty; | ||||||||||||
|
|
@@ -36,12 +28,7 @@ | |||||||||||
| import org.apache.fesod.excel.annotation.write.style.ContentRowHeight; | ||||||||||||
| import org.apache.fesod.excel.annotation.write.style.HeadRowHeight; | ||||||||||||
| import org.apache.fesod.excel.enums.CellDataTypeEnum; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.CommentData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.FormulaData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.HyperlinkData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.ImageData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.RichTextStringData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.WriteCellData; | ||||||||||||
| import org.apache.fesod.excel.metadata.data.*; | ||||||||||||
|
||||||||||||
| import org.apache.fesod.excel.metadata.data.*; | |
| import org.apache.fesod.excel.metadata.data.CellData; | |
| import org.apache.fesod.excel.metadata.data.WriteCellData; | |
| import org.apache.fesod.excel.metadata.data.WriteRowData; | |
| import org.apache.fesod.excel.metadata.data.WriteSheetData; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,7 @@ | |
|
|
||
| package org.apache.fesod.excel.converters; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.*; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid import * |
||
| import org.apache.fesod.excel.context.WriteContext; | ||
| import org.apache.fesod.excel.metadata.property.ExcelContentProperty; | ||
|
|
||
|
|
@@ -44,6 +40,11 @@ public class WriteConverterContext<T> { | |
| */ | ||
| private T value; | ||
|
|
||
| /** | ||
| * Java row level record | ||
| */ | ||
| private Object record; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about generics?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 采用泛型对已存在的代码改动比较多,我可以增加get方法进行泛型输出,这样改动比较少。在使用行级对象的时候不需要进行强制类型转换。 |
||
|
|
||
| /** | ||
| * Content property.Nullable. | ||
| */ | ||
|
|
@@ -53,4 +54,9 @@ public class WriteConverterContext<T> { | |
| * write context | ||
| */ | ||
| private WriteContext writeContext; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public <E extends Object> E getRecord() { | ||
| return (E) record; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,7 +19,6 @@ | |||||||||
|
|
||||||||||
| package org.apache.fesod.excel.write.executor; | ||||||||||
|
|
||||||||||
| import java.util.List; | ||||||||||
| import org.apache.commons.collections4.CollectionUtils; | ||||||||||
| import org.apache.fesod.excel.context.WriteContext; | ||||||||||
| import org.apache.fesod.excel.converters.Converter; | ||||||||||
|
|
@@ -28,31 +27,17 @@ | |||||||||
| import org.apache.fesod.excel.converters.WriteConverterContext; | ||||||||||
| import org.apache.fesod.excel.enums.CellDataTypeEnum; | ||||||||||
| import org.apache.fesod.excel.exception.ExcelWriteDataConvertException; | ||||||||||
| import org.apache.fesod.excel.metadata.data.CommentData; | ||||||||||
| import org.apache.fesod.excel.metadata.data.FormulaData; | ||||||||||
| import org.apache.fesod.excel.metadata.data.HyperlinkData; | ||||||||||
| import org.apache.fesod.excel.metadata.data.ImageData; | ||||||||||
| import org.apache.fesod.excel.metadata.data.WriteCellData; | ||||||||||
| import org.apache.fesod.excel.metadata.data.*; | ||||||||||
| import org.apache.fesod.excel.metadata.property.ExcelContentProperty; | ||||||||||
| import org.apache.fesod.excel.support.ExcelTypeEnum; | ||||||||||
| import org.apache.fesod.excel.util.DateUtils; | ||||||||||
| import org.apache.fesod.excel.util.FileTypeUtils; | ||||||||||
| import org.apache.fesod.excel.util.ListUtils; | ||||||||||
| import org.apache.fesod.excel.util.StyleUtil; | ||||||||||
| import org.apache.fesod.excel.util.WorkBookUtil; | ||||||||||
| import org.apache.fesod.excel.util.WriteHandlerUtils; | ||||||||||
| import org.apache.fesod.excel.util.*; | ||||||||||
|
||||||||||
| import org.apache.fesod.excel.util.*; | |
| import org.apache.fesod.excel.util.ListUtils; | |
| import org.apache.fesod.excel.util.StyleUtil; | |
| import org.apache.fesod.excel.util.WriteHandlerUtils; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is written in Chinese. For consistency with the rest of the codebase, documentation should be in English.