maven依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
PoiUtils 工具類
public class PoiUtils {
public static ResponseEntity<byte[]> exportArticleExcel(List<ArticleVO> articles) {
HttpHeaders headers = null;
ByteArrayOutputStream baos = null;
try {
//1.創(chuàng)建Excel文檔
HSSFWorkbook workbook = new HSSFWorkbook();
//2.創(chuàng)建文檔摘要
workbook.createInformationProperties();
//3.獲取文檔信息张峰,并配置
DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation();
//3.1文檔類別
dsi.setCategory("文章信息");
//3.2設(shè)置文檔管理員
dsi.setManager("哨戒班");
//3.3設(shè)置組織機(jī)構(gòu)
dsi.setCompany("26");
//4.獲取摘要信息并配置
SummaryInformation si = workbook.getSummaryInformation();
//4.1設(shè)置文檔主題
si.setSubject("文章");
//4.2.設(shè)置文檔標(biāo)題
si.setTitle("首頁文章");
//4.3 設(shè)置文檔作者
si.setAuthor("26");
//4.4設(shè)置文檔備注
si.setComments("備注信息暫無");
//創(chuàng)建Excel表單
HSSFSheet sheet = workbook.createSheet("文章表");
//創(chuàng)建日期顯示格式
HSSFCellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
//創(chuàng)建標(biāo)題的顯示樣式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//定義列的寬度
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);
//5.設(shè)置表頭
HSSFRow headerRow = sheet.createRow(0);
HSSFCell cell0 = headerRow.createCell(0);
cell0.setCellValue("文章id");
cell0.setCellStyle(headerStyle);
HSSFCell cell1 = headerRow.createCell(1);
cell1.setCellValue("作者id");
cell1.setCellStyle(headerStyle);
HSSFCell cell2 = headerRow.createCell(2);
cell2.setCellValue("標(biāo)題");
cell2.setCellStyle(headerStyle);
HSSFCell cell3 = headerRow.createCell(3);
cell3.setCellValue("內(nèi)容");
cell3.setCellStyle(headerStyle);
HSSFCell cell4 = headerRow.createCell(4);
cell4.setCellValue("創(chuàng)建日期");
cell4.setCellStyle(headerStyle);
//6.裝數(shù)據(jù)
for (int i = 0; i < articles.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
ArticleVO article = articles.get(i);
row.createCell(0).setCellValue(article.getId());
row.createCell(1).setCellValue(article.getUId());
row.createCell(2).setCellValue(article.getTitle());
row.createCell(3).setCellValue(article.getContent());
HSSFCell CreateTimeCell = row.createCell(4);
CreateTimeCell.setCellValue(article.getCreateTime());
CreateTimeCell.setCellStyle(dateCellStyle);
headers = new HttpHeaders();
}
headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment",
new String("員工表.xls".getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
baos = new ByteArrayOutputStream();
workbook.write(baos);
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
}
}
Controller控制層
@RequestMapping(value = "/exportArticle", method = RequestMethod.GET)
public ResponseEntity<byte[]> exportEmp() {
return PoiUtils.exportArticleExcel(articleService.selectAll());
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者