依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
工具类
package com.tedu.exceldeom.Util;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* @Auther: ziye
* @Date: 2020/2/14
* @Description:
*/
public class ExcelUtil {
/**
* 导出Excel
*
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null)
wb = new HSSFWorkbook();
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//声明列对象
HSSFCell cell = null;
//创建标题
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//创建内容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
模拟业务controller层
package com.tedu.exceldeom.controller;
import com.tedu.exceldeom.Util.ExcelUtil;
import com.tedu.exceldeom.pojo.User;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/export")
public class exportExcel {
@RequestMapping("/zz")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
String year = df.format(new Date());
//模拟查表
User user1 = new User(1L, "张三", 18, 2);
User user2 = new User(2L, "张三", 18, 2);
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
//设置Excel表头
String[] title = {"ID", "用户名称", "性别", "年龄"};
//设置Excel文件名
String filename = "UserFrom" + year + ".xls";
//设置工作表名称
String sheetName = "sheet1";
//开始对从数据库中获取到的数据进行处理
String[][] content = new String[list.size()][6];
try {
for (int i = 0; i < list.size(); i++) {
content[i][0] = String.valueOf(list.get(i).getId());
content[i][1] = list.get(i).getUserName();
if (list.get(i).getSex() == 0) {
content[i][2] = "女";
} else {
content[i][2] = "男";
}
content[i][3] = list.get(i).getAge() + "";
}
} catch (Exception e) {
e.printStackTrace();
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
try {
// 响应到客户端
this.setResponseHeader(response, filename);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}