依赖
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>
代码实现细节
package com.lyh.mp.utils;import com.lyh.mp.entity.User;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;public class POIUtils {public static ResponseEntity<byte[]> student2Excel(List<User> list){//1. 创建一个 Excel 文档HSSFWorkbook workbook = new HSSFWorkbook();//2. 创建文档摘要workbook.createInformationProperties();//3. 获取并配置文档信息DocumentSummaryInformation docInfo = workbook.getDocumentSummaryInformation();//文档类别docInfo.setCategory("学生信息");//文档管理员docInfo.setManager("huanji");//设置公司信息docInfo.setCompany("www.huanji.org");//4. 获取文档摘要信息SummaryInformation summInfo = workbook.getSummaryInformation();//文档标题summInfo.setTitle("学生信息表");//文档作者summInfo.setAuthor("huanji");// 文档备注summInfo.setComments("本文档由 huanji 提供");//5. 创建样式//创建标题行的样式HSSFCellStyle headerStyle = workbook.createCellStyle();headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//创建日期单元格的样式HSSFCellStyle dateCellStyle = workbook.createCellStyle();dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));HSSFSheet sheet = workbook.createSheet("学生信息表");//设置列的宽度sheet.setColumnWidth(0, 5 * 256);sheet.setColumnWidth(1, 12 * 256);sheet.setColumnWidth(2, 10 * 256);sheet.setColumnWidth(3, 5 * 256);sheet.setColumnWidth(4, 12 * 256);sheet.setColumnWidth(5, 20 * 256);sheet.setColumnWidth(6, 10 * 256);sheet.setColumnWidth(7, 10 * 256);sheet.setColumnWidth(8, 16 * 256);sheet.setColumnWidth(9, 12 * 256);sheet.setColumnWidth(10, 15 * 256);sheet.setColumnWidth(11, 20 * 256);sheet.setColumnWidth(12, 16 * 256);sheet.setColumnWidth(13, 14 * 256);sheet.setColumnWidth(14, 14 * 256);sheet.setColumnWidth(15, 12 * 256);sheet.setColumnWidth(16, 8 * 256);sheet.setColumnWidth(17, 20 * 256);sheet.setColumnWidth(18, 20 * 256);sheet.setColumnWidth(19, 15 * 256);sheet.setColumnWidth(20, 8 * 256);sheet.setColumnWidth(21, 25 * 256);sheet.setColumnWidth(22, 14 * 256);sheet.setColumnWidth(23, 15 * 256);sheet.setColumnWidth(24, 15 * 256);//6.创建标题行HSSFRow r0 = sheet.createRow(0);HSSFCell c0 = r0.createCell(0);c0.setCellValue("ID");c0.setCellStyle(headerStyle);HSSFCell c1 = r0.createCell(1);c1.setCellStyle(headerStyle);c1.setCellValue("名字");HSSFCell c2 = r0.createCell(2);c2.setCellStyle(headerStyle);c2.setCellValue("年龄");HSSFCell c3 = r0.createCell(3);c3.setCellStyle(headerStyle);c3.setCellValue("email");HSSFCell c4 = r0.createCell(4);c4.setCellStyle(headerStyle);c4.setCellValue("创建时间");HSSFCell c5 = r0.createCell(5);c5.setCellStyle(headerStyle);c5.setCellValue("更新时间");for(int i = 0; i < list.size(); i++){User s = list.get(i);HSSFRow row = sheet.createRow(i + 1);row.createCell(0).setCellValue(s.getId());row.createCell(1).setCellValue(s.getName());row.createCell(2).setCellValue(s.getAge());row.createCell(3).setCellValue(s.getEmail());// 设置时间格式HSSFCellStyle cellStyle = workbook.createCellStyle();HSSFDataFormat dataFormat = workbook.createDataFormat();cellStyle.setDataFormat(dataFormat.getFormat("yyyy-MM-dd HH:mm:ss"));HSSFCell createTimeCell = row.createCell(4);createTimeCell.setCellValue(s.getCreateTime());createTimeCell.setCellStyle(cellStyle);HSSFCell updateTimeCell = row.createCell(5);updateTimeCell.setCellValue(s.getUpdateTime());updateTimeCell.setCellStyle(cellStyle);}// 将Excel数据写入字节数组ByteArrayOutputStream baos = new ByteArrayOutputStream();HttpHeaders headers = new HttpHeaders();try {//设置到处文件的文件名,病指定编码headers.setContentDispositionFormData("attachment", new String("学生信息表.xls".getBytes("UTF-8"), "ISO-8859-1"));headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);// 将Excel数据写入字节数组workbook.write(baos);} catch (IOException e) {e.printStackTrace();}// 返回包含Excel 数据字节数组的ResponseEntity对象return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);}}