导出Excel是比较常用的功能,在此记录一下,省去了每次要去网上找代码的时间
1:首先看下数据库 order_info 只有简单的几个列
2:导入maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.6</version>
</dependency>
3:创建ExcelUtil工具类
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class ExcelUtil {
/**
* 导出Excel
*
* @param fileName 文件名
* @param sheetName sheet名称
* @param title 表头
* @param data 二维数组格式的数据
* @param response
*/
public static void getHSSFwb(String fileName, String sheetName, String[] title, String data[][], HttpServletResponse response) {
//创建文件、单个表格、行、单元格
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
//标题Style
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = wb.createFont(); // 生成一个字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setColor(HSSFColor.WHITE.index);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
//内容Style
HSSFCellStyle style2 = wb.createCellStyle();
style2.setFillForegroundColor(HSSFColor.WHITE.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont font2 = wb.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
style2.setFont(font2);
// 创建表头标题 第一行数据
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//填充数据 二维数组
for (int i = 0; i < data.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < data[i].length; j++) {
cell = row.createCell(j);
cell.setCellValue(data[i][j]);
cell.setCellStyle(style2);
}
}
//浏览器响应
OutputStream output;
try {
output = response.getOutputStream();
response.reset();
//这里如果文件名想要为中文就要fileName=new String(fileName.getBytes(),"ISO8859-1")
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setContentType("application/vnd.ms-excel");
wb.write(output);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4:这样就可以直接用了,看下Controller的代码吧
@RestController
public class ExcelController {
@Autowired
private OrderInfoService orderInfoService;
@RequestMapping("excel")
public void excel(HttpServletResponse response) {
//select * from order_info
List<OrderInfo> list = orderInfoService.getAllOrderInfo();
String[] title = {"订单Id", "内容", "创建时间", "订单状态"};
String fileName = "订单信息_" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls";
String sheetName = "订单信息";
//二维数组
String[][] data = new String[list.size()][title.length];
for (int i = 0; i < data.length; i++) {
OrderInfo oi = list.get(i);
data[i][0] = oi.getId().toString();
data[i][1] = oi.getContent();
data[i][2] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(oi.getCreateTime());
data[i][3] = oi.getStatus() == 1 ? "已完成" : "其它"; // Java 三元表达式
}
//工具类导出
ExcelUtil.getHSSFwb(fileName, sheetName, title, data, response);
}
}
5:浏览器输入你的controller访问路径,我这里的是 localhost:8080/excel,导出后效果: