NPOI导出Excel

一般应用程序:

using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace WebUI.HanderAshx.PointsMall.Draw
{
    /// <summary>
    /// ExportExcel 的摘要说明
    /// </summary>
    public class ExportExcel : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var dt = bll.GetDrawReport();

            context.Response.ContentType = "application/x-xls";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Ticks + ".xls");
            HSSFWorkbook hssfWorkbook = OutputSearchResult(dt, filter);
            hssfWorkbook.Write(context.Response.OutputStream);
        }
        public HSSFWorkbook OutputSearchResult(DataTable dt, string filter)
        {

            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            ISheet sheet = hssfWorkbook.CreateSheet("抽奖报表");
            IRow rowHeader = sheet.CreateRow(0);
            rowHeader.CreateCell(0, CellType.STRING).SetCellValue("姓名");
            rowHeader.CreateCell(1, CellType.STRING).SetCellValue("手机号码");
            rowHeader.CreateCell(2, CellType.STRING).SetCellValue("地址");
            rowHeader.CreateCell(3, CellType.STRING).SetCellValue("用户名");
            rowHeader.CreateCell(4, CellType.STRING).SetCellValue("会员等级");
            rowHeader.CreateCell(5, CellType.STRING).SetCellValue("奖品");
            rowHeader.CreateCell(6, CellType.STRING).SetCellValue("是否确认");
            rowHeader.CreateCell(7, CellType.STRING).SetCellValue("抽奖时间");
            rowHeader.CreateCell(8, CellType.STRING).SetCellValue("其它");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                IRow dataRow = sheet.CreateRow(Convert.ToInt32(dr["rowNum"]));
                dataRow.CreateCell(0, CellType.STRING).SetCellValue(dr["Name"].ToString());
                dataRow.CreateCell(1, CellType.STRING).SetCellValue(dr["Mobile"].ToString());
                dataRow.CreateCell(2, CellType.STRING).SetCellValue(dr["ProvinceName"].ToString() + dr["CityName"].ToString() + dr["Address"].ToString());
                dataRow.CreateCell(3, CellType.STRING).SetCellValue(dr["MemberName"].ToString());
                dataRow.CreateCell(4, CellType.STRING).SetCellValue(dr["CurrentLevel"].ToString());
                dataRow.CreateCell(5, CellType.STRING).SetCellValue(dr["PrizeName"].ToString());
                dataRow.CreateCell(6, CellType.STRING).SetCellValue(dr["Status"].ToString());
                dataRow.CreateCell(7, CellType.STRING).SetCellValue(dr["CreateDate"].ToString());
                dataRow.CreateCell(8, CellType.STRING).SetCellValue(dr["OpMsg"].ToString());
            }
            return hssfWorkbook;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

页面:

window.open("/Draw/ExportExcel.ashx", "_blank");
posted @ 2016-06-24 14:49  chenjingchun  阅读(323)  评论(0编辑  收藏  举报