先准备poi需要的坐标:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
以下是核心代码:(每一行注释都很清晰哦~)
@Controller
@RequestMapping(value = "/report")
public class ReportController {
/**
* 导出excel报表
*/
@GetMapping(value = "/excel")
public void excel( HttpServletResponse response) throws Exception {
List<TestDataVo> list = datalist();
HSSFWorkbook workbook = null ;
BufferedOutputStream bufferedOutPut = null;
try {
workbook = new HSSFWorkbook();
// 创建页
HSSFSheet sheet = workbook.createSheet("Sheet1");
//设置列宽
sheet.setColumnWidth(0, 256*35);
sheet.setColumnWidth(1, 256*35);
sheet.setColumnWidth(2, 256*35);
sheet.setColumnWidth(3, 256*35);
sheet.setColumnWidth(4, 256*35);
// 创建行
HSSFRow firstrow = sheet.createRow(0);
// 创建列
HSSFCell cell0 = firstrow.createCell(0);
cell0.setCellStyle(getColumnTopStyle(workbook));
cell0.setCellValue("时间");
// 创建列
HSSFCell cell1 = firstrow.createCell(1);
cell1.setCellStyle(getColumnTopStyle(workbook));
cell1.setCellValue("姓名");
// 创建列
HSSFCell cell2 = firstrow.createCell(2);
cell2.setCellStyle(getColumnTopStyle(workbook));
cell2.setCellValue("单号");
// 创建列
HSSFCell cell3 = firstrow.createCell(3);
cell3.setCellStyle(getColumnTopStyle(workbook));
cell3.setCellValue("地址");
// 创建列
HSSFCell cell4 = firstrow.createCell(4);
cell4.setCellStyle(getColumnTopStyle(workbook));
cell4.setCellValue("用途");
for (TestDataVo testDataVo : list) {
HSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
//设置单元格的值,并且设置样式
HSSFCell cell00 = row.createCell(0);
cell00.setCellStyle(getStyle(workbook));
cell00.setCellValue(testDataVo.getDate());
//设置单元格的值,并且设置样式
HSSFCell cell01 = row.createCell(1);
cell01.setCellStyle(getStyle(workbook));
cell01.setCellValue(testDataVo.getName());
//设置单元格的值,并且设置样式
HSSFCell cell02 = row.createCell(2);
cell02.setCellStyle(getStyle(workbook));
cell02.setCellValue(testDataVo.getNumbers());
//设置单元格的值,并且设置样式
HSSFCell cell03 = row.createCell(3);
cell03.setCellStyle(getStyle(workbook));
cell03.setCellValue(testDataVo.getAddress());
//设置单元格的值,并且设置样式
HSSFCell cell04 = row.createCell(4);
cell04.setCellStyle(getStyle(workbook));
cell04.setCellValue(testDataVo.getPurpose());
}
String filename = "导出数据.xls";
response.setHeader("Content-Disposition",
"attachment;filename*=UTF-8''" + URLEncoder.encode(filename, "UTF-8"));
OutputStream outputStream = response.getOutputStream();
getColumnTopStyle(workbook);
bufferedOutPut = new BufferedOutputStream(outputStream);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
} finally {
if (bufferedOutPut != null)
bufferedOutPut.close();
if(workbook != null)
workbook.close();
}
}
下面是给excel设置表格+背景颜色
/*
* 列数据信息单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)12);
//字体加粗
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("宋体");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置背景颜色;
style.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
//solid 填充 foreground 前景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)14);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("黑体");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置背景颜色;
style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
//solid 填充 foreground 前景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}
如果大家不知道有哪些颜色
请参考:https://blog.csdn.net/qq_41234832/article/details/103061884
如果此文章对您有帮助,拜托留个赞再走哦~