jsp导出excel文件,使用POI的

Posted on 2006-10-25 17:55 Hally 阅读(8204) 评论(4)  编辑  收藏 所属分类: 学习随笔

 


<% @ 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 " %>
<%
    response.setContentType(
" APPLICATION/OCTET-STREAM " );
    response.setHeader(
" Content-Disposition " ,
            
" attachment; filename=\ " test.xls "  +

            
" \ "" );

    HSSFWorkbook wb 
=   new  HSSFWorkbook();
    HSSFSheet sheet 
=  wb.createSheet( " sheet1 " );

    
// 以下以写表头
    
// 表头为第一行
    HSSFRow row  =  sheet.createRow(( short 0 );

    HSSFCell cell1 
=  row.createCell(( short 0 );
    HSSFCell cell2 
=  row.createCell(( short 1 );
    HSSFCell cell3 
=  row.createCell(( short 2 );

    cell1.setEncoding((
short 1 );
    cell1.setCellType(
1 );
    cell2.setEncoding((
short 1 );
    cell2.setCellType(
1 );
    cell3.setEncoding((
short 1 );
    cell3.setCellType(
1 );

    
// 定义表头的内容
    cell1.setCellValue( " 测试 " );
    cell2.setCellValue(
" 测试2 " );
    cell3.setCellValue(
" 测试3 " );

    
for  ( int  i  =   0 ; i  <   4 ; i ++ {
        
// 定义数据从第二行开始       
        row  =  sheet.createRow(( short ) i  +   1 );
        cell1 
=  row.createCell(( short 0 );
        cell2 
=  row.createCell(( short 1 );
        cell3 
=  row.createCell(( short 2 );

        cell1.setEncoding((
short 1 );
        cell1.setCellType(
1 );
        cell2.setEncoding((
short 1 );
        cell2.setCellType(
1 );
        cell3.setEncoding((
short 1 );
        cell3.setCellType(
1 );

        
// 填充内容

        cell1.setCellValue(
" ggg " );
        cell2.setCellValue(
" 00000 " );
        cell3.setCellValue(
" adfasdf " );

    }

    wb.write(response.getOutputStream());
    response.getOutputStream().flush();
    response.getOutputStream().close();
%>

Feedback

# re: jsp导出excel文件,使用POI的  回复  更多评论   

2006-10-25 18:04 by Hally
poi 下载地址

http://jakarta.apache.org/site/downloads/downloads_poi.cgi

# re: jsp导出excel文件,使用POI的  回复  更多评论   

2007-07-11 13:06 by adfasdf
afasfasfd

# re: jsp导出excel文件,使用POI的  回复  更多评论   

2008-05-25 20:47 by eter
@adfasdf

# re: jsp导出excel文件,使用POI的  回复  更多评论   

2009-03-02 14:22 by wangdong
顶呀,搞出来是乱码

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问  
 

posts - 43, comments - 200, trackbacks - 0, articles - 2

Copyright © Hally