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

NPOI导出Excel——精简版

时间:2014-12-04 19:32:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   os   sp   for   on   

欢迎大家来吐口水、、

今天查了一天NPOI的资料、发现网上的都太高大上了、自己总结了一版

不多说、直接上代码

bubuko.com,布布扣
public void ExportToExcelTwo(System.Data.DataTable dt, string colTitle, string filePath)
        {
            HSSFWorkbook workBook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workBook.CreateSheet("sheet1");
            HSSFRow row1 = (HSSFRow)sheet.CreateRow(0);
            string[] titles = colTitle.Split(,);
            for (int i = 0; i < titles.Length; i++)
            {
                HSSFCell cell = (HSSFCell)row1.CreateCell(i, CellType.STRING);
                cell.SetCellValue(titles[i]);
            }
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                HSSFRow rows = (HSSFRow)sheet.CreateRow(j + 1);
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    HSSFCell cells = (HSSFCell)rows.CreateCell(k);
                    cells.SetCellValue(dt.Rows[j][k].ToString());
                }
            }
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                workBook.Write(fs);
            }
            GC.Collect();
        }
View Code

 

NPOI导出Excel——精简版

标签:style   blog   http   ar   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/gldblogs/p/4143416.html

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