后台处理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 ...
随机推荐
- Oracle中sys和system用户的区别
1.数据库的启动需要以SYSDBA/SYSOPER身份登录. 2.如果在同一主机上使用IPC连接到数据库使用操作系统授权,登录任何一个用户都可以拥有as sysdba和as sysoper. 3.sy ...
- 设置phpMyAdmin本地自动登陆
一般配置本地测试用的 phpMyAdmin 可以不用每次输入帐号密码,打开后自动登陆就行了. 版本: phpMyAdmin 3.5.3 打开: phpMyAdmin 根目录 复制: config.sa ...
- USACO 1.3 Wormholes
Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...
- html历史
方法一: location.hash = 'abc' window.onhashchange = function(){} location.hash 返回结果 #abc => locati ...
- CSS3秘笈复习:第十一章
1.text-align与vertical-align: text-align控制水平方向的定位,关键字是left.right.center和justify. vertical-align控制垂直方向 ...
- ggplot2 geom设置—散点图
散点图也是目前R中的常用的图形之一 geom_point(mapping = NULL, data = NULL, stat = "identity", position = &q ...
- ggplot2 scale相关设置2—时间设置
在scale设置中,常用的日期方面的设置函数包括: scale_x_date(),scale_y_date(),scale_x_datetime(),scale_y_datetime() 接下来, ...
- Webpack入门——使用Webpack打包Angular项目的一个例子
2016.1.22,对大多数人来说,这是一个非常平常的日子,但这却是我决定在博客园写博客的日子.虽然注册博客园的博客已有4年8个月,却一直没有动手写过一篇博客,原因是觉得自己水平不行,写不出好东西,所 ...
- mysql导出数据表结构,必须退出mysql命令.重新使用msyqldump命令
只导出数据库中所有表结构(-d 减去数据) 导出所有表结构和数据 mysqldump -uroot --default-character-set=utf8 -p123-d必须空格good>H: ...
- 使用UGUI实现拖拽功能(拼图小游戏)
实现方式 1.引入UGUI自带的事件系统 UnityEngine.EventSystems 2.为我们的类添加接口 IBeginDragHandler, IDragHandler, IEndDragH ...