poi 5.2 導出
如果能給你帶來幫助,不勝榮幸,如果有錯誤也請批評指正。
1:maven 依賴,現在好多都是用的poi 3.6 和 poi3.9 的jar,項目升級了,現在得用5.x的了,所以就用5.x的給大家簡單的演示,盡可能找依賴相同的,因為不同版本里面的方法什么的會有變化
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
2:后臺(網上后臺太多了,全是寫后臺bug的,我就不多說了,我就用最慫的一種給大家展示,僅做展示)
①網上隨便扒一個工具類,大家可以看一下里面的注釋,個別地方說了一下廢話
import org.apache.poi.hssf.usermodel.*; /** * excel導出工具 * * @author Tomas·Red * @since */ public class ExcelExportUtil { /** * 導出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文件
// 你創建 HSSFWorkbook 就是代表創建 .xls(2003)最大導出行數為:65536;創建XSSFWorkbook 代表創建.xlsx(2007)最大行數為:1048576 至于用什么自己定義
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); // 樣式我給隱藏了,因為5.2的poi,不支持這樣寫了,我沒去查,后期查了之后再修改吧 //聲明列對象 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; } }
②業務層或者控制層代碼
@RequestMapping("/handExprot")
public Object handExprot(HttpServletResponse response, WellEquipmentParam wellEquipmentParam){
// 導出的數據
List<Well> list = (List<Well>) mainBusiness.findAll();
//excel標頭
String[] title = {"title1", "title2", "title3", "title4", "title5", "title6", "title7", "title8"};
//excel文件名
String fileName = "活動報名信息表" + System.currentTimeMillis() + ".xls";
//sheet名
String sheetName = "活動報名信息表";
int size = list.size();
String [][] content = new String[size][8];
// 循環給單元格賦值
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
Well well = list.get(i);
content[i][0] = well.getId() + "";
content[i][1] = well.getWellName() + "";
content[i][2] = well.getWellCode() + "";
content[i][3] = well.getWellType() + "";
content[i][4] = well.getLatitude() + "";
content[i][5] = well.getLongitude() + "";
content[i][6] = well.getWellDepth() + "";
content[i][7] = well.getWellAddress() + "";
}
//創建HSSFWorkbook
HSSFWorkbook wb = ExcelExportUtil.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();
}
return null;
}
//發送響應流方法 public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } }
到這里后臺的方法已經全部寫完了。剩下的就是前臺了,網上搜的很多都是沒有前臺的,沒有前臺你寫個der啊,老衲就是前臺不會,后來自己東拼西湊了一個前臺,大家湊合著看吧
export function handExprot(data) { let consturl=process.env.SERVER_API + "/WellController/handExprot" + "?" + qs.stringify(data, { indices: false }); var xhr = new XMLHttpRequest(); console.log({consturl}) xhr.open("get", consturl, true); // get、post都可 xhr.responseType = "blob"; // 轉換流 xhr.setRequestHeader("token",window.localStorage.getItem("token")); //加請求頭 xhr.onload = function() { // debugger if (this.status == 200) { var blob = this.response; var a = document.createElement("a") var url = window.URL.createObjectURL(blob) a.href = url a.download = "導出文件.xls" // } a.click() window.URL.revokeObjectURL(url) } xhr.send(); }
到這里就完了,后臺有很多大家可以取隨便抄,但是前臺不能單純的ajax去請求。要注意屬性

浙公網安備 33010602011771號