技术分享

浏览器导出excel文件

2019-09-30  本文已影响0人  星可码农

浏览器导出excel文件

1.包的引入

  <dependency>
 
     <groupId>org.apache.poi</groupId>

        <artifactId>poi</artifactId>

        <version>3.9</version>

    </dependency>

2、代码如下

       public void findCreawPrint(HttpServletRequest request, HttpServletResponse response, 
              ZmdrawPayLogVo zmdrawPayLogVo) throws Exception {
            String reportDate = DateFormatUtil.format_Ymd(new Date());
            String fileName = System.currentTimeMillis() + "-" + reportDate + ".xls";
    
            try {
                response.setHeader("Content-Disposition", "attachment; filename=".concat(fileName));
                response.setHeader("Content-Type", "application/msexcel");
                OutputStream ouputStream = response.getOutputStream();
                /*第一页*/
                //每个sheet数据条数
                Integer pageSize = 1000;
                zmdrawPayLogVo.setPageSize(pageSize);
                Integer total = iZmcoinPayLogService.findCreawTotal(zmdrawPayLogVo);
                int page = total / pageSize + 1;
                HSSFWorkbook workbook = new HSSFWorkbook();
                for (int i = 1; i < page; i++) {
                    List<List<String>> data0 = new ArrayList<>();
                    zmdrawPayLogVo.setPageNo(i);
                    zmdrawPayLogVo.setPageSize(pageSize);
                    ResultVo<List<DrawPayLogVo>> resultVo = 
     iZmcoinPayLogService.findCreawPrint(zmdrawPayLogVo);
                    for (DrawPayLogVo dto : resultVo.getData()) {
                        List rowData = new ArrayList();
                        rowData.add(dto.getTradeNo());
                        rowData.add(dto.getPayeeName());
                        rowData.add(dto.getMobile());
                        rowData.add(dto.getAmount());
                        rowData.add(dto.getBalance());
                        rowData.add(dto.getOrderDetail());
                        rowData.add(DateFormatUtil.formatY_m_dhms(dto.getCreateTime()));
                        data0.add(rowData);
                    }
                    String[] headers0 = {"订单号", "昵称", "手机号", "提现金额", "账户余额", "提现详情", "提现时间"};
                    ExportExcelUtils eeu = new ExportExcelUtils();
                    eeu.exportExcel(workbook, i - 1, "提现流水" + i, headers0, data0, ouputStream);
                }
                response.flushBuffer();
                //原理就是将所有的数据一起写入,然后再关闭输入流。
                workbook.write(ouputStream);
                ouputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        
      其中工具类:ExportExcelUtils
    package com.rt.platform.infosys.admin.common.excel;
    
    import org.apache.poi.hssf.usermodel.*;
    
    import java.io.OutputStream;
    import java.util.List;
    
    /**
     * @author : hekk
     * @title :  导出excel工具
     * @description :
     * @company : 润投科技
     * @time: 2019/7/23 9:43
     */
    public class ExportExcelUtils {
        /**
         * @param workbook
         * @param sheetNum   (sheet的位置,0表示第一个表格中的第一个sheet)
         * @param sheetTitle (sheet的名称)
         * @param headers    (表格的列标题)
         * @param result     (表格的数据)
         * @param out        (输出流)
         * @throws Exception
         * @Description: 导出Excel
         */
        public void exportExcel(HSSFWorkbook workbook, int sheetNum,
                                String sheetTitle, String[] headers, List<List<String>> result,
                                OutputStream out) throws Exception {
            // 生成一个表格
            HSSFSheet sheet = workbook.createSheet();
            workbook.setSheetName(sheetNum, sheetTitle);
            // 设置表格默认列宽度为20个字节
            sheet.setDefaultColumnWidth((short) 30);
            sheet.autoSizeColumn(5, true);
            sheet.autoSizeColumn(6, true);
            // 生成一个样式
            HSSFCellStyle style = workbook.createCellStyle();
            // 生成一个字体
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 12);
            // 把字体应用到当前的样式
            style.setFont(font);
    
            // 指定当单元格内容显示不下时自动换行
            style.setWrapText(true);
    
            // 产生表格标题行
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell((short) i);
    
                cell.setCellStyle(style);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text.toString());
            }
            // 遍历集合数据,产生数据行
            if (result != null) {
                int index = 1;
                for (List<String> m : result) {
                    row = sheet.createRow(index);
                    int cellIndex = 0;
                    for (String str : m) {
                        HSSFCell cell = row.createCell((short) cellIndex);
                        cell.setCellValue(str.toString());
                        cellIndex++;
                    }
                    index++;
                }
            }
        }
    }
...
3.前端请求最好是用a标签发起

注意:浏览器生成的文件名中文不显示
上一篇下一篇

猜你喜欢

热点阅读