diff --git a/CHANGELOG.md b/CHANGELOG.md index 8484ad0e4..6ffba8a9c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index 8a24bcc65..ba801c65f 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -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 { return this; } + /** + * 设置忽略错误,即Excel中的绿色警告小标,只支持XSSFSheet
+ * 见: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"); + } + /** * 增加下拉列表 * diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/WriteNumberToStringTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/WriteNumberToStringTest.java new file mode 100644 index 000000000..7f6217e2e --- /dev/null +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/WriteNumberToStringTest.java @@ -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(); + } +}