zoukankan      html  css  js  c++  java
  • 导出

    private void Export2Excel(String[] heads, String[] names,
                JSONArray jsonArray, HttpServletResponse response) {
    
            @SuppressWarnings("resource")
            Workbook workbook = new XSSFWorkbook();// 创建一个Excel文件
            
            Sheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
    
            CellStyle style = workbook.createCellStyle();
    //        HSSFColor
            style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
    
            Row titleRow = sheet.createRow(0);
    
            for (int i = 0; i < heads.length; i++) {
                titleRow.createCell(i).setCellValue(heads[i]);
            }
    
            titleRow.setRowStyle(style);
    
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    Row row = sheet.createRow(i + 1);
                    JSONObject json = jsonArray.getJSONObject(i); // 遍历 jsonarray
                    for (int j = 0; j < names.length; j++) {
    //                    instanceof 
                        Object temp=json.get(names[j]);
                        if(temp instanceof Double){
                            logger.info("==double:"+temp.toString());
                            row.createCell(j).setCellValue(
                                    Double.parseDouble(temp.toString()));
                        }else{
                            row.createCell(j).setCellValue(
                                    json.get(names[j]).toString());
                        }
                        
    
                    }
    
                }
            }
    
            try {
                String mimetype = "application/x-msdownload";
                response.setContentType(mimetype);
                String downFileName = URLEncoder.encode("充电审计结果.xlsx", "UTF-8");
                String inlineType = "attachment"; // 是否内联附件
                response.setHeader("Content-Disposition", inlineType
                        + ";filename="" + downFileName + """);
                
                OutputStream out = response.getOutputStream();
                workbook.write(out);
                out.flush();        
                out.close();
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        
    
        
    学习的时间不一定要特定安排
  • 相关阅读:
    java Object类是可以接收集合类型的
    java.lang.String中[ "张飞"+1+1 ] 和 [ "张飞"+(1+1) ]
    AFL Fuzz入门
    [转载]linux与grep
    linux下安装clamav
    [转载]Linux连续执行多条命令
    [转载]linux下各文件夹的结构说明及用途介绍
    [转载]linux常用命令
    [转载]Ubuntu 16.04 蓝屏解决方案
    pycharm修改python版本
  • 原文地址:https://www.cnblogs.com/zhongzheng123/p/7151123.html
Copyright © 2011-2022 走看看