有幸看到piliskys的文章,正好解決導(dǎo)出的問(wèn)題,在此謝過(guò)!
       
原文地址http://www.tkk7.com/piliskys/archive/2005/11/23/21095.aspx
      jsp導(dǎo)出excel有很多種方法,在此介紹本人認(rèn)為簡(jiǎn)單的一種,
前提:能在jsp頁(yè)面取到要導(dǎo)出的內(nèi)容,即 request能得到導(dǎo)出的數(shù)據(jù),然后代碼如下
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page language="java"  import="java.util.*,
                                  org.apache.poi.hssf.usermodel.HSSFWorkbook,
                                  org.apache.poi.hssf.usermodel.HSSFSheet,
                                  org.apache.poi.hssf.usermodel.HSSFRow,
                                  org.apache.poi.hssf.usermodel.HSSFCell,
                                  java.text.DecimalFormat
"
%>
<%
    response.reset();
    response.setContentType(
"application/msexcel");
    response.setHeader(
"Content-disposition","inline;filename=untitled.xls");//
定義文件名
    DecimalFormat f 
= new DecimalFormat("#,##0.00"
);
    HSSFWorkbook wb 
= new
 HSSFWorkbook();
    HSSFSheet sheet 
= wb.createSheet("sheet1"
);
    
String[] taxpayerid = request.getParameterValues("taxpayerid"
);
    
String[] taxpayername = request.getParameterValues("taxpayername"
);
    
String[] tax = request.getParameterValues("tax"
);
    
String[] taxreduce = request.getParameterValues("taxreduce"
);
    
String[] deratereasonname = request.getParameterValues("deratereasonname"
);
    
String[] orgdeptname = request.getParameterValues("orgdeptname"
);
    
String[] operatortime = request.getParameterValues("operatortime"
);
    
String[] declaredate = request.getParameterValues("declaredate"
);
    
String[] taxtermbegin = request.getParameterValues("taxtermbegin"
);
    
String[] taxtermend = request.getParameterValues("taxtermend"
);

//
以下以寫(xiě)表頭
        
//
表頭為第一行
      HSSFRow row 
= sheet.createRow((short) 0
);
//
定義10列
         HSSFCell cell1 
= row.createCell((short) 0
);
        HSSFCell cell2 
= row.createCell((short) 1
);
        HSSFCell cell3 
= row.createCell((short) 2
);
        HSSFCell cell4 
= row.createCell((short) 3
);
        HSSFCell cell5 
= row.createCell((short) 4
);
        HSSFCell cell6 
= row.createCell((short) 5
);
        HSSFCell cell7 
= row.createCell((short) 6
);
        HSSFCell cell8 
= row.createCell((short) 7
);
        HSSFCell cell9 
= row.createCell((short) 8
);
        HSSFCell cell10 
= row.createCell((short) 9
);

        cell1.setEncoding((short) 
1
);
        cell1.setCellType(
1
);
        cell2.setEncoding((short) 
1
);
        cell2.setCellType(
1
);
        cell3.setEncoding((short) 
1
);
        cell3.setCellType(
1
);
        cell4.setEncoding((short) 
1
);
        cell4.setCellType(
1
);
        cell5.setEncoding((short) 
1
);
        cell5.setCellType(
0
);
        cell6.setEncoding((short) 
1
);
        cell6.setCellType(
1
);
        cell7.setEncoding((short) 
1
);
        cell7.setCellType(
1
);
        cell8.setEncoding((short) 
1
);
        cell8.setCellType(
1
);
        cell9.setEncoding((short) 
1
);
        cell9.setCellType(
1
);
        cell10.setEncoding((short) 
1
);
        cell10.setCellType(
1
);
//
定義表頭的內(nèi)容
        cell1.setCellValue(
"納稅人管理碼"
);
        cell2.setCellValue(
"納稅人名稱"
);
        cell3.setCellValue(
"稅種"
);
        cell4.setCellValue(
"減免金額"
);
        cell5.setCellValue(
"減免原因"
);
        cell6.setCellValue(
"征收單位"
);
        cell7.setCellValue(
"操作日期"
);
        cell8.setCellValue(
"申報(bào)日期"
);
        cell9.setCellValue(
"所屬期起"
);
        cell10.setCellValue(
"所屬期止"
);


    
for(int i= 0; i < taxpayerid.length; i++
){
//
定義數(shù)據(jù)從第二行開(kāi)始       
  row 
= sheet.createRow((short) i+1
);
                cell1 
= row.createCell((short) 0
);
                cell2 
= row.createCell((short) 1
);
                cell3 
= row.createCell((short) 2
);
                cell4 
= row.createCell((short) 3
);
                cell5 
= row.createCell((short) 4
);
                cell6 
= row.createCell((short) 5
);
                cell7 
= row.createCell((short) 6
);
                cell8 
= row.createCell((short) 7
);
                cell9 
= row.createCell((short) 8
);
                cell10 
= row.createCell((short) 9
);

               cell1.setEncoding((short) 
1
);
               cell1.setCellType(
1
);
               cell2.setEncoding((short) 
1
);
               cell2.setCellType(
1
);
               cell3.setEncoding((short) 
1
);
               cell3.setCellType(
1
);
               cell4.setEncoding((short) 
1
);
               cell4.setCellType(
1
);
               cell5.setEncoding((short) 
1
);
               cell5.setCellType(
0
);
               cell6.setEncoding((short) 
1
);
               cell6.setCellType(
1
);
               cell7.setEncoding((short) 
1
);
               cell7.setCellType(
1
);
               cell8.setEncoding((short) 
1
);
               cell8.setCellType(
1
);
               cell9.setEncoding((short) 
1
);
               cell9.setCellType(
1
);
               cell10.setEncoding((short) 
1
);
               cell10.setCellType(
1
);

//
填充內(nèi)容

        cell1.setCellValue(taxpayerid[i]);
        cell2.setCellValue(taxpayername[i]);
        cell3.setCellValue(tax[i]);
        cell4.setCellValue(f.parse(taxreduce[i].trim()).doubleValue());
        cell5.setCellValue(deratereasonname[i]);
        cell6.setCellValue(orgdeptname[i]);
        cell7.setCellValue(operatortime[i].substring(
0,16
));
        cell8.setCellValue(declaredate[i].substring(
0,16
));
        cell9.setCellValue(taxtermbegin[i].substring(
0,16
));
        cell10.setCellValue(taxtermend[i].substring(
0,16
));
    }
    wb.write(response.getOutputStream());
    response.getOutputStream().flush();
    response.getOutputStream().close();
%>


代碼比較簡(jiǎn)單,首先把取得到的數(shù)據(jù)定義為一系列數(shù)組,然后定義表頭,然后把取得的數(shù)據(jù)做為excel數(shù)據(jù)對(duì)應(yīng)的放入,對(duì)poi有何疑問(wèn)請(qǐng)參考http://java2.5341.com/3.html
posted on 2005-11-23 10:27 霹靂火