导出excel,前后台代码示例
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                导出excel,前后台代码示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            ExcelUtil工具類:
package com.leigod.NNN.base.util;import org.apache.poi.hssf.usermodel.*;import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;public class exportExcelUtil {//創建表頭public 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("訂單編號");cell.setCellStyle(style);cell = row.createCell(1);cell.setCellValue("昵稱");cell.setCellStyle(style);cell = row.createCell(2);cell.setCellValue("ID");cell.setCellStyle(style);cell = row.createCell(3);cell.setCellValue("類型");cell.setCellStyle(style);cell = row.createCell(4);cell.setCellValue("單價");cell.setCellStyle(style);cell = row.createCell(5);cell.setCellValue("數量");cell.setCellStyle(style);cell = row.createCell(6);cell.setCellValue("訂單金額");cell.setCellStyle(style);cell = row.createCell(7);cell.setCellValue("實付金額");cell.setCellStyle(style);cell = row.createCell(8);cell.setCellValue("下單用戶");cell.setCellStyle(style);cell = row.createCell(9);cell.setCellValue("下單用戶ID");cell.setCellStyle(style);cell = row.createCell(10);cell.setCellValue("訂單狀態");cell.setCellStyle(style);cell = row.createCell(11);cell.setCellValue("訂單生成時間");cell.setCellStyle(style);cell = row.createCell(12);cell.setCellValue("訂單支付時間");cell.setCellStyle(style);cell = row.createCell(13);cell.setCellValue("接單時間");cell.setCellStyle(style);cell = row.createCell(14);cell.setCellValue("訂單取消時間");cell.setCellStyle(style);cell = row.createCell(15);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);//瀏覽器下載excelbuildExcelDocument(fileName,workbook,response);return "download excel";}*///生成excel文件public void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{FileOutputStream fos = new FileOutputStream(filename);workbook.write(fos);fos.flush();fos.close();}//瀏覽器下載excelpublic 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();}} 
                        
                        
                        后臺controller:
?
@ApiOperation(value = "導出excel")@RequestMapping(value = "/exportExcel", method = RequestMethod.GET, produces = "application/json;charset=utf-8")public Object exportExcel(HttpServletResponse response) throws Exception{exportExcelUtil exportexcelUtil = new exportExcelUtil();HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("訂單表");exportexcelUtil.createTitle(workbook,sheet);List<OrderObj> list = orderMemberService.getOrderList(3, TransitionUtil.orderPlayerStatus(0), 0, null, null, "");//設置日期格式HSSFCellStyle style = workbook.createCellStyle();style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));//新增數據行,并且設置單元格數據SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");int rowNum=1;for(OrderObj orderObj:list){HSSFRow row = sheet.createRow(rowNum);if(!StringUtils.isEmpty(orderObj.getOrderNo())){row.createCell(0).setCellValue(orderObj.getOrderNo());}if(!StringUtils.isEmpty(orderObj.getGoodsName())){row.createCell(1).setCellValue(orderObj.getGoodsName());}if(!StringUtils.isEmpty(orderObj.getGoodsId())){row.createCell(2).setCellValue(orderObj.getGoodsId());}if(!StringUtils.isEmpty(orderObj.getGoodsSpecifica())){row.createCell(3).setCellValue(orderObj.getGoodsSpecifica());}if(!StringUtils.isEmpty(orderObj.getOrderPrice())){row.createCell(4).setCellValue(orderObj.getOrderPrice().toString());}if(!StringUtils.isEmpty(orderObj.getOrderNumber())){row.createCell(5).setCellValue(orderObj.getOrderNumber());}if(!StringUtils.isEmpty(orderObj.getOrderAmount())){row.createCell(6).setCellValue(orderObj.getOrderAmount().toString());}if(!StringUtils.isEmpty(orderObj.getPay())){row.createCell(7).setCellValue(orderObj.getPay().getPayAmount().toString());}if(!StringUtils.isEmpty(orderObj.getMemberNickName())){row.createCell(8).setCellValue(orderObj.getMemberNickName());}if(!StringUtils.isEmpty(orderObj.getStatusType())){row.createCell(9).setCellValue(orderObj.getMemberNo());}if(!StringUtils.isEmpty(orderObj.getStatusType())){row.createCell(10).setCellValue(orderObj.getStatusType());}if(!StringUtils.isEmpty(orderObj.getOrderTime())){row.createCell(11).setCellValue(sdf.format(orderObj.getOrderTime()));}if(!StringUtils.isEmpty(orderObj.getPaymentTime())){row.createCell(12).setCellValue(sdf.format(orderObj.getPaymentTime()));}if(!StringUtils.isEmpty(orderObj.getReceiptTime())){row.createCell(13).setCellValue(sdf.format(orderObj.getReceiptTime()));}if(!StringUtils.isEmpty(orderObj.getPaymentTime())){row.createCell(14).setCellValue(sdf.format(orderObj.getPaymentTime()));}if(!StringUtils.isEmpty(orderObj.getReceiptTime())){row.createCell(15).setCellValue(sdf.format(orderObj.getReceiptTime()));}}elementUI,導出的事件:
exportExcel(){exportExcelAPI(this.formInline).then(res => {if(res.size > 0){const content = resconst blob = new Blob([content])const fileName = '訂單表.xlsx'if ('download' in document.createElement('a')) { // 非IE下載const elink = document.createElement('a')elink.download = fileNameelink.style.display = 'none'elink.href = URL.createObjectURL(blob)document.body.appendChild(elink)elink.click()URL.revokeObjectURL(elink.href) // 釋放URL 對象document.body.removeChild(elink)} else { // IE10+下載navigator.msSaveBlob(blob, fileName)}/* this.$message.success("導出成功")*/}}).catch(() => {}); },?
總結
以上是生活随笔為你收集整理的导出excel,前后台代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 基于C++的OpenCV(八)图像处理
- 下一篇: 没有经验该如何转行做产品经理?
