您的位置:首页 > 编程语言 > ASP

ASP.Net使用NPOI导出Excel

2014-01-08 15:39 645 查看
ASP.Net使用NPOI导出Excel            采用一般处理程序进行处理

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using NPOI.SS.UserModel;

using NPOI.HSSF.UserModel;

using NPOI.HSSF.Util;

using System.Text;

namespace MySEO.ashx

{

    /// <summary>

    /// ExportExcel 的摘要说明

    /// </summary>

    public class ExportExcel : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            WriteExcel(context.Response);

        }

        private void WriteExcel(HttpResponse response)

        {

            response.Clear();

            response.Buffer = true;     

            response.ContentType = "ms-excel";

            //attachment;让浏览器弹出下载对话框保存返回报文

            //filename=是默认文件名,如果文件名中有中文等需要使用UrlEncode编码

            string encodeFileName = HttpUtility.UrlEncode("过滤词.xls");

            response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");   

            response.AddHeader("Content-Disposition",

                string.Format("attachment;filename=\"{0}\"", encodeFileName));

            //1.创建一个新的Workbook

            IWorkbook wk = new HSSFWorkbook();

            //2.创建一个工作表Sheet

            ISheet sheet = wk.CreateSheet("我的工作表");

   
//设置表格格式

            ICellStyle style1 = wk.CreateCellStyle();//样式

            IFont font1 = wk.CreateFont();//字体

            //font1.FontHeightInPoints = 16;//设置字体大小

            font1.Boldweight = (short)FontBoldWeight.BOLD;

            style1.FillForegroundColor = HSSFColor.GREEN.index;// 设置背景色

            style1.FillPattern = FillPatternType.SOLID_FOREGROUND;

            style1.SetFont(font1);//样式里的字体设置具体的字体样式

            style1.Alignment = HorizontalAlignment.CENTER;//文字水平对齐方式

            style1.VerticalAlignment = VerticalAlignment.CENTER;//文字垂直对齐方式

            int _max = 300;

            int maxLength = 0;

            int curLength = 0;

            int colounwidth = 0;

            string value = "";

            IRow rowHeader = sheet.CreateRow(0);

            //每行生成20个单元格

            for (int k = 0; k < 20; k++)

            {

                value = "表头" + k.ToString();

                curLength = Encoding.Default.GetByteCount(value);

                maxLength = (maxLength < curLength ? curLength : maxLength);

                colounwidth = 256 * maxLength;

                sheet.SetColumnWidth(k, colounwidth);    
//控制列宽

                ICell cell = rowHeader.CreateCell(k);

                cell.CellStyle = style1;        
//应用表头格式

                cell.SetCellValue(value);

            }

            maxLength = 0;

            curLength = 0;

            colounwidth = 0;

            //循环创建30000行

            for (int i = 1; i < _max; i++)

            {

                IRow row = sheet.CreateRow(i);

                //每行生成20个单元格

                for (int j = 0; j < 20; j++)

                {

                    if (j % 2 == 0)

                    {

                        value = "单元格" + j.ToString();

                    }

                    else

                    {

                        value = "我是加长的单元格" + j.ToString();

                    }

                    curLength = Encoding.Default.GetByteCount(value);

                    maxLength = (maxLength < curLength ? curLength : maxLength);

                    colounwidth = 256 * maxLength;

                    sheet.SetColumnWidth(j, colounwidth);

                    row.CreateCell(j).SetCellValue(value);

                }

            }

            wk.Write(response.OutputStream);     //使用输出流方式输出

            response.End();

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: