java 后台 将数据导入excel 数据

controller

/**
 * 下载 地址 集合 获取 地址的详细信息
 */
@GetMapping("downLoadAddressHaveAndTxDataByAddress")
public void downLoadAddressHaveAndTxDataByAddress(HttpServletRequest request, HttpServletResponse response,String addressArrStr) {
  
Map<String,Integer> map = new HashMap<>();
List<AddressHoldTxStat> resultList = addressHoldTxStatDao.getInfoByAddress(toBeProcessedAddress);
    collectionsMintAddressService.fypclXls(map,request,response,resultList);
}

service:

public void fypclXls( Map<String,Integer> map,HttpServletRequest request, HttpServletResponse response, List<AddressHoldTxStat> resultList) {
    //初始化 最大值
    this.initCellMaxTextLength();
    //标题
    String[] title = {"交易地址","mint 数量","数据统计","最优 collection","持有mint 数量","持有 购买 数量","持有 转过来 数量","最优 collection 地板价",
            "交易 collections 数据统计","最优 交易 collection","最优 交易 collection 价格","交易 平均","交易盈利 数量","交易亏损 数量"};
    //xls表名
    String fileName = "mint_address_hold_and_tx.xlxs";
    String sheetName = "mint address 的持有和交易记录";

    Map<String, AddressHoldTxStat> mapObj = resultList.stream().collect(Collectors.toMap(AddressHoldTxStat::getAddress, a -> a,(k1, k2)->k1));

    String[][] content = new String[resultList.size()][title.length];
    int i = 0;
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        String address = entry.getKey();
        //把数据遍历添加到数组中
        AddressHoldTxStat obj = mapObj.get(address);
        content[i][0] = address;
        content[i][1] = entry.getValue()+"";
        content[i][2] = obj.getHoldContractStatData();
        content[i][3] = obj.getHoldBestContract();
        content[i][4] = obj.getHoldMintNumber()+"";
        content[i][5] = obj.getHoldBuyNumber()+"";
        content[i][6] = obj.getHoldTransferNumber()+"";
        content[i][7] =obj.getHoldBestFloorPrice();
        content[i][8] =obj.getTxContractStatData();
        content[i][9] =obj.getTxBestContract();
        content[i][10] =obj.getTxBestPrice();
        content[i][12] =obj.getTxAvgProfit()+"";
        content[i][12] =obj.getTxProfitNumber()+"";
        content[i][13] =obj.getTxLossNumber()+"";
        i++;
    }
    try {
        //使用方法得到api对象
        XSSFWorkbook hssfWorkbook = getXSSFWorkbook(sheetName, title, content, null);
        //实现页面下载
        setResponseHeader(request,response,fileName);
        //创建页面输出流对象
        ServletOutputStream outputStream = response.getOutputStream();
        //把文件写入输出流的对象中
        hssfWorkbook.write(outputStream);
        //outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

tools:

public XSSFWorkbook getXSSFWorkbook(String sheetName, String[] title, String[][] values, XSSFWorkbook workbook){
    //sheetName 表名称
    //title 表格第一行的表头名称
    //values 存放的内容
    //workbook 实现api的对象
    if (workbook == null){
        workbook = new XSSFWorkbook();
    }
    Sheet sheet = workbook.createSheet(sheetName);
    Row row = sheet.createRow(0);
    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    Cell cell = null;
    for (int i = 0; i < title.length; i++) {
        cell = row.createCell(i);
        cell.setCellValue(title[i]);
        cell.setCellStyle(cellStyle);
    }
    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 workbook;
}

public void setResponseHeader(HttpServletRequest request,HttpServletResponse response, String fileName)  {
    //fileName 文件名称
    try {
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        if(StringUtils.contains(agent, "Mozilla")){
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        }else {
            fileName = URLEncoder.encode(fileName, "utf8");
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");// 设置contentType为excel格式
        response.setHeader("Content-Disposition", "Attachment;Filename="+ fileName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

注意:导出excel 报错 。The maximum length of cell contents (text) is 32,767 characters,已经解决。 将 .xls 改成  .xlxs 文件。

猜你喜欢

转载自blog.csdn.net/qq_30346433/article/details/129742924