poi 导出 excel
- private void exportAssetExcel(HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- String ogid = RequestUtil.getRequestBytes(request, "ogid").trim();
- String objectgroupname = RequestUtil.getRequestBytes(request, "objectgroupname").trim();
- String excelTitle = new String(objectgroupname.getBytes("ISO-8859-1"), "utf-8");
- if(objectgroupname==null || objectgroupname==""){
- objectgroupname = "备播单";
- }
- ObjectGroupRObjectMgr ogroMgr = new ObjectGroupRObjectMgr();
- BaseDAO basedao = ogroMgr.getBasedao();
- String strCondition= "select "
- + " ogro.new_assetname,"
- + " a.assetname,"
- + " m.filepath,"
- + " a.programtype,"
- + " a.videotype,"
- + " a.duration,"
- + " ogro.asset_price,"
- + " ogro.online_time,"
- + " ogro.asset_path"
- + " from "
- + " Objectgrouprobject ogro,"
- + " movie m,"
- + " asset a "
- + " where "
- + " ogro.objectid = a.assetid"
- + " and a.assetid = m.objectguid"
- + " and ogro.ogid = ?"
- + " order by ogro.ordernum asc";
- List<Comparable> paramlist = new ArrayList();
- paramlist.add(ogid);
- // 关联objectgrouprobject asset查询
- List<Object[]> list = basedao.SQLQuery(strCondition, paramlist);
- //创建一个workbook 对应一个excel文件
- HSSFWorkbook book = new HSSFWorkbook();
- //创建工作簿,对应一个sheet
- HSSFSheet sheet = book.createSheet(excelTitle);
- sheet.setColumnWidth((short)0,(short)1000);
- sheet.setColumnWidth((short)1,(short)4000);
- sheet.setColumnWidth((short)2,(short)4000);
- sheet.setColumnWidth((short)3,(short)10000);
- sheet.setColumnWidth((short)4,(short)4000);
- sheet.setColumnWidth((short)5,(short)4000);
- sheet.setColumnWidth((short)6,(short)4000);
- sheet.setColumnWidth((short)7,(short)2000);
- sheet.setColumnWidth((short)8,(short)4000);
- sheet.setColumnWidth((short)9,(short)10000);
- sheet.setColumnWidth((short)10,(short)10000);
- //设置格式
- HSSFCellStyle style_1 = book.createCellStyle();
- style_1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFFont f = book.createFont();
- f.setFontHeightInPoints((short) 14);//字号
- f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
- style_1.setFont(f);
- //创建第一行 合并单元格
- HSSFRow firstRow = sheet.createRow(0);
- sheet.addMergedRegion(new Region(0, (short)0, 0, (short)10));
- //组织第一行的数据
- HSSFCell cell = firstRow.createCell((short) 0);
- cell.setCellValue(excelTitle);
- cell.setCellStyle(style_1);
- // cell.setCellType(HSSFCell.ENCODING_UTF_16);
- //创建第二行 合并单元格
- HSSFRow secondeRow = sheet.createRow(1);
- sheet.addMergedRegion(new Region(1, (short)0, 1, (short)8));
- //组织第二行的数据
- Calendar c = Calendar.getInstance();
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- String calendarStr = year + "年" + month + "月" + day + "日";
- cell = secondeRow.createCell((short)0);
- cell.setCellValue("制表时间: "+ calendarStr);
- cell = secondeRow.createCell((short)9);
- cell.setCellValue("制表部门:内容(集成)中心");
- cell = secondeRow.createCell((short)10);
- cell.setCellValue("节目单类型:上片单");
- //组织第三行数据
- HSSFRow thirdRow = sheet.createRow(2);
- HSSFCellStyle style_2 = book.createCellStyle();
- style_2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- HSSFFont f2 = book.createFont();
- f2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
- style_2.setFont(f2);
- HSSFCell third_cell = thirdRow.createCell((short)0);
- third_cell = thirdRow.createCell((short)1);
- third_cell.setCellValue("节目名称");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)2);
- third_cell.setCellValue("正题名称");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)3);
- third_cell.setCellValue("文件名称");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)4);
- third_cell.setCellValue("节目类型");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)5);
- third_cell.setCellValue("高标清");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)6);
- third_cell.setCellValue("节目时长");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)7);
- third_cell.setCellValue("价格");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)8);
- third_cell.setCellValue("上片时间");
- third_cell = thirdRow.createCell((short)9);
- third_cell.setCellValue("节目路径");
- third_cell.setCellStyle(style_2);
- third_cell = thirdRow.createCell((short)10);
- third_cell.setCellValue("备注");
- third_cell.setCellStyle(style_2);
- int listSize = 0;
- if(list!=null && list.size()>0){
- listSize = list.size();
- HSSFCellStyle style_3 = book.createCellStyle();
- style_3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- style_3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- //组织节目数据
- for(int i=0; i<listSize; i++){
- Object[] obj = list.get(i);
- HSSFRow row = sheet.createRow(i+3);
- row.setHeight((short)800);
- HSSFCell i_cell = row.createCell((short)0);
- i_cell.setCellValue(""+(i+1));
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)1);
- i_cell.setCellValue(String.valueOf(obj[0]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)2);
- i_cell.setCellValue(String.valueOf(obj[1]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)3);
- i_cell.setCellValue(String.valueOf(obj[2]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)4);
- i_cell.setCellValue(String.valueOf(obj[3]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)5);
- i_cell.setCellValue(String.valueOf(obj[4]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)6);
- i_cell.setCellValue(String.valueOf(obj[5]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)7);
- i_cell.setCellValue(String.valueOf(obj[6]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)8);
- i_cell.setCellValue(String.valueOf(obj[7]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)9);
- i_cell.setCellValue(String.valueOf(obj[8]).trim());
- i_cell.setCellStyle(style_3);
- i_cell = row.createCell((short)10);
- i_cell.setCellValue("");
- i_cell.setCellStyle(style_3);
- }
- }
- HSSFRow row_one = sheet.createRow(3+listSize);
- HSSFCell cell_one = row_one.createCell((short)0);
- cell_one.setCellValue("编辑:");
- cell_one = row_one.createCell((short)9);
- cell_one.setCellValue("审核:");
- sheet.addMergedRegion(new Region(3+listSize, (short)0, 3+listSize, (short)8));
- sheet.addMergedRegion(new Region(3+listSize, (short)9, 3+listSize, (short)10));
- HSSFRow row_two = sheet.createRow(4+listSize);
- HSSFCell cell_two = row_two.createCell((short)0);
- cell_two.setCellValue("制作:");
- sheet.addMergedRegion(new Region(4+listSize, (short)0, 4+listSize, (short)10));
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- book.write(os);
- } catch (IOException e) {
- e.printStackTrace();
- }
- byte[] content = os.toByteArray();
- InputStream is = new ByteArrayInputStream(content);
- // 设置response参数,可以打开下载页面
- response.reset();
- response.setContentType("application/vnd.ms-excel;charset=utf-8");
- String filename = objectgroupname + ".xls";
filename = encodeFilename(filename, request);- response.setHeader("Content-Disposition", "attachment;filename="+ filename);
- response.setCharacterEncoding("utf-8");
- ServletOutputStream out = response.getOutputStream();
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- try {
- bis = new BufferedInputStream(is);
- bos = new BufferedOutputStream(out);
- byte[] buff = new byte[2048];
- int bytesRead;
- // Simple read/write loop.
- while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
- bos.write(buff, 0, bytesRead);
- }
- } catch (final IOException e) {
- throw e;
- } finally {
- if (bis != null)
- bis.close();
- if (bos != null)
- bos.close();
- }
- }
- public static String encodeFilename(String filename, HttpServletRequest request) {
/**
* 获取客户端浏览器和操作系统信息
* 在IE浏览器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
* 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
*/
String agent = request.getHeader("USER-AGENT");
try {
if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
String newFileName = URLEncoder.encode(filename, "UTF-8");
newFileName = StringUtils.replace(newFileName, "+", "%20");
if (newFileName.length() > 150) {
newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
newFileName = StringUtils.replace(newFileName, " ", "%20");
}
return newFileName;
}
if ((agent != null) && (-1 != agent.indexOf("Mozilla")))
return MimeUtility.encodeText(filename, "UTF-8", "B");
return filename;
} catch (Exception ex) {
return filename;
}
}
以上是java部分的代码, 导出按照workbook-->sheet-->row-->cell的大体流程走, 再加上格式设置, 加上值等等
前端需要弹出下载框,可以用:
- window.open("objectgroup.do"+"?action=exportAssetExcel&ogid="+ogid+"&objectgroupname="+objectgroupname);
或者用form提交
jsp页面
- <form action="exportExcel_program" method="post" id="exportExcel_program_form">
- <input type="hidden" name="searchXML" id="searchXML"/>
- </form>
js处理
- $("#searchXML").val(searchXML);
- $("#exportExcel_program_form").submit();
这里参数可能有中文,所以java部分要处理,可以参见上面java代码
poi 导出 excel的更多相关文章
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
- POI导出excel的简单demo
目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...
- [转载]poi导出excel,可以自定义保存路径
poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的 1.ac ...
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
- 重构:以Java POI 导出EXCEL为例2
前言 上一篇博文已经将一些对象抽象成成员变量以及将一些代码块提炼成函数.这一节将会继续重构原有的代码,将一些函数抽象成类,增加成员变量,将传入的参数合成类等等. 上一篇博文地址:http://www. ...
- 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结
poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产生内存溢出问题,SXSSFWorkbook是一种大数据量导出格式,csv是另一种excel导出的一种轻快的 ...
- 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结
1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...
随机推荐
- Android开发之ProgressDialog在独立Thread线程中更新进度
简单的需求:在一个工作Thread中更新进度对话框ProgressDialog 遇到的问题: 1,创建需要Context,这个需要传进来 2,Thread中不能创建ProgressDialog,否则需 ...
- Android 模拟器 获得 root权限
启动一个模拟器,开始-运行-输入cmd,打开dos,依次输入 adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ...
- 【原】SQL ROW_NUMBER() OVER
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) SELECT ROW_NUMBER() OVER(ORDER BY CASE Col ...
- js两种定义函数、继承方式及区别
一:js两种定义函数的方式及区别 1:函数声明: function sayA() { alert("i am A"); } 2:函数表达式: var sayB = function ...
- 对C#对象的Shallow、Deep Cloning认识【转】
好像园内比较多博客对 Shallow.Deep Cloning的翻译是深拷贝.浅拷贝,当时我懵了,这个叫法怎么怪怪的. 就好像看军情观察室,台湾评论员,导弹叫飞弹. 至于它们的区别,一张图就可以解释. ...
- datagrid 动态列
var options={}; $(function(){ var myNj = 9; //初始化 $("#disgrid").datagrid({ type: 'POST', n ...
- 【PRML读书笔记-Chapter1-Introduction】1.5 Decision Theory
初体验: 概率论为我们提供了一个衡量和控制不确定性的统一的框架,也就是说计算出了一大堆的概率.那么,如何根据这些计算出的概率得到较好的结果,就是决策论要做的事情. 一个例子: 文中举了一个例子: 给定 ...
- 【NS2仿真】TCP与UDP混合
# # ftp # \ # tcp # \ # n0 sink # \ / # \ / # n1---5M 2ms---n3 # / \ # / \ # n2 null # / # udp # / # ...
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...
- 自动化回归测试案例评价标准 MeRest
自动化回归测试案例评价标准试图定义不同维度来评价自动化案例的优劣,作为后续我们评判讨论测试框架.测试技术和测试案例编写模式的基础.那什么是好的自动化回归测试案例呢?简而言之,就是投资回报率高的案例,因 ...