站內文章

2014年10月17日 星期五

gridview export excel

參考書籍 微軟MVP的ASP.NET 4.5專題實務I p11-7
書上光碟有附很多範例,如果真要買這方面的書,蠻推薦這本的@@



這是使用NPOI1.2.4版來匯出EXCEL(其他版本可能不適用)
所以一開始得先抓一下NPOI的Library
點我下載 NPOI 1.2.4 Library

抓完後再專案那右鍵->加入參考


//== NPOI的宣告 ==================
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel; 
//===============================

        //== 本範例的資料來源:http://msdn.microsoft.com/zh-tw/ee818993.aspx 
        //== http://tonyqus.sinaapp.com/archives/73  (v.1.2.4版)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //=======================================(v.1.2.4版)
        //== 新增試算表 Sheet名稱。使用 NPOI.SS.UserModel命名空間。
        ISheet u_sheet = (ISheet)workbook.CreateSheet("My Sheet_124");
        //=======================================
 
        //===============================================(主要功能start)
        //== 利用迴圈,把gridview的資料 一個一個 寫入 Excel各個儲存格裡面。
        for (int a = 0 ; a < GridView1.Rows.Count ; a++) // gridview的row
        {
            //**** 先建好一列(Row),才能去作格子(Cell)
            IRow u_Row = u_sheet.CreateRow(a+1);
            for (int i = 0; i < GridView1.Rows[a].Cells.Count; i++) //gridview的column
            {
               //== .CreateCell() 可設定為同一列(Row)的 [第幾個格子]
               u_Row.CreateCell(i).SetCellValue(GridView1.Rows[a].Cells[i].Text);
            }
        }
        //===============================================(主要功能end)
        MemoryStream ms = new MemoryStream();  //==需要 System.IO命名空間
        workbook.Write(ms);
        //== Excel檔名,請寫在最後面 filename的地方
        Response.AddHeader("Content-Disposition", "attachment; filename=申請書.xls");
        Response.BinaryWrite(ms.ToArray());

        //== 釋放資源
        workbook = null;   //== VB為 Nothing
        ms.Close();
        ms.Dispose();

        //== 如果要寫在 Web Server的硬碟裡面,請參閱上一個範例 NPOI_01_new_v124.aspx。
    }



// 最後面這邊補上單一欄位新增的方式

        //**** CreateRow()方法,只有這一列的「第一格子」可以這樣用。(v.1.2.4版 的新變化)
        // 每一列的第0個值
        u_sheet.CreateRow(0).CreateCell(0).SetCellValue("0000"); // 第0列 第0欄
        u_sheet.CreateRow(1).CreateCell(0).SetCellValue("1111"); // 第1列 第0欄
        //== 以下在  (v.1.2.1版,正常) 在 目前這個版本會異常
        //u_sheet.CreateRow(6).CreateCell(1).SetCellValue("6666");  // 第6列 第1欄
        //u_sheet.CreateRow(6).CreateCell(2).SetCellValue("7777");

        //== 如果要「讀取」某個格子,請用 .GetCell(index)方法

        //**********************************************************(start)
        //**** v.1.2.4版在此有很大的改變!!!請看 http://tonyqus.sinaapp.com/archives/73  
        IRow u_Row= u_sheet.CreateRow(6);    //== 先用 IRow介面,建立全新的一列。 第6列
        //== .CreateCell() 可設定為同一列(Row)的 [第幾個格子]
        u_Row.CreateCell(1).SetCellValue("6666");   // 第6列 第1欄 新增 6666
        u_Row.CreateCell(2).SetCellValue("7777");   // 第6列 第2欄 新增 7777




這邊再分享一個匯出時可以去除girdview裡面的 "& nbsp;" 以及新增第一行欄位名稱

    protected void Button1_Click(object sender, EventArgs e)
    {
        // 去除girdview裡面的 " "
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
            {
                if (GridView1.Rows[i].Cells[j].Text == " ")
                {
                    GridView1.Rows[i].Cells[j].Text = "";
                }
            }
        }
        //== 本範例的資料來源:http://msdn.microsoft.com/zh-tw/ee818993.aspx 
        //== http://tonyqus.sinaapp.com/archives/73  (v.1.2.4版)

        HSSFWorkbook workbook = new HSSFWorkbook();

        //=======================================(v.1.2.4版)
        //== 新增試算表 Sheet名稱。使用 NPOI.SS.UserModel命名空間。
        ISheet u_sheet = (ISheet)workbook.CreateSheet("My Sheet_124");
        //=======================================
   
        //===============================================(start)
        //== 利用迴圈,把資料寫入 Excel各個儲存格裡面。
        for (int a = -1 ; a < GridView1.Rows.Count ; a++) // +1個 欄位名稱的 row
        {
            //**** 先建好一列(Row),才能去作格子(Cell)
            IRow u_Row = u_sheet.CreateRow(a+1);
            if (a == -1) //第 0 列要顯示欄位名稱
            {
                // 第0列新增欄位名稱
                u_Row.CreateCell(0).SetCellValue("項目號碼"); // 第0欄
                u_Row.CreateCell(1).SetCellValue("檢修品名稱"); // 第1欄
                u_Row.CreateCell(2).SetCellValue("規格");
                u_Row.CreateCell(3).SetCellValue("數量");
                u_Row.CreateCell(4).SetCellValue("單位");
                u_Row.CreateCell(5).SetCellValue("試驗項目");
                u_Row.CreateCell(6).SetCellValue("良");
                u_Row.CreateCell(7).SetCellValue("不良");
                u_Row.CreateCell(8).SetCellValue("備註");
            }
            else
            {
                for (int i = 0; i < GridView1.Rows[a].Cells.Count; i++)
                {
                    //== .CreateCell() 可設定為同一列(Row)的 [第幾個格子]
                    u_Row.CreateCell(i).SetCellValue(GridView1.Rows[a].Cells[i].Text);
                }
            }
        }
        //===============================================(end)

        MemoryStream ms = new MemoryStream();  //==需要 System.IO命名空間
        workbook.Write(ms);
        //== Excel檔名,請寫在最後面 filename的地方
        Response.AddHeader("Content-Disposition", "attachment; filename=材料課委試報告書.xls");
        Response.BinaryWrite(ms.ToArray());

        //== 釋放資源
        workbook = null;   //== VB為 Nothing
        ms.Close();
        ms.Dispose();
        //== 如果要寫在 Web Server的硬碟裡面,請參閱上一個範例 NPOI_01_new_v124.aspx。
    }

沒有留言:

張貼留言