后台处理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();
}
}
后台处理excel下载输出流的更多相关文章
- 后台生成excel前端下载
后台生成Excel时前端获取下载 Controller控制器: package com.example.test.controller; import com.example.test.common. ...
- ASP.NET Excel下载方法一览
方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 using System; using System.Co ...
- Flex Excel下载
最近做Flex里的Excel下载,用as3xls进行Excel导出后,Excel修改编辑后老出现:不能以当前格式保存...若要保存所做的更改,请单击“确定”,然后将其另存为最新的格式. 最后通过JAV ...
- 后台生成EXCEL文档,自定义列
后台生成EXCEL文档,自定义列 //response输出流处理 //设置编码.类型.文件名 getResponse().reset(); getResponse().setCharacterEnco ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
- 一步步实现ABAP后台导入EXCEL到数据库【3】
在一步步实现ABAP后台导入EXCEL到数据库[2]里,我们已经实现计划后台作业将数据导入数据库的功能.但是,这只是针对一个简单的自定义结构的导入程序.在实践应用中,面对不同的表.不同的导入文件,我们 ...
- 一步步实现ABAP后台导入EXCEL到数据库【1】
在SAP的应用当中,导入.导出EXCEL文件的情况是一个常见的需求,有时候用户需要将大量数据定期导入到SAP的数据库中.这种情况下,使用导入程序在前台导入可能要花费不少的时间,如果能安排导入程序为后台 ...
- DevExpress中GridView Excel下载
DevExpress中GridView提供了许多Excel下载的方法,如gridView.ExportToExcelOld(sfdExcelDown.FileName); 在修改Bug时,遇到这样问题 ...
- [转]50个极好的bootstrap 后台框架主题下载
50个极好的bootstrap 后台框架主题下载 http://sudasuta.com/bootstrap-admin-templates.html 越来越多的设计师和前端工程师开始用bootstr ...
随机推荐
- L2-002. 链表去重
L2-002. 链表去重 题目链接:https://www.patest.cn/contests/gplt/L2-002 这题因为结点地址只有四位数,所以可以直接开一个10000的数组模拟内存就好了. ...
- linux命令随记
1.当再Hadoop平台上跑代码的时候,可能会发现有些job是需要被kill重新跑的,此刻,如果一个一个手动去删除会很缓慢,此刻需要使用一个简单的脚本来实现自动全部删除操作: for i in `ha ...
- servlet 用法
引入servlet的jar包,这个包在tomcat的lib下 新建一个servlet文件继承httpServlet,要实现里面的doGet或者doPost方法 在web.xml文件中配置 在form表 ...
- WebSocket的原理,以及和Http的关系
一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...
- [SOJ] 1282. Computer games (KMP)
坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...
- Infix to postfix without '(' and ')'
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- ubuntu 修改root密码
重新安装过ubuntu 14.04后,切换到root权限,需要输入密码,输入n次都不对. 也没有记得设置过密码. 百度一下,得到方法如下,已经验证测试成功. 命令为: sudo passwd 提示输入 ...
- Linux的网卡由eth0变成了eth1,如何修复
Linux的网卡由eth0变成了eth1,如何修复 使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间后,不知道为什么eth0无法使用,系统却自动生 ...
- delphi中ShellExecute使用详解
http://jingyan.baidu.com/article/ae97a646ae00a2bbfd461d38.html 百度的讲解 http://www.cnblogs.com/del/arch ...
- Python库 - import matplotlib.pyplot as plt 报错问题
为了避免各种问题,请使用最新的2.7.13安装文件 1.先设置好环境变量 在path变量中设置好以下路径: C:\Python27\Scripts C:\Python27 2.大部分报错问题 ...