重要方法
bigWriter.setSheet(sheet.getSheetName());
工具类
public class HuExcelUtils {
public static void exportExcel(HttpServletResponse response, List<SheetDTO> sheetList, String fileName) {
ExcelWriter bigWriter = ExcelUtil.getBigWriter();
bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
for (SheetDTO sheet : sheetList) {
bigWriter.setSheet(sheet.getSheetName());
Integer[] columnWidth = sheet.getColumnWidth();
if (columnWidth == null || columnWidth.length != sheet.getFieldAndAlias().size()) {
for (int i = 0; i < sheet.getFieldAndAlias().size(); i++) {
bigWriter.setColumnWidth(i, 25);
}
} else {
for (int i = 0; i < columnWidth.length; i++) {
bigWriter.setColumnWidth(i, columnWidth[i]);
}
}
bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
bigWriter.setOnlyAlias(true);
bigWriter.setDefaultRowHeight(18);
bigWriter.setFreezePane(1);
bigWriter.write(sheet.getCollection(), true);
}
ServletOutputStream out = null;
try {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" +
URLEncoder.encode(fileName + DateUtil.today() + ".xlsx", "UTF-8"));
out = response.getOutputStream();
bigWriter.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
bigWriter.close();
}
IoUtil.close(out);
}
}
public class SheetDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String sheetName;
private Map<String, String> fieldAndAlias;
private List<Integer> columnWidth;
private Collection<?> collection;
public SheetDTO() {
}
public SheetDTO(String sheetName, Map<String, String> fieldAndAlias, Collection<?> collection) {
super();
this.sheetName = sheetName;
this.fieldAndAlias = fieldAndAlias;
this.collection = collection;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public Map<String, String> getFieldAndAlias() {
return fieldAndAlias;
}
public void setFieldAndAlias(Map<String, String> fieldAndAlias) {
this.fieldAndAlias = fieldAndAlias;
}
public List<Integer> getColumnWidth() {
return this.columnWidth;
}
public void setColumnWidth(List<Integer> columnWidth) {
this.columnWidth = columnWidth;
}
public Collection<?> getCollection() {
return collection;
}
public void setCollection(Collection<?> collection) {
this.collection = collection;
}
}
public Class TestController {
@ResponseBody
@RequestMapping("/export_all")
public void export_all(HttpServletResponse response) {
List<Map<String, Object>> listData1 = xxxService.listData1();
List<Map<String, Object>> listData2 = xxxService.listData2();
List<Map<String, Object>> listData3 = xxxService.listData3();
Map<String, String> map1 = new LinkedHashMap<String, String>();
map1.put("store_name", "客户名称");
map1.put("store_out_trade_no", "客户编码");
map1.put("store_contract_year", "年份");
map1.put("business_dept_name", "所属事业部");
Map<String, String> map2 = new LinkedHashMap<String, String>();
map2.put("store_name", "客户名称");
map2.put("store_out_trade_out", "客户编码");
map2.put("store_contract_year", "年份");
map2.put("store_name", "关联客户名称");
map2.put("store_out_trade_out", "关联客户编码");
Map<String, String> map3 = new LinkedHashMap<String, String>();
map3.put("store_name", "客户名称");
map3.put("store_out_trade_out", "客户编码");
map3.put("store_contract_year", "年份");
map3.put("name", "重要负责人姓名");
map3.put("position", "重要负责人职位");
List<SheetDTO> arrayList = new ArrayList<SheetDTO> ();
arrayList.add(new SheetDTO("客户信息", map1, listData1));
arrayList.add(new SheetDTO("关联客户信息", map2, listData2));
arrayList.add(new SheetDTO("重要负责人信息", map3, listData3));
HuExcelUtils.exportExcel(response, arrayList, "客户信息导出");
}
}
java解决poi导出excel使用SXSSF时“Attempting to write a row[?] in the range [0,?]that is already written to disk.”异常
package com.***;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bison.tracecode.vo.WineMonthExcelVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ExcelExportUtil {
public static void setSizeColumn(Sheet sheet, int size) {
for (int columnNum = 0; columnNum <= size; columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row currentRow;
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
Cell currentCell = currentRow.getCell(columnNum);
int cellType = currentCell.getCellType();
int code = CellType.STRING.getCode();
if (cellType == code) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 256);
}
}
public static void exportExcel(HttpServletResponse response, List<WineMonthExcelVO> sheetList, String fileName) {
ExcelWriter bigWriter = ExcelUtil.getWriter();
bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
for (WineMonthExcelVO sheet : sheetList) {
bigWriter.setSheet(sheet.getSheetName());
bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
bigWriter.setOnlyAlias(true);
bigWriter.setFreezePane(1);
bigWriter.write(sheet.getCollection(), true);
setSizeColumn(bigWriter.getSheet(), sheet.getCollection().size());
}
ServletOutputStream out = null;
try {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" +
URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
out = response.getOutputStream();
bigWriter.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
bigWriter.close();
}
IoUtil.close(out);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?