码迷,mamicode.com
首页 > 其他好文 > 详细

简单的创建一张excel表

时间:2014-07-26 03:11:08      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:public   excel   npoi   

bubuko.com,布布扣

*类库去我的下载里面下载

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Data;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Excel();
    }
    void Excel()
    {
        HSSFWorkbook wordbook = new HSSFWorkbook();//创建Workbook对象
        ISheet sheet = wordbook.CreateSheet("sheet1");//创建工作表
        wordbook.CreateSheet("sheet2");
        wordbook.CreateSheet("sheet3");
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (j == 0)//第0行没有创建过,用CreateRow()
                {
                    //             第几行    第几列
                    sheet.CreateRow(i).CreateCell(j).SetCellType(CellType.STRING);//设置单元格的类型
                    sheet.CreateRow(i).CreateCell(j).SetCellValue(i + "," + j);//设置单元格的内容
                }
                else//当第0行创建了,就需要用GetRow()
                {
                    sheet.GetRow(i).CreateCell(j).SetCellType(CellType.STRING);
                    sheet.GetRow(i).CreateCell(j).SetCellValue(i + "," + j);
                }
            }
        }
        FileStream fs = new FileStream(Server.MapPath("~/test.xls"), FileMode.Create);//实例化文件流
        wordbook.Write(fs);//讲工作表写入到文件流中
        fs.Close();//关闭文件流


//----------------------------下载
        Response.ContentType = "application/ms-excel";//配置输出类型为execl
        Response.AppendHeader("Content-disposition", "attachment;filename=aa.xls");//配置标头
        wordbook.Write(Response .OutputStream);//讲工作表写入到输出流中
    }
}

 

本文出自 “程序猿的家” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1530198

简单的创建一张excel表,布布扣,bubuko.com

简单的创建一张excel表

标签:public   excel   npoi   

原文地址:http://962410314.blog.51cto.com/7563109/1530198

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!