首页 > 其他 > 详细

npoi实现数据导出Excel

时间:2014-05-26 21:26:54      阅读:384      评论:0      收藏:0      [点我收藏+]
  • npoi
    .NET第三方的Office功能组件。
  • 引用命名空间
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
  • 功能代码
    /// <summary>
    /// 操作EXCEL导出数据报表的类
    /// </summary>
    public class DataToExcel
    {
      
        /// <summary>
/// 以HTTP的形式创建Excel文件
        /// </summary>
        /// <param name="ds"></param>
        public static void CreateExcel(DataSet ds)
        {
            HSSFWorkbook hssfworkbook;
            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
            HttpContext.Current.Response.Clear();
 
            InitializeWorkbook(out hssfworkbook);
            GenerateData(ref hssfworkbook, ds.Tables[0]);
 
            HttpContext.Current.Response.BinaryWrite(WriteToStream(hssfworkbook).GetBuffer());
            HttpContext.Current.Response.End();
        }
 
        /// <summary>
        /// 写入字符流
        /// </summary>
        /// <param name="hssfworkbook"></param>
        /// <returns></returns>
        private static MemoryStream WriteToStream(HSSFWorkbook hssfworkbook)
        {
            MemoryStream file = new MemoryStream();
            hssfworkbook.Write(file);
            return file;
        }
 
        /// <summary>
        /// 数据转换
        /// </summary>
        /// <param name="hssfworkbook"></param>
        private static void GenerateData(ref HSSFWorkbook hssfworkbook, DataTable dt)
        {
            ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
 
            IRow row0 = sheet1.CreateRow(0);
            for (int z = 0; z < dt.Columns.Count; z++)
            {
                row0.CreateCell(z).SetCellValue(dt.Columns[z].ColumnName.ToString());
            }
 
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row = sheet1.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
                }
            }
        }
 
        /// <summary>
        /// 初始化工作薄
        /// </summary>
        /// <param name="hssfworkbook"></param>
        private static void InitializeWorkbook(out HSSFWorkbook hssfworkbook)
        {
            hssfworkbook = new HSSFWorkbook();
 
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "";
            hssfworkbook.DocumentSummaryInformation = dsi;
 
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "";
            hssfworkbook.SummaryInformation = si;
        }
 
    }

npoi实现数据导出Excel,布布扣,bubuko.com

npoi实现数据导出Excel

原文:http://www.cnblogs.com/jameslif/p/3746030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!