标签:poi
public class FileZip {
/**  
     *   
     * @param srcfile 文件名数组  
     * @param zipfile 压缩后文件  
     */  
    public static void ZipFiles(File[] srcfile, File zipfile) {  
        byte[] buf = new byte[1024];  
        try {  
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  
                    zipfile));  
            for (int i = 0; i < srcfile.length; i++) {  
                FileInputStream in = new FileInputStream(srcfile[i]);  
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));  
                int len;  
                while ((len = in.read(buf)) > 0) {  
                    out.write(buf, 0, len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
            out.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
} 
@RequestMapping("/exportCourseDataAll.do")
public String exportCourseDataListAll(HttpServletRequest req, HttpServletResponse res) throws IOException {
String startDate = req.getParameter("startDate");               // 开始日期
String endDate = req.getParameter("endDate");    
   // 结束日期
String reportType = req.getParameter("reportType");
   // 日周月报:‘day’,‘week’,‘month’。
String companyName = req.getParameter("companyName");
       // 公司名称
// 只能导出昨天的课程用户学习明细,日期传空
if (startDate == null || endDate == null) {
startDate = DateTimeUtils.getDateString("yyyy-MM-dd", Calendar.DATE, -1);
endDate = DateTimeUtils.getDateString("yyyy-MM-dd", Calendar.DATE, 0);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("startDate", startDate);
params.put("endDate", endDate);
params.put("reportType", reportType);
params.put("companyName", companyName);
res.setContentType("application/octet-stream;charset=UTF-8");  
String fileName = DateTimeUtils.formatDateNo_(new Date())+"用户明细数据";
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
        res.setHeader("Content-Disposition", "attachment;filename="  
                + java.net.URLEncoder.encode(fileName, "UTF-8")  
                + ".zip");  
        res.addHeader("Pargam", "no-cache");  
        res.addHeader("Cache-Control", "no-cache");  
// 数据量比较大时,分页导出数据
org.extremecomponents.table.context.Context context = new HttpServletRequestContext(req);
org.extremecomponents.table.limit.LimitFactory limitFactory = new TableLimitFactory(context);
Limit limit = new TableLimit(limitFactory);
OutputStream out = null;  
try {
int totalRows = limit.getTotalRows();
List<CourseDetailDTO> listAll = new ArrayList<CourseDetailDTO>();
totalRows =  wholeDataService.getCourseDetailListForPageCount(params);
int pageSize = 5000;
int totalPage = Integer.valueOf(CommonUtility.calcTotalPage(pageSize, totalRows));
for (int i = 0; i < totalPage; i++) {
params.put("pageNo", (i+1));
params.put("pageSize", pageSize);
List<CourseDetailDTO> listPage = wholeDataService.getCourseDetailAll(params);
listAll.addAll(listPage);
}
// 写入数据
out = res.getOutputStream(); 
toExcel(listAll,req,50000,fileName,out);
} catch (Exception e) {
logger.error("导出用户明细数据异常",e);
}
return null;
}
/**
* @Title: toExcel版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:poi
原文地址:http://blog.csdn.net/huangjinsheng1988/article/details/47755661