栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java excle导出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java excle导出

pom 引入:

    
        org.apache.poi
        poi
        4.1.2
        
            
                commons-codec
                commons-codec
            
        
    
    
        org.apache.poi
        poi-ooxml
        4.1.2
    


 传入表单


@RequestMapping(“/exportPayOrderList”)
public void orderListExport(@RequestParam String exportParam
, HttpServletResponse response
, HttpServletRequest request) throws UnsupportedEncodingException {
System.out.println(“exportParam-------------------->>” + exportParam);
exportParam = URLDecoder.decode(exportParam, “utf-8”);
exportParam = exportParam.replaceAll(“””, “”“);
exportParam = exportParam.replaceAll(”,“, “,”);
exportParam = exportParam.replaceAll(”【“, “[”);
exportParam = exportParam.replaceAll(”】", “]”);

    BillVoPm billVoPm = JSONObject.parseObject(exportParam, BillVoPm.class);
    billManagePayService.exportOrderList(billVoPm, response, request);
}


void exportOrderList(BillVoPm hotelAdditionOrderVO
, HttpServletResponse response
, HttpServletRequest request) ;

//s实现层
@SneakyThrows
@Override
public void exportOrderList(BillVoPm mapValue, HttpServletResponse response, HttpServletRequest request) {
log.info("下载数据data={}—> ", mapValue.getMonthData());
if (Objects.isNull(mapValue)) {
throw new SecurityException(“请传入数据!”);
}
List orderList = mapValue.getMonthData();
String tempFileName = “账单信息@” + DateUtil.currentTime(DateUtil.YYYY_MM_DD_PATTERN) + “.xls”;
String filenameEncoder = URLDecoder.decode(tempFileName, “UTF-8”);
String agent = request.getHeader(“User-Agent”).toLowerCase();
response.setContentType(“application/x-download;charset=utf-8”);
if ((agent.contains(“safari”) || agent.contains(“iphone”)) && !agent.contains(“chrome”)){
//如果是苹果浏览器
log.info(“苹果浏览器”);
response.setHeader(“Content-Disposition”, “attachment; filename=” + new String(tempFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}else {
//其他浏览器
log.info(“其他浏览器”);
filenameEncoder = new String(tempFileName.getBytes(“UTF-8”), “ISO-8859-1”);
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“application/x-msdownload”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + filenameEncoder);
}
Workbook workbook = buildWordBook(orderList);
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (Exception e) {
log.info(“年账单列表失败----------->>{}”, e.getMessage());
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (Exception e) {
log.info(“关闭输出流失败----------->>{}”, e.getMessage());
}
}
if (null != workbook) {
try {
workbook.close();
} catch (Exception e) {
log.info(“关闭workbook流失败----------->>{}”, e.getMessage());
}
}
}
}

private Workbook buildWordBook(List orderList) {
    Workbook wb = new SXSSFWorkbook(10000);
    String sheetName = "账单列表";
    Sheet sh = wb.createSheet(sheetName);
    createTitleCell2(wb, sh);

    for (int rownum = 0; rownum < orderList.size(); rownum++) {
        Row row = sh.createRow(rownum + 3);
        // 月份
        String additionOrderId = orderList.get(rownum).getTime();
        Cell cell = row.createCell(0);
        cell.setCellValue(additionOrderId == null ? "" : additionOrderId);

        // 订单汇总
        String providerName = orderList.get(rownum).getCount();
        Cell cell1 = row.createCell(1);
        cell1.setCellValue(providerName == null ? "" : providerName);

        // 金额汇总
        String totalPrice = orderList.get(rownum).getFee();
        Cell cell2 = row.createCell(2);
        cell2.setCellValue(totalPrice == null ? "" : totalPrice);
    }

    return wb;
}

private void createTitleCell2(Workbook wb, Sheet sh) {
    List headers2 = new ArrayList() {{
        add("月份");
        add("订单汇总");
        add("金额汇总");
    }};
    ((SXSSFWorkbook) wb).setCompressTempFiles(true); //使用gzip压缩,减小空间占用
    //设置样式
    CellStyle style = wb.createCellStyle();
    CellStyle style1 = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER); // 居中
    style1.setWrapText(true);
    style1.setAlignment(HorizontalAlignment.CENTER);
    style1.setVerticalAlignment(VerticalAlignment.CENTER);
    //设置字体样式
    Font font = wb.createFont();

// font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 24);//设置字体大小
// 把字体应用到当前的样式
style.setFont(font);

    //设置表格标题
    Row rowTitle = sh.createRow(0);
    sh.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
    Cell titleCell = rowTitle.createCell(0);
    titleCell.setCellValue("账单数据");
    titleCell.setCellStyle(style);

    //设置每一列的宽度,注意 要乘以256,因为1代表excel中一个字符的1/256的宽度
    sh.setColumnWidth(0, 25 * 256);
    sh.setColumnWidth(1, 25 * 256);
    sh.setColumnWidth(2, 15 * 256);

    Row rowHeader = sh.createRow(1);
    // 设置表格标题
    for (int i = 0, j = headers2.size(); i < j; i++) {
        Cell cellHeader = rowHeader.createCell(i);
        cellHeader.setCellValue(headers2.get(i));
        cellHeader.setCellStyle(style1);
    }
    log.info("账单数据下载成功------");
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号