poi 导出功能。

public  class ExportToExcelServiceImpl implements ExportToExcelService{
    private HSSFWorkbook workbook;
    private String[] tableNames;

    public ExportToExcelServiceImpl(){
    };
    /**
       */
    public HSSFWorkbook exportToExcel(String title, List values,String[] excelHeader) {
        workbook = new HSSFWorkbook();
        int tableNum = 0;
        if (values.size() % 50000 == 0) {
            tableNum = values.size() / 50000 ;
        } else {
            tableNum = values.size() / 50000 +1;
        }
        for (int i = 1; i <= tableNum; i++) {
            HSSFSheet sheet = workbook.createSheet(title  + i );
            HSSFRow row = sheet.createRow((int) 0);
            HSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            if(excelHeader!=null){
                for (int j = 0; j < excelHeader.length; j++) {
                    HSSFCell cell = row.createCell((short) j);
                    cell.setCellStyle(style);
                    cell.setCellValue(excelHeader[j]);
                }
            }else{
                Field[] fieldNum = values.get(i).getClass().getDeclaredFields();
                int serial=0;
                for (int j = 0; j < fieldNum.length; j++) {
                    if(fieldNum[j].getName().equals("serialVersionUID")){
                        serial=1;
                        continue;
                    }
                    HSSFCell cell = row.createCell((short) (j - serial));
                    cell.setCellStyle(style);
                    cell.setCellValue(fieldNum[j].getName());
                }
            }
            int pageSize=0;
            if(i==tableNum){
                pageSize=values.size();
            }else{
                pageSize=i*50000;
            }
            for (int j =(i -1) * 50000; j < pageSize; j++) {
                row = sheet.createRow(j-(i-1)*50000+1);
                Field[] fields = values.get(j).getClass().getDeclaredFields();
                int serial=0;
                for (int k = 0; k < fields.length; k++) {
                    Field field = fields[k];
                    String fieldName = field.getName();
                    String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    if(fieldName.equals("serialVersionUID")){
                        serial=1;
                        continue;
                    }
                    HSSFCell cell = row.createCell((short)(k-serial));
                    cell.setCellStyle(style);
                    try {
                        Class clazz = values.get(j).getClass();
                        Method getMethod = clazz.getMethod(getMethodName, new Class[]{});
                        Object value = getMethod.invoke(values.get(j), new Object[]{});
                           if (value instanceof Integer) {
                            int intValue = (Integer) value;
                            cell.setCellValue(intValue);
                        } else if (value instanceof Float) {
                            float fValue = (Float) value;
                            cell.setCellValue(fValue);
                        } else if (value instanceof Double) {
                            double dValue = (Double) value;
                            cell.setCellValue(dValue);
                        } else if (value instanceof Long) {
                            long longValue = (Long) value;
                            cell.setCellValue(longValue);
                        } else if (value instanceof Date) {
                            Date date = (Date) value;
                            cell.setCellValue(date);
                        } else if(value instanceof  String){
                            String valueString=(String)value;
                            cell.setCellValue(valueString);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return workbook;
    }

    public static void main(String[] args){
        String [] params={"xuehao","xingming","sex","gun","6","4","43","4"};
        List<MbChlRspMapDO> list=new ArrayList<MbChlRspMapDO>();
        for(int i=0; i<50050; i++){
            MbChlRspMapDO mb=new MbChlRspMapDO();
            mb.setChannelId("1234"+i);
            mb.setChlRspCode("2235"+i);
            mb.setChlRspMsg("woshihaoren"+i);
            mb.setCreator("zhangbo"+i);
            mb.setId("323"+i);
            mb.setStatus("4340"+i);
            mb.setUpdator("dao"+i);
            mb.setFlag("Y"+i);
            list.add(mb);
        }
        HSSFWorkbook workbook=new ExportToExcelServiceImpl().exportToExcel("woshihaoren",  list,null);
        OutputStream out=null;
        try{
           out=new FileOutputStream("D:\\a.xls");
            workbook.write(out);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.10-FINAL</version>
</dependency>

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.10-FINAL</version>
</dependency>

 response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=customer.xls");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值