spring-boot導出excel

spring-boot導出excel

本文使用Apache POL實現excel文檔的導出java

pom.xml 添加Apache POL依賴

<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> 

導出excel

導出excel比較重要的api有如下幾個,其餘的並沒有難點web

  • 建立一個excel文件工做薄;(HSSFWorkbook workbook = new HSSFWorkbook())
  • 建立一張表;HSSFSheet sheet = workbook.createSheet("統計表")
  • 建立一行;HSSFRow row = sheet.createRow(0)
  • 填充一列數據; row.createCell(0).setCellValue("數據")
  • 設置一個單元格樣式;cell.setCellStyle(style)

 

package com.example.demo.controller; import com.example.demo.domain.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; @RestController @RequestMapping(value = "/excel") public class ExcelController { @Autowired private UserService userService; //建立表頭 private void createTitle(HSSFWorkbook workbook,HSSFSheet sheet){ HSSFRow row = sheet.createRow(0); //設置列寬,setColumnWidth的第二個參數要乘以256,這個參數的單位是1/256個字符寬度 sheet.setColumnWidth(1,12*256); sheet.setColumnWidth(3,17*256); //設置爲居中加粗 HSSFCellStyle style = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setBold(true); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setFont(font); HSSFCell cell; cell = row.createCell(0); cell.setCellValue("ID"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("顯示名"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("用戶名"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("建立時間"); cell.setCellStyle(style); } //生成user表excel @GetMapping(value = "/getUser") public String getUser(HttpServletResponse response) throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("統計表"); createTitle(workbook,sheet); List<User> rows = userService.getAll(); //設置日期格式 HSSFCellStyle style = workbook.createCellStyle(); style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); //新增數據行,而且設置單元格數據 int rowNum=1; for(User user:rows){ HSSFRow row = sheet.createRow(rowNum); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getUsername()); HSSFCell cell = row.createCell(3); cell.setCellValue(user.getCreate_time()); cell.setCellStyle(style); rowNum++; } String fileName = "導出excel例子.xls"; //生成excel文件 buildExcelFile(fileName, workbook); //瀏覽器下載excel buildExcelDocument(fileName,workbook,response); return "download excel"; } //生成excel文件 protected void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{ FileOutputStream fos = new FileOutputStream(filename); workbook.write(fos); fos.flush(); fos.close(); } //瀏覽器下載excel protected void buildExcelDocument(String filename,HSSFWorkbook workbook,HttpServletResponse response) throws Exception{ response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8")); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } }轉載地址:https://my.oschina.net/tongjh/blog/1600000
相關文章
相關標籤/搜索