后台处理excel下载输出流

前台

<ul class="navtop-right">
	<li >
        <a href="/portal/trip/importExec" title="Data Download"> <img src="${pageContext.request.contextPath}/style/images/excel6.jpg" width=20px height=20px style="padding-top:15px"/> </a> </li> </ul>

 

 

 

后台

HSSFRow row = sheet.createRow(0);: 创建第0行

HSSFCell c2 = row.createCell(2); 创建0行2列

HSSFCell c3 = row.createCell(4); 说明2列占了2列

sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)0, (short)0)); 说明 合并单元格, 0行,1行合并成一行

 

@RequestMapping(value = "importExec", method = RequestMethod.GET)
	@ResponseBody
	public void importExec(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		String fname = "Biztrip list";
		response.reset();// 清空输出流
		response.setHeader("Content-disposition","attachment; filename=" + fname + ".xls");// 设定输出文件头
		response.setContentType("application/msexcel");//EXCEL格式  Microsoft excel
		//创建workbook   
        HSSFWorkbook workbook = new HSSFWorkbook(); 
        HSSFCellStyle style = workbook.createCellStyle();  
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
	    HSSFFont f  = workbook.createFont();  
	   // f.setColor(HSSFColor.RED.index);
	    f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗 
	    style.setFont(f);
	    style.setFillForegroundColor(HSSFColor.LIME.index);   
	    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        //创建sheet页  
        HSSFSheet sheet = workbook.createSheet("Business Trip Info.");   
        //创建单元格  
        HSSFRow row = sheet.createRow(0);   
        HSSFCell c0 = row.createCell(0);   
        c0.setCellValue(new HSSFRichTextString("No"));   
        c0.setCellStyle(style);  

        HSSFCell c1 = row.createCell(1);   
        c1.setCellValue(new HSSFRichTextString("Name")); 
        c1.setCellStyle(style);  

        HSSFCell c2 = row.createCell(2);   
        c2.setCellValue(new HSSFRichTextString("Part")); 
        c2.setCellStyle(style);  

        HSSFCell c3 = row.createCell(4);   
        c3.setCellValue(new HSSFRichTextString("Purpose"));  
        c3.setCellStyle(style);  

        HSSFCell c4 = row.createCell(5);   
        c4.setCellValue(new HSSFRichTextString("Schedule")); 
        c4.setCellStyle(style);  

        HSSFCell c5 = row.createCell(8);   
        c5.setCellValue(new HSSFRichTextString("Destination")); 
        c5.setCellStyle(style);  
        
        HSSFCell c6 = row.createCell(11);   
        c6.setCellValue(new HSSFRichTextString("Report")); 
        c6.setCellStyle(style); 
        HSSFCell c7 = row.createCell(12);   
        c7.setCellValue(new HSSFRichTextString("Ref.")); 
        c7.setCellStyle(style); 
       

        HSSFRow row1 = sheet.createRow(1);   
       
        HSSFCell c8 = row1.createCell(5); 
        c8.setCellValue(new HSSFRichTextString("Start"));   
        c8.setCellStyle(style);  
        HSSFCell c9 = row1.createCell(6); 
        c9.setCellValue(new HSSFRichTextString("End"));   
        c9.setCellStyle(style);  
        HSSFCell c10 = row1.createCell(7); 
        c10.setCellValue(new HSSFRichTextString("Days"));   
        c10.setCellStyle(style); 
        HSSFCell c11 = row1.createCell(8); 
        c11.setCellValue(new HSSFRichTextString("Country"));   
        c11.setCellStyle(style);
        HSSFCell c12 = row1.createCell(9); 
        c12.setCellValue(new HSSFRichTextString("Region"));   
        c12.setCellStyle(style);
        HSSFCell c13 = row1.createCell(10); 
        c13.setCellValue(new HSSFRichTextString("Dept."));   
        c13.setCellStyle(style);  
       
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)0,  (short)0));
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)1,  (short)1));
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)2,  (short)3));
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)4,  (short)4));
        sheet.addMergedRegion(new CellRangeAddress(0, 0,(short)5,  (short)7));
        sheet.addMergedRegion(new CellRangeAddress(0, 0,(short)8,  (short)10));
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)11,  (short)11));
        sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)12,  (short)12));
       
        List<Trip> tripList =  tripService.findAll();
        for(int i=0;i<tripList.size();i++){
        	row=sheet.createRow((int)i+2);
        	Trip trip = (Trip)tripList.get(i);
        	row.createCell((short)0).setCellValue(new HSSFRichTextString(i+1+""));
        	row.createCell((short)1).setCellValue(new HSSFRichTextString(trip.getName()));
        	row.createCell((short)2).setCellValue(new HSSFRichTextString(trip.getPart()));
        	row.createCell((short)3).setCellValue(new HSSFRichTextString(trip.getSubPart()));
        	row.createCell((short)4).setCellValue(new HSSFRichTextString(trip.getPurpose()));
        	row.createCell((short)5).setCellValue(new HSSFRichTextString(trip.getScheduleStart()));
        	row.createCell((short)6).setCellValue(new HSSFRichTextString(trip.getScheduleEnd()));
        	row.createCell((short)7).setCellValue(new HSSFRichTextString(trip.getDuration()));
        	row.createCell((short)8).setCellValue(new HSSFRichTextString(trip.getDestination()));
        	row.createCell((short)9).setCellValue(new HSSFRichTextString(trip.getRegion()));
        	row.createCell((short)10).setCellValue(new HSSFRichTextString(trip.getDepartment()));
        	row.createCell((short)11).setCellValue(new HSSFRichTextString(trip.getReport()));
        	row.createCell((short)12).setCellValue(new HSSFRichTextString(trip.getReferrence()));
        }
        
        try{   
	          workbook.write(response.getOutputStream());  
	    }  
	    catch (Exception e){  
	        e.printStackTrace();  
	    }  
	}

 

