1.ExportSelectXlsWorkBookUtils
import org.apache.poi.ss.usermodel.Cell;
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.xssf.streaming.SXSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import static org.springframework.util.StringUtils.capitalize;
/**
* 选择字段导出excel
* dataList:数据源
* exportFields:列名
* heards:字段名也就是excel首行
* excelName:sheet名
*/
public class ExportSelectXlsWorkBookUtils {
public static void getWorkBook(List<?> dataList, HttpServletResponse response, List<String> exportFields, List<String> heards, String excelName) {
// 创建工作簿对象
Workbook workbook = new SXSSFWorkbook();
// 创建工作表对象
Sheet sheet = workbook.createSheet(excelName);
//第一行 标题行
Row oneHeaderRow = sheet.createRow(0);
Cell oneHeaderCell = oneHeaderRow.createCell(0);
oneHeaderCell.setCellValue(excelName);
// 合并单元格,参数依次为起始行,结束行,起始列,结束列 (索引0开始)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, exportFields.size() - 1));//标题合并单元格操作,6为总列数
CellStyle oneCellStyle = workbook.createCellStyle();
oneCellStyle.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
oneCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
oneHeaderCell.setCellStyle(oneCellStyle);
// 创建字体对象
Font oneHeaderFont = workbook.createFont();
oneHeaderFont.setFontName("黑体");
oneHeaderFont.setFontHeightInPoints((short) 16);
// 将字体应用于单元格样式
oneCellStyle.setFont(oneHeaderFont);
// 应用样式到单元格
oneHeaderCell.setCellStyle(oneCellStyle);
// 第二行 小标题行
Row headerRow = sheet.createRow(1);
// 根据导出字段列表创建表头单元格
for (int i = 0; i < exportFields.size(); i++) {
Cell headerCell = headerRow.createCell(i);
headerCell.setCellValue(heards.get(i));
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
sheet.setColumnWidth(i, 256 * 25 + 184);//设置宽度 这里的25对应excel中的列宽
cellStyle.setWrapText(true);// 设置自动换行
headerCell.setCellStyle(cellStyle);
// 创建字体对象
Font headerFont = workbook.createFont();
headerFont.setFontName("黑体");
headerFont.setFontHeightInPoints((short) 14);
// 将字体应用于单元格样式
cellStyle.setFont(headerFont);
// 应用样式到单元格
headerCell.setCellStyle(cellStyle);
}
// 创建数据行
for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) {
Row dataRow = sheet.createRow(rowIndex + 2);//这里+2因为我已经给第一行设置了值
// 获取数据对象
Object dataObj = dataList.get(rowIndex);
// 遍历导出字段列表
for (int colIndex = 0; colIndex < exportFields.size(); colIndex++) {
String fieldName = exportFields.get(colIndex);
try {
// 根据字段名获取对应的 getter 方法
Method method = dataObj.getClass().getMethod("get" + capitalize(fieldName));
// 调用 getter 方法获取字段值
Object fieldValue = method.invoke(dataObj);
// 创建单元格并设置字段值
Cell dataCell = dataRow.createCell(colIndex);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
sheet.setColumnWidth(colIndex, 256 * 25 + 184);//设置宽度 这里的25对应excel中的列宽
dataCell.setCellStyle(cellStyle);
if (fieldValue instanceof String) {
dataCell.setCellValue((String) fieldValue);
} else if (fieldValue instanceof Number) {
dataCell.setCellValue(((Number) fieldValue).doubleValue());
} else if (fieldValue instanceof Date) {
dataCell.setCellValue((Date) fieldValue);
} else if (fieldValue instanceof Boolean) {
dataCell.setCellValue((Boolean) fieldValue);
} else {
dataCell.setCellValue(String.valueOf(fieldValue));
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
// 设置响应头信息
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ".xls");
try {
// 将工作簿写入响应输出流中
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.controller
/**
* 选择字段导出excel
* @param deviceProperties
* @param response
*/
@RequestMapping(value = "/exportSelectXls", method = RequestMethod.POST)
@ApiOperation(value = "硬件设备台账-选择字段导出excel", notes = "硬件设备台账-选择字段导出excel")
public void exportSelectXls(DeviceProperties deviceProperties, HttpServletResponse response){
devicePropertiesService.exportSelectXls(deviceProperties,response,deviceProperties.getExportFields(),deviceProperties.getHeards());
}
3.service
void exportSelectXls(DeviceProperties deviceProperties, HttpServletResponse response, List<String> exportFields, List<String> heards);
4.impl
// 从数据库中获取需要导出的数据
QueryWrapper query = new QueryWrapper(deviceProperties);
List<DeviceProperties> dataList = devicePropertiesService.list(query);
ExportSelectXlsWorkBookUtils.getWorkBook(dataList, response, exportFields, heards, "硬件设备");
}