Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

/**
* 拦截器中单元格上下文可以获取到行数据
Copy link

Copilot AI Sep 30, 2025

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.

Suggested change
* 拦截器中单元格上下文可以获取到行数据
* The cell context in the interceptor can obtain the row data.

Copilot uses AI. Check for mistakes.
*/
@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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge impacting performance and security part on write record with info level.

}
}
}
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;

/**
* 通过自定义转换器转换
Copy link

Copilot AI Sep 30, 2025

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.

Suggested change
* 通过自定义转换器转换
* Converted by a custom converter

Copilot uses AI. Check for mistakes.
*/
@ExcelProperty(converter = RecordStringStringConverter.class)
private String text;
}
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();
}

/**
* 这里是写的时候会可以访问到原始数据,你可以进行其他业务处理,然后进行转换
Copy link

Copilot AI Sep 30, 2025

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.

Suggested change
* 这里是写的时候会可以访问到原始数据你可以进行其他业务处理然后进行转换
* When writing, you can access the original data here. You can perform other business processing before conversion.

Copilot uses AI. Check for mistakes.
*/
@Override
public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
// 获取原始数据
RecordData record = context.getRecord();
log.info("原始数据:{}", record);
return new WriteCellData<>("自定义:" + context.getValue() + "-" + record.getDoubleData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wildcard imports reduces code readability and can lead to naming conflicts. Consider using explicit imports instead.

Suggested change
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;

Copilot uses AI. Check for mistakes.
import org.apache.fesod.excel.util.BooleanUtils;
import org.apache.fesod.excel.util.FileUtils;
import org.apache.fesod.excel.util.ListUtils;
Expand All @@ -58,16 +45,16 @@
import org.apache.fesod.excel.write.metadata.style.WriteFont;
import org.apache.fesod.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.fesod.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.*;

/**
* 写的常见写法
*
Expand Down Expand Up @@ -711,6 +698,15 @@ public void customHandlerWrite() {
.doWrite(data());
}

@Test
public void recordWrite() {
String fileName = TestFileUtil.getPath() + "recordWrite" + System.currentTimeMillis() + ".xlsx";
FastExcel.write(fileName, RecordData.class)
.registerWriteHandler(new RecordCellWriteHandler())
.sheet("模板")
.doWrite(records());
}

/**
* 插入批注
* <p>
Expand Down Expand Up @@ -844,6 +840,19 @@ private List<DemoData> data() {
return list;
}

private List<RecordData> records() {
List<RecordData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
RecordData data = new RecordData();
data.setString("STRING" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
data.setText("record-" + i);
list.add(data);
}
return list;
}

private List<DemoData> dataHex() {
List<DemoData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Copy link
Member

Choose a reason for hiding this comment

The 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;

Expand All @@ -44,6 +40,11 @@ public class WriteConverterContext<T> {
*/
private T value;

/**
* Java row level record
*/
private Object record;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about generics?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

采用泛型对已存在的代码改动比较多,我可以增加get方法进行泛型输出,这样改动比较少。在使用行级对象的时候不需要进行强制类型转换。


/**
* Content property.Nullable.
*/
Expand All @@ -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
Expand Up @@ -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;
Expand All @@ -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.*;
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wildcard imports reduces code readability and can lead to naming conflicts. Consider using explicit imports instead.

Suggested change
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;

Copilot uses AI. Check for mistakes.
import org.apache.fesod.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;

import java.util.List;

/**
* Excel write Executor
*
Expand Down Expand Up @@ -361,7 +346,7 @@ private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerConte
try {
cellData = ((Converter<Object>) converter)
.convertToExcelData(new WriteConverterContext<>(
cellWriteHandlerContext.getOriginalValue(), excelContentProperty, writeContext));
cellWriteHandlerContext.getOriginalValue(), cellWriteHandlerContext.getOriginalRecord(), excelContentProperty, writeContext));
} catch (Exception e) {
throw new ExcelWriteDataConvertException(
cellWriteHandlerContext,
Expand Down
Loading