zoukankan      html  css  js  c++  java
  • 导出Excel(终极版)

    /**
    	 * 导出excel 案例
    	 */
    	public void exportExcel(){
    		long beginTime = System.currentTimeMillis();
    		HttpServletResponse response= ServletActionContext.getResponse();
    		OutputStream os=null;
    		if(activityForm==null){
    			activityForm=new ActivityForm();
    		}
    		try {
    			activityForm.setStartSize((getCurrentpage()-1)*getPagesize());
    			activityForm.setPageSize(getPagesize());
    			activityForm.setUserInfoId(getUserInfoSession().getId());
    			PageList<ActivityForm> page=iActivityService.getPage(activityForm);
    			activityForm.setPageSize(page.getTotalcount());
    			List<ActivityForm> activitys=iActivityService.getPage(activityForm).getResult();
    			
    			Workbook createbook = createActivityBook(activitys);
    			// 创建下载文件名
    			String uploadFileName = System.currentTimeMillis() + ".xls";
    			os = response.getOutputStream();// 取得输出流
    			response.reset();// 清空输出流
    			response.setHeader("Content-disposition", "attachment; filename="+ uploadFileName);// 设定输出文件头
    			response.setContentType("application/x-download");
    			createbook.write(os);
    			os.close();
    		} catch (Exception e) {
    			error("请求exportActivity方法异常",e);
    		}finally{
    			printTime(beginTime, getClass(), "exportActivity");
    		}
    	}
    

      

    public static HSSFWorkbook createActivityBook(List<ActivityForm> list){
    		// 创建Excel文档
    		HSSFWorkbook hwb = new HSSFWorkbook();
    		ActivityForm activityForm=null;
    		// sheet 对应一个工作页
    		HSSFSheet sheet = hwb.createSheet("导出活动列表信息");
    		int CountColumnNum = 5;  //指定列头个数
    		HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
    		HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
    		String[] names = new String[CountColumnNum];
    		names[0] = "活动名称";
    		names[1] = "活动类型";
    		names[2] = "活动链接";
    		names[3] = "备注";
    		names[4] = "创建时间";
    		for (int j = 0; j < CountColumnNum; j++) {
    			firstcell[j] = firstrow.createCell(j);
    			firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
    		}
    		for (int i = 0; i < list.size(); i++) {
    			// 创建一行
    			HSSFRow row = sheet.createRow(i + 1);
    			// 得到要插入的每一条记录
    			activityForm = list.get(i);
    			for (int colu = 0; colu < CountColumnNum; colu++) {
    				// 在一行内循环
    				HSSFCell name = row.createCell(0);
    				HSSFCell type = row.createCell(1);
    				HSSFCell url = row.createCell(2);
    				HSSFCell remark = row.createCell(3);
    				HSSFCell createDate = row.createCell(4);
    				
    				name.setCellValue(activityForm.getName());
    				type.setCellValue(activityForm.getType());
    				url.setCellValue(activityForm.getUrl());
    				remark.setCellValue(activityForm.getRemark());
    				createDate.setCellValue(activityForm.getCreateDate());
    			}
    		}
    		return hwb;
    	}
    

      

    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    

      

  • 相关阅读:
    npm 引入第三方过滤器
    登录加密 md5
    JavaScript 日期处理类库 moment
    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
    js 常见的小数取整问题
    vue 路由跳转到外部链接
    js some和filter用法和区别
    前端调用后端接口返回200(成功状态码),后端有返回,但是控制台Network Response为空,没展示任何信息
    二叉树的镜像
    树的子结构
  • 原文地址:https://www.cnblogs.com/zhaojinhui/p/4964480.html
Copyright © 2011-2022 走看看