java项目中导出功能的参考写法。

来源:互联网 发布:mysql mvcc 乐观锁 编辑:程序博客网 时间:2024/06/06 02:21

java方法代码如下:

@ResponseBody
@RequestMapping(value = "/exportCorrectionEnd", method = RequestMethod.POST)
public Result expCorrectionEnd(CorrectionEndVO vo){

Integer inumCell =5;

if (vo.getDeptPk() == null || vo.getDeptPk() < 1) {
Dept dept = new Dept();
dept = (Dept) getSession().getAttribute(Constants.CURRENT_USER_DEPT);
vo.setDeptPk(dept.getPkId());
}

String[] headerArr ={"矫正单位","姓名","证件号码","手机号码","矫正类别"};
String sqlId = "com.cdkj.correct.domain.CorrectionEnd.CorrectEndExcelExport";
List<ExcelExportBean> listBean = ((CorrectEndService) getBaseService()).getExportList(sqlId, vo);


String res = "";
   try{  
    res = arrayExcel("归档档案", listBean, inumCell, headerArr);
   }catch(Exception e){   
       e.printStackTrace();   
  }  
Result result = new Result();
result.setStatus(Status.OK);
result.setMessage("导出成功:"+res);
return result;

}


private String arrayExcel(String sheetName, List<ExcelExportBean> listBean, int inumCell, String[] headerArr)
throws Exception {
int numCell = inumCell;
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        //得到项目路径  ServletActionContext.getServletContext().getRealPath("/")
        StringBuffer filepath = new StringBuffer("C:\\Users\\Administrator\\Desktop\\");   
        //String filepath = ServletActionContext.getServletContext().getRealPath("/")+"upload/excel";
        String fileName= createFileName();
        File dirFile = new File(filepath.toString());
   if(!dirFile.exists()){         
    dirFile.mkdirs();
   }
   String filedir = "upload/excel/"+fileName;
   String tempPath = filepath.append(filedir).toString();
        File excelFile = new File(tempPath);
        if(!excelFile.exists()){
        if(!excelFile.getParentFile().exists()){
        excelFile.getParentFile().mkdirs();
        }
            excelFile.createNewFile();
            //excelFile = null;
            //workbook = new HSSFWorkbook();
            //sheet = workbook.createSheet(StringUtils.isEmpty(sheetName)?"0":sheetName);
        }
        workbook = new HSSFWorkbook();
        sheet = workbook.createSheet("sheet");
        HSSFRow row = sheet.createRow((short)(0));
        for(int j = 0; j < numCell; j++){
            HSSFCell cell = row.createCell((short)(j));
cell.setCellValue(headerArr[j]);
        }
        int K = 1;
        int count = 0;
        int i = 0;
        Object[] oob = new Object[]{};
        for(ExcelExportBean beans : listBean){
        i++;
    if(i%10000 == 0){
        sheet = workbook.createSheet("sheet"+(i/10000));
        count = 0;
        }
    count++;
            row = sheet.createRow(count);
            for(int j = 1; j <= numCell; j++){
           
            Object o = beans;
            String methodName = "getC"+j;
                Method m = o.getClass().getDeclaredMethod(methodName);
    String val = (String)m.invoke(o, oob);
           
                HSSFCell cell = row.createCell(j - 1);
cell.setCellValue(val);
            }
            K++;
        }
        FileOutputStream excelOutStream = new FileOutputStream(tempPath);
        workbook.write(excelOutStream);
        excelOutStream.flush();
        excelOutStream.close();
        return filedir;
}

private String createFileName() {   
        StringBuffer sb = new StringBuffer();   
        Date date = new Date();   
        //获取年月日时分秒   
        sb.append(new SimpleDateFormat("yyyyMMddHHmmss").format(date));   
        //毫秒   
        String milli = String.valueOf(date.getTime() % 1000);   
        while (milli.length() < 3) {   
            milli = "0" + milli;   
        }   
        sb.append(milli);   
        //四位随机数   
        String rondom = String.valueOf(new Random().nextInt(10000));   
        while (rondom.length() < 4) {   
            rondom = "0" + rondom;   
        }   
        sb.append(rondom).append(".xls");   
        return sb.toString();   
    }

//日期格式化
private  String getStringDate() {
  Date currentTime = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dateString = formatter.format(currentTime);
  return dateString;
}


0 0