增加ExcelWriter.addIgnoredErrors,支持忽略警告小标

This commit is contained in:
Looly 2024-05-08 16:57:15 +08:00
parent f8dbf66bc9
commit bee80f3d14
3 changed files with 54 additions and 13 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.28(2024-05-06)
# 5.8.28(2024-05-08)
### 🐣新特性
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释issue#I9CPC7@Gitee
@ -15,6 +15,7 @@
* 【captcha】 Captcha.setBackground为null时背景透明issue#3558@Github
* 【captcha】 HttpDownloader.downloadBytes增加超时参数重载issue#3556@Github
* 【http 】 增加ExceptionFilter和DefaultExceptionFilter支持异常处理issue#3568@Github
* 【poi 】 增加ExcelWriter.addIgnoredErrors支持忽略警告小标
### 🐞Bug修复
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题issue#3536@Github

View File

@ -20,20 +20,11 @@ import cn.hutool.poi.excel.cell.CellLocation;
import cn.hutool.poi.excel.cell.CellUtil;
import cn.hutool.poi.excel.style.Align;
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
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.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Row;
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.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.io.File;
import java.io.IOException;
@ -591,6 +582,26 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this;
}
/**
* 设置忽略错误即Excel中的绿色警告小标只支持XSSFSheet<br>
* https://stackoverflow.com/questions/23488221/how-to-remove-warning-in-excel-using-apache-poi-in-java
*
* @param cellRangeAddress 指定单元格范围
* @param ignoredErrorTypes 忽略的错误类型列表
* @return this
* @throws UnsupportedOperationException 如果sheet不是XSSFSheet
* @since 5.8.28
*/
public ExcelWriter addIgnoredErrors(final CellRangeAddress cellRangeAddress, final IgnoredErrorType... ignoredErrorTypes) throws UnsupportedOperationException {
final Sheet sheet = this.sheet;
if (sheet instanceof XSSFSheet) {
((XSSFSheet) sheet).addIgnoredErrors(cellRangeAddress, ignoredErrorTypes);
return this;
}
throw new UnsupportedOperationException("Only XSSFSheet supports addIgnoredErrors");
}
/**
* 增加下拉列表
*

View File

@ -0,0 +1,29 @@
package cn.hutool.poi.excel;
import cn.hutool.core.collection.ListUtil;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.junit.Ignore;
import org.junit.Test;
public class WriteNumberToStringTest {
@Test
@Ignore
public void writeNumberTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/dataWithNumber.xlsx");
final XSSFSheet sheet = (XSSFSheet) writer.getSheet();
sheet.addIgnoredErrors(new CellRangeAddress(0, 100, 0, 100), IgnoredErrorType.NUMBER_STORED_AS_TEXT);
final CellStyle cellStyle = writer.getStyleSet().getCellStyle();
cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("TEXT"));
writer.writeRow(ListUtil.of("姓名", "编号"));
writer.writeRow(ListUtil.of("张三", "010001"));
writer.writeRow(ListUtil.of("李四", "120001"));
writer.writeRow(ListUtil.of("王五", 123456));
writer.close();
}
}