easyUI 滚动条 Poi导出报表

阅读更多

ExportExcel.java类

package com.pro.lottery.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.pro.lottery.dao.DataSourceFactory;
import com.pro.lottery.modle.ReqLottery;

/**
 * 
 * @author Caixu
 *
 */
public class ExportExcel extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3297347144890990614L;
	public static double totalCount = 0;	//总共有多少条数据
	public static double execCount = 0;		//已经处理的数据
	/** 
     * 对list数据源将其里面的数据导入到excel表单 
     *  
     * @param fieldName 	
     *            [] 导出到excel文件里的表头名 
     * @param columnIt 
     *            [] 导出到excel文件里的表头NAME 
     * @param sheetName 
     *            工作表的名称 
     * @param sheetSize 
     *            每个sheet中数据的行数,此数值必须小于65536 
     * @param output 
     *            java输出流 
     */  
    public static boolean exportExcel(List list, String[] fieldName,  
            Object[] columnIt, String sheetName, int sheetSize,  
            OutputStream output) {  
        HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象  
        if (sheetSize >= 65536) {  
            sheetSize = 65536;  
        }  
        double sheetNo = Math.ceil(list.size() / sheetSize);  
        for (int index = 0; index <= sheetNo; index++) {  
            HSSFSheet sheet = workbook.createSheet();// 产生工作表对象  
            workbook.setSheetName(index, sheetName+index);//设置工作表的名称.  
            HSSFRow row = sheet.createRow(0);// 产生一行  
            HSSFCell cell;// 产生单元格  
            
            //表头样式
            HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式  
            headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    //设置垂直居中  
            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   //设置水平居中  
            HSSFFont headerFont = (HSSFFont) workbook.createFont(); //创建字体样式  
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
            headerFont.setFontName("Times New Roman");  //设置字体类型  
            headerFont.setFontHeightInPoints((short) 10);    //设置字体大小  
            headerStyle.setFont(headerFont);    //为标题样式设置字体样式 
            headerStyle.setFillBackgroundColor((short) 2);
            headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
            headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
            headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
            headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
            headerStyle.setWrapText(true); // 设置为自动换行 
            headerStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置背景色
            headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置前景色
            headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            
            //表body样式
            HSSFCellStyle cellStyle = (HSSFCellStyle) workbook .createCellStyle();
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    //设置垂直居中  
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   //设置水平居中
            cellStyle.setWrapText(true); // 设置为自动换行 
            
            // 写入各个字段的名称  
            for (int i = 0; i < fieldName.length; i++) { 
                cell = row.createCell(i); // 创建第一行各个字段名称的单元格 
                row.setHeight((short) 450);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置单元格内容为字符串型  
                // cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
                // //为了能在单元格中输入中文,设置字符集为UTF_16  
                cell.setCellValue(fieldName[i]); // 给单元格内容赋值  
                
                cell.setCellStyle(headerStyle); 
            }  
  
            int startNo = index * sheetSize;  
            int endNo = Math.min(startNo + sheetSize, list.size());  
            // 写入各条记录,每条记录对应excel表中的一行  
            for (int i = startNo; i < endNo; i++) {  
                row = sheet.createRow(i + 1 - startNo); 
                row.setHeight((short) 350);
                sheet.setColumnWidth(i,  4500);
                
                HashMap map = (HashMap) list.get(i);  
                for (int j = 0; j < columnIt.length; j++) {  
                    cell = row.createCell(j);  
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
                    cell.setCellStyle(cellStyle);
                    // cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
                    Object value = map.get(columnIt[j]);  
                    if (value != null) {  
                        cell.setCellValue(map.get(columnIt[j]).toString());  
                    } else  
                        cell.setCellValue("");  
                }  
                execCount = execCount + 1;
            }  
        }  
        try {
        	//为了让百分比不显示100% 
        	execCount = execCount - (totalCount * 0.02);
        	workbook.write(output);
        	execCount = execCount + (totalCount * 0.02);
            output.flush();  
            output.close();  
            return true;  
        } catch (IOException e) {  
            e.printStackTrace();  
            System.out.println("Output is closed ");  
            System.out.println("清除了。。。。。。。。。。。");
			execCount = 0;
            return false;  
        }  
  
    }  
	
    public String exportExcelAction(){
    	HttpServletResponse response = this.getResponse();
    	HttpServletRequest request = this.getRequest();
		response.setCharacterEncoding("utf-8");
		String fileName = request.getParameter("fileName");
		// 初始化数据  
      /* List> list = new ArrayList>(); 
        for (int i = 0; i < 53333; i++) {  
            Map map = new HashMap();  
            map.put("id", i);  
            map.put("name", "姓名" + i);  
            map.put("age", 20+ i);  
            list.add(map);  
        } */
		long count = DataSourceFactory.getInstance().findCount("select count(1) from lotteryInfo where 1=1 ");
		List lotterInfo = DataSourceFactory.getInstance().getObjListByProcedure(ReqLottery.class, "PUBLIC_FINDVIEWPAGE_FIND", new Object[]{"lotteryInfo",
			"1", String.valueOf(count), "lotteryissue", "*", "0", "1", "1=1 " });
		
		List> list = new ArrayList>(); 
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
		for (ReqLottery lottery : lotterInfo) {
			Map map = new HashMap();  
			if(lottery.getLotteryId() == 1001){
				map.put("lotteryId", "重庆时时彩");
			}else{
				map.put("lotteryId", "江西时时彩");
			}
			map.put("lotteryNum", lottery.getLotteryNum());
			map.put("lotteryIssue", lottery.getLotteryIssue());
			String str = df.format(lottery.getLotteryDate());
			map.put("lotteryDateStr", str);
			map.put("lotteryType1", lottery.getLotteryType1());
			map.put("lotteryType2", lottery.getLotteryType2());
			map.put("lotteryType3", lottery.getLotteryType3());
			list.add(map);
		}
        totalCount = list.size();
        String[] alias = { "彩种类型", "开奖号码", "期号", "日期", "前三", "中三", "后三" };// excel的列头  
        String[] names = { "lotteryId", "lotteryNum", "lotteryIssue", "lotteryDateStr", "lotteryType1", "lotteryType2", "lotteryType3" };// 数据List中的Map的key值. 
       /* String[] alias = { "编号", "姓名", "年龄" };// excel的列头  
        String[] names = { "id", "name", "age"};*/
        OutputStream os = null;
        try {  
	        os = response.getOutputStream();
	        //FileOutputStream out = null;  
           // out = new FileOutputStream("C:\\Work\\e.xls");
            //设置对话框
            response.setHeader("Content-disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));
            //设置 MIME(Excel)
            response.setContentType("application/vnd.ms-excel");
            //设置编码
            response.setCharacterEncoding("UTF-8");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        exportExcel(list, alias, names, "学生信息表", 60000, os); 
        System.out.println("成功");
        return null;
    }
    
    /**
     * 得到进度条的百分比
     * @return
     * @throws IOException
     */
    public String getProgressVal() throws IOException{
    	HttpServletResponse response = this.getResponse();
    	HttpServletRequest request = this.getRequest();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = getResponse().getWriter();
		
		//第一次请求将已经处理的条数清0
		System.out.println("execCount:"+execCount+"   totalCount:"+totalCount);
		if(execCount == 0){
			out.print(0.01);
			return null;
		}
		try {
			double val = (execCount / totalCount) * 100;
			//DecimalFormat df = new DecimalFormat("#.00");
			//System.out.println("valule:"+df.format(val));
			if(val >= 100){
				System.out.println("val >= 100清除了。。。。。。。。。。。");
				execCount = 0;
			}
			if(execCount >= totalCount){
				System.out.println("execCount >= totalCount 清除了。。。。。。。。。。。");
				execCount = 0;
			}
			out.print(val);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			execCount = 0;
			out.print(0);
			return null;
		}finally{
			out.flush();
			out.close();
		}
    }
	public static void main(String[] args) {
		// 初始化数据  
        List> list = new ArrayList>();  
        for (int i = 0; i < 65536; i++) {  
            Map map = new HashMap();  
            map.put("id", i);  
            map.put("name", "姓名" + i);  
            map.put("age", 20+ i);  
            list.add(map);  
        }  
  
        String[] alias = { "编号", "姓名", "年龄" };// excel的列头  
        String[] names = { "id", "name", "age" };// 数据List中的Map的key值.  
        FileOutputStream out = null;  
        try {  
            out = new FileOutputStream("C:\\Work\\e.xls");  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
  
        exportExcel(list, alias, names, "学生信息表", 60000, out);  
        System.out.println("----执行完毕----------");  
	}
}

 jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		
		 
		
		

 效果图如下:


easyUI 滚动条 Poi导出报表_第1张图片
 
easyUI 滚动条 Poi导出报表_第2张图片
 
easyUI 滚动条 Poi导出报表_第3张图片
 

  • easyUI 滚动条 Poi导出报表_第4张图片
  • 大小: 122.2 KB
  • easyUI 滚动条 Poi导出报表_第5张图片
  • 大小: 182.8 KB
  • easyUI 滚动条 Poi导出报表_第6张图片
  • 大小: 185 KB
  • 查看图片附件

你可能感兴趣的:(导报表,EasyUI,滚动条,poi)