poi 导出excel(传入map类型)

工具类:

public HSSFWorkbook exportExcelByList(HSSFWorkbook workbook, int sheetNum,String title, String[] headers, String[] columNames,
            List<Map<String,Object>> dataset,String pattern){  
        
        // 生成一个表格  
        HSSFSheet sheet = workbook.createSheet();  
        workbook.setSheetName(sheetNum,title);
        // 设置表格默认列宽度为15个字节  
        sheet.setDefaultColumnWidth((short) 15);  
        // 产生表格标题行  
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        for (short i = 0; i < headers.length; i++){  
            cell = row.createCell(i);
            
            HSSFFont font = (HSSFFont) workbook.createFont();
            font.setFontHeightInPoints((short) 12); // 字体高度
            font.setFontName("宋体"); // 字体
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度加粗
            HSSFCellStyle style = (HSSFCellStyle) workbook.createCellStyle();

           style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

            style.setFont(font);
            cell.setCellStyle(style);
            
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
            cell.setCellValue(text);
        }  
        // 遍历集合数据,产生数据行  
        int index = 0;  
        for(int ii = 0;ii<dataset.size();ii++){
            index++;  
            row = sheet.createRow(index);  
            Map<String,Object> map = dataset.get(ii);
            for (short i = 0; i < columNames.length; i++){  
                cell = row.createCell(i);  
                try{  
                    Object value = map.get(columNames[i]);
                    String textValue = null;  
                    // 判断值的类型后进行强制类型转换  
                    if(null != value){
                        if (value instanceof Date){  
                            Date date = (Date) value;  
                            SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
                            textValue = sdf.format(date);  
                        }else{  
                            // 其它数据类型都当作字符串简单处理  
                            textValue = value.toString();
                        }  
                    }
                    // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成  
                    if (textValue != null){  
                        Pattern p = Pattern.compile("^//d+(//.//d+)?$");  
                        Matcher matcher = p.matcher(textValue);  
                        if (matcher.matches()){  
                            // 是数字当作double处理  
                            cell.setCellValue(Double.parseDouble(textValue));  
                        }else{  
                            HSSFRichTextString richString = new HSSFRichTextString(textValue);  
                            cell.setCellValue(richString);  
                        }  
                    }else{
                        textValue="";
                    }  
                }catch (Exception e){  
                    e.printStackTrace();  
                }
            }  
        }
        return workbook;
    }

action类:

public String exportBiologicalAnalysis(){
         // 声明一个工作薄  
       HSSFWorkbook workbook = new HSSFWorkbook(); 
       ExportExcel<Sample> ex = new ExportExcel<Sample>();  
       
       int total=biologicalAnalysisService.findCount(conditions);
        List<Map<String,Object>> biologicalAnalysis=new ArrayList<Map<String,Object>>();
        if(total>0){
            biologicalAnalysis=biologicalAnalysisService.find(conditions, 0, total);
        }
        String[] headers = {"批次","源序号","代码","文库名称(唯一标识)","文库编号","barcode","实验使用芯片","捕获文库名称","data/G","实验设计","优先级","分析范围(程序生成)","实验备注","上机时间","任务日期","采血类型","检测项目","产品线","任务备注","其他说明","合作机构","姓名","姓名(系统修正)","性别","年龄","家系号","与先证者关系","患者/轻微表型/正常","客户单位","科室","客户名字","样本类型","科研目的"};    
        String[] columNames = {"batchNo","resourceCode","seqCode","libraryName","libraryCode","barcode","chip","captureLibrary","seqData","design","priority","analysisRange","seqRemark","sequencTime","taskDate","diccode","examname","classescode","taskRemark","otherRemark","insName","checkerName","amendName","gender","age","familyCode","relativeCode","diseaseCondition","hospitalName","departmentName","doctorName","checkType","scientificPurposes"};    
        try {
            workbook = ex.exportExcelByList(workbook,0,"生物分析",headers,columNames, biologicalAnalysis);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            workbook.write(output);
            byte[] ba = output.toByteArray();
            excelFile = new ByteArrayInputStream(ba);
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       return SUCCESS;
    }




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值