java实现下载excle(jxl和poi 两种方式)

@RequestMapping(value="/download_index")
	public  String  downloadExcel(HttpServletRequest request,HttpServletResponse response,String name) {

		OutputStream os =  null;
    	WritableWorkbook wbook = null;
    	try {
    		
            String flieName =  name;
            // 取得输出流
            os = response.getOutputStream();
            response.reset();// 清空输出流
            // 设定输出文件头
            response.setHeader("Content-disposition", "attachment; filename=" + new String(flieName.getBytes("UTF-8"), "ISO8859-1") + ".xls");
            // 定义输出类型
            response.setContentType("application/msexcel;charset=utf-8");
            
            // 建立excel文件
            wbook = Workbook.createWorkbook(os);
            
            //要下载的list数据
            List<UnifiedVO>  unifiedVOList = new ArrayList<UnifiedVO>();
            	
            // 设置数据
            setWorkBookData(unifiedVOList, wbook,flieName);
            
            if (wbook.getNumberOfSheets() > 0) {
                wbook.write();
            }
            wbook.close();
            os.close();
            
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {
		if(wbook != null) {
				 try {
					wbook.close();
				} catch (WriteException | IOException e) {
					e.printStackTrace();
				}
			}
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		}
		return null;
	}

unifiedVOList是下载数据集合。然后将list数据集合、wbook、表名传入方法内。

	private void setWorkBookData(List<UnifiedVO> giftRecords, WritableWorkbook wbook,String name)
			throws WriteException, RowsExceededException {
		
		final Integer EXCEL_MAX_NUMBER = 65535;
		
        WritableCellFormat wc = new WritableCellFormat();
        // 设置居中
        wc.setAlignment(Alignment.CENTRE);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		WritableSheet wsheet = null;

		int sheetNumber = 0;
		if (giftRecords.size() % EXCEL_MAX_NUMBER != 0) {
		    sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER + 1;
		} else {
		    sheetNumber = giftRecords.size() / EXCEL_MAX_NUMBER;
		}

		for (int j = 0; j < sheetNumber; j++) {
		    wsheet = wbook.createSheet(name + (j + 1), j); // sheet名称
		    // 开始生成主体内容
		    wsheet.addCell(new Label(0, 0, "名称", wc));
		    wsheet.setColumnView(0, 32);
		    
		    wsheet.addCell(new Label(1, 0, "时间", wc));
		    wsheet.setColumnView(1, 20);

		    wsheet.addCell(new Label(2, 0, "数据", wc));
		    wsheet.setColumnView(2, 32);

		    wsheet.addCell(new Label(3, 0, "单位", wc));
		    wsheet.setColumnView(3, 30);

		    int start = EXCEL_MAX_NUMBER * j;
		    int end = EXCEL_MAX_NUMBER + start;

		    for (int i = start, k = giftRecords.size(); i < k && i < end; i++) {
		    	UnifiedVO unifiedVO = giftRecords.get(i);
		        int row = i - start + 1;
		        wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));
		        wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));
		        wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));
		        wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));
		    }
		}
	}

wsheet.addCell(new Label(0, 0, "名称", wc))
wsheet.addCell(new Label(1, 0, "时间", wc));
wsheet.addCell(new Label(2, 0, "数据", wc));

wsheet.addCell(new Label(3, 0, "单位", wc));

上面的字段一一对应VO字段

wsheet.addCell(new Label(0, row, unifiedVO .getKey(), wc));
wsheet.addCell(new Label(1, row, unifiedVO .getDate(), wc));
wsheet.addCell(new Label(2, row, unifiedVO .getData(), wc));
wsheet.addCell(new Label(3, row, unifiedVO .getUnit(), wc));

对应的maven API :

		<!-- 导出excel jxl  -->
		<dependency>
			<groupId>net.sourceforge.jexcelapi</groupId>
			<artifactId>jxl</artifactId>
			<version>2.6.12</version>
		</dependency>

=====================================================================================

 poi 导出:

	<!-- 导出excel poi  -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.17</version>
		</dependency>
        

最简单的下载数据实现:

public test(HttpServletRequest request,HttpServletResponse response) {
		
		List<?> arrayList  = new ArrayList<Object>();
		
		OutputStream os =  null;
		
        // 取得输出流
        try {
			os = response.getOutputStream();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
        response.reset();// 清空输出流
        
        //1.在内存中创建一个excel文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //2.创建工作簿
        HSSFSheet sheet = hssfWorkbook.createSheet();
        
        //3.创建标题行
        HSSFRow titlerRow = sheet.createRow(0);
        HSSFCellStyle createCellStyle = hssfWorkbook.createCellStyle();//创建单元格样式 
        
        createCellStyle.setFillForegroundColor((short) 255);// 设置背景色
        createCellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION); // 居中
        createCellStyle.setBorderTop(BorderStyle.SLANTED_DASH_DOT);
        
        titlerRow.createCell(0).setCellValue("营销顾问");
     
       
        titlerRow.setRowStyle(createCellStyle);
        
        for (Object o : arrayList) {
        	  //获取最后一行的行号
            int lastRowNum = sheet.getLastRowNum();
            
            HSSFRow dataRow = sheet.createRow(lastRowNum + 1);
            
            dataRow.createCell(0).setCellValue(o.get营销顾问());
       
		}
        //5.创建文件名
        String fileName = "区域数据统计.xls";
        //9.设置信息头
        response.setContentType(fileName);
        try {
			fileName = new String (fileName.getBytes ("utf-8"),"ISO8859-1");
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
		}
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        //10.写出文件,关闭流
        try {
			hssfWorkbook.write(os);
		} catch (IOException e1) {
		}
        try {
			hssfWorkbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值