如题,实现这个功能(excel2003和2007均适用),使用的第三方jar包是 poi ,主要是记录一下做过的东西,以便日后用。

 

一。jsp页面 :

<form id ="exportExcel" name="exportExcel" action="exportExcel.do" method="post">

                       <input type="hidden" id ="A" name="param1" >

                       <input type="hidden" id ="B" name="param2" >

                       <input type="hidden" id ="C" name="param3" >                

</form>

param1,2,3是参数,传到action。

 

二。struts2的xml配置 :

  <!-- 导出Excel -->

  <action name="exportExcel" method="exportExcel" class="testAction">

             <result name="success" type="stream">

                 <param name="contentType">application/vnd.ms-excel</param>

                 <param name="contentDisposition">p_w_upload;filename="${fileName}"</param>

                 <param name="inputName">excelFile</param>

             </result>

      </action> 

 

 

三。 action端,需要两个方法,一个是exportExcel()和struts配置文件指明的stream流方法excelFile().

exportExcel()

public String exportWaitOrderExcel(){

               String name ="产品列表.xls";

               try {

                       name = java.net.URLEncoder.encode(name, "UTF-8");

                       fileName = new String(name.getBytes(), "iso-8859-1");

               } catch (UnsupportedEncodingException e) {

                       log4j.error("字符转码失败");

               }

               return SUCCESS;

        }


 

excelFile()

public InputStream getExcelFile()       //getExcelFile()一定要与excelFile对应,否则会出现异常

        {

               //第一步:接收参数,查询产品列表信息

               String param1= getRequest().getParameter("param1");

               String param2= getRequest().getParameter("param2");

               String param3= getRequest().getParameter("param3");

               List<ProductBean> productList = new ArrayList<ProductBean>();

 

               Map<Object, Object> map = new HashMap<Object, Object>();

 

               if (param1!= null && !param1.equals("")) {

                       map.put("param1", param1);

 

               }

 

               if (param2!= null && !param2.equals("")) {

                       map.put("param2", param2);

 

               }

                if (param3!= null && !param3.equals("")) {

                       map.put("param3", param3);

 

               }

 

               productList = productServicr.searchProductList(map);

 

               //第二步:构建excel表格,封装数据到excel                    

               HSSFWorkbook workbook = new HSSFWorkbook();

               HSSFSheet sheet = workbook.createSheet("sheet1");

 

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

              

               HSSFCell cell = row.createCell(0);//第一列

               cell.setCellValue("产品列表"); //行内容

              

               HSSFCellStyle style = workbook.createCellStyle(); //创建样式

               HSSFFont font = workbook.createFont(); //创建字体样式

                       font.setFontHeight((short)(20*20)); //字体

                       font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //加粗

                       style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中

               style.setFont(font); //设置字体样式  

               cell.setCellStyle(style); //设置样式

               sheet.addMergedRegion(new org.apache.poi.ss.util.Region(0,(short)0,0,(short)4)); //合并列

               //String menus = "产品名称,产品类型,产品描述";

              

               row = sheet.createRow(1);//创建第一行

               cell = row.createCell(0); //创建第一列

               cell.setCellValue(new HSSFRichTextString("产品名称"));

               cell = row.createCell(1);//创建第二列

               cell.setCellValue(new HSSFRichTextString("产品类型"));      

               cell = row.createCell(2);//

               cell.setCellValue(new HSSFRichTextString("产品描述"));

              

               for (int i = 0; i < productList.size(); i++) {

                       ProductBean product= list.get(i);

                       row=sheet.createRow(i+2);//创建第i+1行                      

                      

                       cell=row.createCell(0);//创建第一列

                       cell.setCellValue(product.getName());                

                       cell=row.createCell(1);//创建第二列

                       cell.setCellValue(product.getCategory()                     

                       cell=row.createCell(2);

                       cell.setCellValue(product.getDescription());                

               }

              

               //第三步:写入输出流

               ByteArrayOutputStream baos = new ByteArrayOutputStream();

 

               try {

                       workbook.write(baos);//写入

               } catch (IOException e) {

                       e.printStackTrace();

               }

 

               byte[] ba = baos.toByteArray(); 

               ByteArrayInputStream bais = new ByteArrayInputStream(ba); 

               return bais;

        }