转载于:https://www.cnblogs.com/wujixing/p/5948740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
后台使用 Java 导出 Excel 文件时,可以使用 Apache POI 库来创建和写入 Excel 文件。具体步骤如下: 1. 创建工作簿,并创建工作表对象 ```java Workbook workbook = new HSSFWorkbook(); // 创建工作簿对象,支持xls格式 Sheet sheet = workbook.createSheet("Sheet1"); // 创建工作表对象 ``` 2. 编写数据写入逻辑,将数据写入到工作表中 ```java Row row = sheet.createRow(0); // 创建第一行对象 // 写入第一行数据 Cell cell1 = row.createCell(0); cell1.setCellValue("姓名"); Cell cell2 = row.createCell(1); cell2.setCellValue("年龄"); Row row2 = sheet.createRow(1); // 创建第二行对象 // 写入第二行数据 Cell cell3 = row2.createCell(0); cell3.setCellValue("张三"); Cell cell4 = row2.createCell(1); cell4.setCellValue(25); ``` 3. 将写入的数据输出到文件中 ```java OutputStream out = response.getOutputStream(); // 获取响应输出 response.reset(); response.setHeader("Content-disposition", "attachment; filename=example.xls"); // 设置文件名 response.setContentType("application/msexcel"); // 设置文件类型 workbook.write(out); // 将工作簿写入输出 out.flush(); // 刷新缓冲区 out.close(); // 关闭输出 ``` 在前端页面中,可以使用 `XMLHttpRequest` 或者 `fetch` API 发起请求,获取 Excel 文件。获取到文件后,可以通过 Blob 对象将 Excel 文件转换为 URL,然后通过 a 标签下载。示例代码如下: ```javascript // 使用 fetch API 发起请求 fetch('/export/excel').then(function(response) { return response.blob(); }).then(function(blob) { // 将文件转换为 URL var url = window.URL.createObjectURL(blob); // 创建 a 标签,设置下载链接和文件名,模拟下载 var link = document.createElement('a'); link.href = url; link.download = 'example.xls'; link.click(); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值