ASP.Net使用NPOI导出Excel

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;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值