This commit is contained in:
Looly 2023-05-29 11:45:35 +08:00
parent 6260734304
commit 99994a4d92
3 changed files with 47 additions and 44 deletions

View File

@ -12,8 +12,12 @@
package org.dromara.hutool.poi.excel;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.net.url.URLEncoder;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.poi.excel.cell.CellLocation;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.dromara.hutool.poi.excel.style.StyleUtil;
@ -30,6 +34,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.Closeable;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@ -511,6 +516,45 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return this.sheet instanceof XSSFSheet || this.sheet instanceof SXSSFSheet;
}
/**
* 获取Content-Type头对应的值可以通过调用以下方法快速设置下载Excel的头信息
*
* <pre>
* response.setContentType(excelWriter.getContentType());
* </pre>
*
* @return Content-Type值
* @since 5.6.7
*/
public String getContentType() {
return isXlsx() ? ExcelUtil.XLSX_CONTENT_TYPE : ExcelUtil.XLS_CONTENT_TYPE;
}
/**
* 获取Content-Disposition头对应的值可以通过调用以下方法快速设置下载Excel的头信息
*
* <pre>
* response.setHeader("Content-Disposition", excelWriter.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8));
* </pre>
*
* @param fileName 文件名如果文件名没有扩展名会自动按照生成Excel类型补齐扩展名如果提供空使用随机UUID
* @param charset 编码null则使用默认UTF-8编码
* @return Content-Disposition值
*/
public String getDisposition(String fileName, Charset charset) {
if (null == charset) {
charset = CharsetUtil.UTF_8;
}
if (StrUtil.isBlank(fileName)) {
// 未提供文件名使用随机UUID作为文件名
fileName = IdUtil.fastSimpleUUID();
}
fileName = StrUtil.addSuffixIfNot(URLEncoder.encodeAll(fileName, charset), isXlsx() ? ".xlsx" : ".xls");
return StrUtil.format("attachment; filename=\"{}\"", fileName);
}
/**
* 关闭工作簿<br>
* 如果用户设定了目标文件先写出目标文件后给关闭工作簿

View File

@ -462,7 +462,7 @@ public class ExcelUtil {
* @return 0-A; 1-B...26-AA
* @since 4.1.20
*/
public static String indexToColName(int index) {
public static String indexToColName(final int index) {
return CellLocationUtil.indexToColName(index);
}

View File

@ -100,8 +100,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
*/
private final AtomicInteger currentRow;
// -------------------------------------------------------------------------- Constructor start
// region Constructors
/**
* 构造默认生成xls格式的Excel文件<br>
* 此构造不传入写出的Excel文件路径只能调用{@link #flush(OutputStream)}方法写出到流<br>
@ -202,8 +201,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
this.styleSet = new StyleSet(workbook);
this.currentRow = new AtomicInteger(0);
}
// -------------------------------------------------------------------------- Constructor end
// endregion
/**
* 设置单元格值处理逻辑<br>
@ -384,45 +382,6 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this.currentRow.get();
}
/**
* 获取Content-Disposition头对应的值可以通过调用以下方法快速设置下载Excel的头信息
*
* <pre>
* response.setHeader("Content-Disposition", excelWriter.getDisposition("test.xlsx", CharsetUtil.CHARSET_UTF_8));
* </pre>
*
* @param fileName 文件名如果文件名没有扩展名会自动按照生成Excel类型补齐扩展名如果提供空使用随机UUID
* @param charset 编码null则使用默认UTF-8编码
* @return Content-Disposition值
*/
public String getDisposition(String fileName, Charset charset) {
if (null == charset) {
charset = CharsetUtil.UTF_8;
}
if (StrUtil.isBlank(fileName)) {
// 未提供文件名使用随机UUID作为文件名
fileName = IdUtil.fastSimpleUUID();
}
fileName = StrUtil.addSuffixIfNot(URLEncoder.encodeAll(fileName, charset), isXlsx() ? ".xlsx" : ".xls");
return StrUtil.format("attachment; filename=\"{}\"", fileName);
}
/**
* 获取Content-Type头对应的值可以通过调用以下方法快速设置下载Excel的头信息
*
* <pre>
* response.setContentType(excelWriter.getContentType());
* </pre>
*
* @return Content-Type值
* @since 5.6.7
*/
public String getContentType() {
return isXlsx() ? ExcelUtil.XLSX_CONTENT_TYPE : ExcelUtil.XLS_CONTENT_TYPE;
}
/**
* 设置当前所在行
*