Apache-POI实现Excel文档的导出下载功能

  1. 一、加载入POI的所有jar包:poi-3.9-20121203.jar;poi-examples-3.9-20121203.jar;poi-excelant-3.9-20121203.jar;poi-ooxml-3.9-20121203.jar;poi-ooxml-schemas-3.9-20121203.jar;poi-scratchpad-3.9-20121203.jar;commons-codec-1.5.jar;commons-logging-1.1.jar;junit-3.8.1.jar;log4j-1.2.13.jar;  
  2.   
  3. 二、建立一个web项目   在WEB-INF中的bin中把jar包导入  
  4.   
  5. 三、编写一个按钮<input type="button" value="导出" οnclick="outputData()"/>  
  6.   
  7. function outputData(){  
  8.         window.location='XXXAction!outputStatisticsData';  
  9. }  
  10. 四、在Action之中添加代码  :  
  11.   
  12.     @SuppressWarnings("deprecation")  
  13.     public void outputStatisticsData() throws JSONException{  
  14.                    
  15.         //创建response对象  
  16.         HttpServletResponse response = ServletActionContext.getResponse() ;  
  17.         //创建一个新的Excel  
  18.         HSSFWorkbook workBook = new HSSFWorkbook() ;  
  19.         //创建sheet页  
  20.         HSSFSheet sheet0 = workBook.createSheet();  
  21.         //sheet页名称  
  22.         workBook.setSheetName(0"短期投资上报导出数据 ");  
  23.         //设置Sheet页为默认  
  24.         sheet0.setSelected(true);  
  25.         //设置放大比例  
  26.         //sheet0.setZoom(6,5);  
  27.         //创建header页  
  28.         HSSFHeader header = sheet0.getHeader();  
  29.         //设置标题居中  
  30.         header.setCenter("标题");  
  31.                    
  32. //      //让Sheet页自适应页面大小  
  33. //      PrintSetup ps = sheet0.getPrintSetup();  
  34. //      ps.setFitHeight((short)1);  
  35. //      ps.setFitWidth((short)1);  
  36.         //设置自动换行  
  37.         sheet0.setAutobreaks(true) ;  
  38.                    
  39.         //设置列宽  
  40.         sheet0.setColumnWidth((short)0,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  41.         sheet0.setColumnWidth((short)1,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  42.         sheet0.setColumnWidth((short)2,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  43.         sheet0.setColumnWidth((short)3,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  44.         sheet0.setColumnWidth((short)4,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  45.         sheet0.setColumnWidth((short)5,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  46.         sheet0.setColumnWidth((short)6,(short)(35.7*80));  //设置列宽(35.7*n,其中n是像素值)  
  47.         sheet0.setColumnWidth((short)7,(short)(35.7*100)); //设置列宽(35.7*n,其中n是像素值)  
  48.         sheet0.setColumnWidth((short)8,(short)(35.7*100)); //设置列宽(35.7*n,其中n是像素值)  
  49.         sheet0.setColumnWidth((short)9,(short)(35.7*100)); //设置列宽(35.7*n,其中n是像素值)  
  50.         sheet0.setColumnWidth((short)10,(short)(35.7*100));//设置列宽(35.7*n,其中n是像素值)  
  51.         sheet0.setColumnWidth((short)11,(short)(35.7*100));//设置列宽(35.7*n,其中n是像素值)  
  52.         sheet0.setColumnWidth((short)12,(short)(35.7*100));//设置列宽(35.7*n,其中n是像素值)  
  53.                    
  54.         //设置第一行为上报表的表头   
  55.         HSSFRow row0 = sheet0.createRow((short)0);                      //取得Excel对象的第一行  
  56.         row0.setHeightInPoints((short)30.0) ;  
  57.         HSSFCell cell0 = row0.createCell((short)0) ;//合并单元格 第一行就只有一条数据  
  58.         cell0.setCellValue("村集体经济组织短期投资最终汇总表") ;  
  59.                    
  60.         //创建Excel合并单元格对象  
  61.         sheet0.addMergedRegion(new CellRangeAddress(  
  62.                 0,              //firstRow这是从第一列开始  
  63.                 0,              //lastRow 这是终结的列号  
  64.                 0,              //firstCol这是第一列的号码    
  65.                 12));           //lastCol这是要合并的最后一列  
  66.                    
  67.         //创建单元格的格式  
  68.         CellStyle cellStyleHeader = workBook.createCellStyle() ;  
  69.         //设置单元格的水平居中  
  70.         cellStyleHeader.setAlignment(XSSFCellStyle.ALIGN_CENTER) ;  
  71.         //设置单元格的垂直居中  
  72.         cellStyleHeader.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER) ;  
  73.         //设置单元格的北京颜色25%的灰色  
  74.         //cellStyleHeader.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());  
  75.         cellStyleHeader.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());  
  76.         cellStyleHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);  
  77.                    
  78.         //创建字体格式  
  79.         Font fontHeader = workBook.createFont() ;  
  80.         //设置字体加粗显示  
  81.         fontHeader.setBoldweight(Font.BOLDWEIGHT_BOLD) ;      
  82.         //设置字体的字号size  
  83.         fontHeader.setFontHeightInPoints((short)20) ;  
  84.         //设置字体的文字类型  
  85.         fontHeader.setFontName("黑体");  
  86.         //fontHeader.setItalic(true) ;//设置是否为斜体  
  87.         cellStyleHeader.setFont(fontHeader) ;  
  88.         cell0.setCellStyle(cellStyleHeader) ;  
  89.                    
  90.         //创建Excel对象的第二行内容  
  91.         HSSFRow row1 = sheet0.createRow((short)1) ;  
  92.         row1.setHeightInPoints((short)16);  
  93.         //获取到当前组织形式的组织名称  
  94.         String orgName = getiCurrentinvestService().getOrgName() ;  
  95.         HSSFCell cell1_0 = row1.createCell((short)0);  
  96.         cell1_0.setCellValue("集体经济组织名称:"+orgName) ;  
  97.         HSSFCell cell1_1 = row1.createCell((short)5) ;  
  98.         Date date = new Date() ;  
  99.         SimpleDateFormat smdFormat = new SimpleDateFormat("yyyy-MM-dd");  
  100.         cell1_1.setCellValue("时间: " +smdFormat.format(date)) ;  
  101.         HSSFCell cell1_2 = row1.createCell((short)9) ;  
  102.         cell1_2.setCellValue("金额单位:元") ;  
  103.         sheet0.addMergedRegion(new CellRangeAddress(  
  104.                 1,              //firstRow这是从第2列开始  
  105.                 1,              //lastRow 这是终结的列号  
  106.                 0,              //firstCol这是第2列的号码    
  107.                 4));            //lastCol这是要合并的最后一列  
  108.         sheet0.addMergedRegion(new CellRangeAddress(  
  109.                 1,              //firstRow这是从第2列开始  
  110.                 1,              //lastRow 这是终结的列号  
  111.                 5,              //firstCol这是第2列的号码    
  112.                 8));            //lastCol这是要合并的最后一列  
  113.         sheet0.addMergedRegion(new CellRangeAddress(  
  114.                 1,              //firstRow这是从第2列开始  
  115.                 1,              //lastRow 这是终结的列号  
  116.                 9,              //firstCol这是第2列的号码    
  117.                 12));           //lastCol这是要合并的最后一列  
  118.                    
  119.         CellStyle titleCellStyle = workBook.createCellStyle() ;  
  120.         titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER) ;  
  121.         titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  122.         titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());  
  123.         titleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);  
  124.         Font fontContent = workBook.createFont() ;  
  125.         fontContent.setFontName("楷体");  
  126.         fontContent.setFontHeightInPoints((short)10) ;  
  127.         fontContent.setBoldweight(Font.BOLDWEIGHT_BOLD) ;  
  128.         titleCellStyle.setFont(fontContent) ;  
  129.         cell1_0.setCellStyle(titleCellStyle);  
  130.         cell1_1.setCellStyle(titleCellStyle) ;  
  131.         cell1_2.setCellStyle(titleCellStyle) ;  
  132.                    
  133.         //设置第三行数据  
  134.         HSSFRow row2 = sheet0.createRow((short)2) ;  
  135.         HSSFCell cell2_0 = row2.createCell((short)0);  
  136.         sheet0.addMergedRegion(new CellRangeAddress(2300));  
  137.         cell2_0.setCellValue("序号") ;  
  138.         HSSFCell cell2_1 = row2.createCell((short)1);  
  139.         sheet0.addMergedRegion(new CellRangeAddress(2311));  
  140.         cell2_1.setCellValue("组织名称") ;  
  141.         HSSFCell cell2_2 = row2.createCell((short)2);  
  142.         sheet0.addMergedRegion(new CellRangeAddress(2322));  
  143.         cell2_2.setCellValue("证券名称") ;  
  144.         HSSFCell cell2_3 = row2.createCell((short)3);  
  145.         sheet0.addMergedRegion(new CellRangeAddress(2333));  
  146.         cell2_3.setCellValue("证券编号");  
  147.         HSSFCell cell2_4 = row2.createCell((short)4);  
  148.         sheet0.addMergedRegion(new CellRangeAddress(2344));  
  149.         cell2_4.setCellValue("证券单位") ;  
  150.         HSSFCell cell2_5 = row2.createCell((short)5);  
  151.         sheet0.addMergedRegion(new CellRangeAddress(2256));  
  152.         cell2_5.setCellValue("期初结余");  
  153.         HSSFCell cell2_7 = row2.createCell((short)7);  
  154.         sheet0.addMergedRegion(new CellRangeAddress(2278));  
  155.         cell2_7.setCellValue("本期证券购入");  
  156.         HSSFCell cell2_9 = row2.createCell((short)9);  
  157.         sheet0.addMergedRegion(new CellRangeAddress(22910));  
  158.         cell2_9.setCellValue("本期证券出售");  
  159.         HSSFCell cell2_11 = row2.createCell((short)11);  
  160.         sheet0.addMergedRegion(new CellRangeAddress(221112));  
  161.         cell2_11.setCellValue("期末结余");  
  162.                    
  163.         //设置第四行数据   
  164.         HSSFRow row3 = sheet0.createRow((short)3);  
  165.         HSSFCell cell3_5 = row3.createCell((short)5);  
  166.         cell3_5.setCellValue("期初金额");  
  167.         HSSFCell cell3_6 = row3.createCell((short)6);  
  168.         cell3_6.setCellValue("期初数量");  
  169.         HSSFCell cell3_7 = row3.createCell((short)7);  
  170.         cell3_7.setCellValue("证券购入金额");  
  171.         HSSFCell cell3_8 = row3.createCell((short)8);  
  172.         cell3_8.setCellValue("证券购入数量");  
  173.         HSSFCell cell3_9 = row3.createCell((short)9);  
  174.         cell3_9.setCellValue("证券出售数量");  
  175.         HSSFCell cell3_10 = row3.createCell((short)10);  
  176.         cell3_10.setCellValue("证券出售金额");  
  177.         HSSFCell cell3_11 = row3.createCell((short)11);  
  178.         cell3_11.setCellValue("期末结余数量");  
  179.         HSSFCell cell3_12 = row3.createCell((short)12);  
  180.         cell3_12.setCellValue("期末结余金额");  
  181.                    
  182.         //设置副标题的格式  
  183.         CellStyle subTitleCellStyle = workBook.createCellStyle();  
  184.         subTitleCellStyle.setFont(fontContent);  
  185.         subTitleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER) ;  
  186.         subTitleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  187.         cell2_0.setCellStyle(subTitleCellStyle) ;  
  188.         cell2_1.setCellStyle(subTitleCellStyle) ;  
  189.         cell2_2.setCellStyle(subTitleCellStyle) ;  
  190.         cell2_3.setCellStyle(subTitleCellStyle) ;  
  191.         cell2_4.setCellStyle(subTitleCellStyle) ;  
  192.         cell2_5.setCellStyle(subTitleCellStyle) ;  
  193.         cell2_7.setCellStyle(subTitleCellStyle) ;  
  194.         cell2_9.setCellStyle(subTitleCellStyle) ;  
  195.         cell2_11.setCellStyle(subTitleCellStyle) ;  
  196.         cell3_5.setCellStyle(subTitleCellStyle) ;  
  197.         cell3_6.setCellStyle(subTitleCellStyle) ;  
  198.         cell3_7.setCellStyle(subTitleCellStyle) ;  
  199.         cell3_8.setCellStyle(subTitleCellStyle) ;  
  200.         cell3_9.setCellStyle(subTitleCellStyle) ;  
  201.         cell3_10.setCellStyle(subTitleCellStyle) ;  
  202.         cell3_11.setCellStyle(subTitleCellStyle) ;  
  203.         cell3_12.setCellStyle(subTitleCellStyle) ;  
  204.                    
  205.                    
  206. //**************************************下面利用response对象利用输出流输出到客户端********************************//  
  207.         //通过Response把数据以Excel格式保存  
  208.         response.reset();  
  209.         //设置response流信息的头类型,MIME码  
  210.         response.setContentType("application/msexcel;charset=UTF-8");  
  211.         try {  
  212.             response.addHeader("Content-Disposition""attachment;filename=\""  
  213.                     + new String(("短期投资上报信息导出表" +new Date().toString()+ ".xls").getBytes("GBK"),  
  214.                             "ISO8859_1") + "\"");  
  215.             //创建输出流对象   
  216.             OutputStream out = response.getOutputStream();  
  217.             //将创建的Excel对象利用二进制流的形式强制输出到客户端去  
  218.             workBook.write(out);  
  219.             //强制将数据从内存中保存  
  220.             out.flush();  
  221.             out.close();  
  222.         } catch (Exception e) {  
  223.             e.printStackTrace();  
  224.         }  
  225.     }  
  226. 五、保存之后 发布项目 点击按钮导出按钮,程序实现思想是利用POI在服务器端完成Excel对象的建立,然后过输出流的对象将处理过的Excel workBook给输出到客户端去,通过配置response对象的头信息,使客户端用户利用MIME进行识别,打开Excel对象完成操作,这样远端用户就可以完成数据的现在保存和下载了。  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值