✨ 毛利分析订单导出合并为单个工作表

This commit is contained in:
caojia
2026-11-17 15:48:57 +08:00
parent 8fb042f171
commit 5c8e905a37
3 changed files with 157 additions and 8 deletions
@@ -111,11 +111,12 @@ public class ExcelUtil<T>
*/
private Map<String, CellStyle> cellStyleCache = new HashMap<String, CellStyle>();
/**
* Excel sheet最大行数,默认65536
*/
/** 通用导出的默认每个工作表数据行数;单工作表 XLSX 导出使用 XLSX 实际上限。 */
public static final int sheetSize = 65536;
/** XLSX 工作表的最大行数。 */
public static final int XLSX_MAX_ROW_COUNT = 1_048_576;
/**
* 工作表名称
*/
@@ -796,6 +797,28 @@ public class ExcelUtil<T>
}
}
/** 写入单个 XLSX 工作表;超出 Excel 行数上限时返回明确错误。 */
public void exportExcelToSingleSheetStream(List<T> list, String sheetName, OutputStream output) throws IOException
{
try
{
this.init(list, sheetName, StringUtils.EMPTY, Type.EXPORT);
int reservedRows = rownum + 1 + (hasStatisticsColumns() ? 1 : 0);
int maxDataRows = XLSX_MAX_ROW_COUNT - reservedRows;
if (this.list.size() > maxDataRows)
{
throw new IOException(StringUtils.format("单个工作表最多支持{}条数据,当前{}条,请缩小查询范围",
maxDataRows, this.list.size()));
}
writeSheet(maxDataRows);
wb.write(output);
}
finally
{
IOUtils.closeQuietly(wb);
}
}
/** 对 list 数据源生成本地 Excel 文件,返回文件名。 */
public AjaxResult exportExcel()
{
@@ -824,9 +847,14 @@ public class ExcelUtil<T>
* 创建写入数据到Sheet
*/
public void writeSheet()
{
writeSheet(sheetSize);
}
private void writeSheet(int rowsPerSheet)
{
// 取出一共有多少个sheet.
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize));
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / rowsPerSheet));
for (int index = 0; index < sheetNo; index++)
{
createSheet(sheetNo, index);
@@ -855,7 +883,7 @@ public class ExcelUtil<T>
}
if (Type.EXPORT.equals(type))
{
fillExcelData(index);
fillExcelData(index, rowsPerSheet);
addStatisticsRow();
}
}
@@ -869,8 +897,13 @@ public class ExcelUtil<T>
@SuppressWarnings("unchecked")
public void fillExcelData(int index)
{
int startNo = index * sheetSize;
int endNo = Math.min(startNo + sheetSize, list.size());
fillExcelData(index, sheetSize);
}
private void fillExcelData(int index, int rowsPerSheet)
{
int startNo = index * rowsPerSheet;
int endNo = Math.min(startNo + rowsPerSheet, list.size());
int currentRowNum = rownum + 1; // 从标题行后开始
for (int i = startNo; i < endNo; i++)
@@ -931,6 +964,18 @@ public class ExcelUtil<T>
}
}
private boolean hasStatisticsColumns()
{
for (Object[] field : fields)
{
if (((Excel) field[1]).isStatistics())
{
return true;
}
}
return false;
}
/**
* 获取子列表最大数
*/