【VB】使用NPOI元件來匯出Excel

NPOI元件是一個簡單又好上手的匯出Excel元件,本人認為功能還蠻強大的,至少可以解決公司上的需求。
使用時必須先引用該元件

引用:
Imports System.IO
Imports NPOI.HSSF.UserModel
Imports NPOI.HPSF
Imports NPOI.POIFS.FileSystem

程式碼(參考) : 本人做法是撈取DB內容,然後匯出Excel。

        Dim file_name As String = Format(Now, "yyyyMMdd") & "採購單.xls"

        Dim workbook As HSSFWorkbook = New HSSFWorkbook()
        Dim ms As MemoryStream = New MemoryStream()  '==需要 System.IO命名空間

        '== 新增試算表 Sheet名稱。 
        Dim u_sheet As HSSFSheet = workbook.CreateSheet("採購單")

        '定義標題
        Dim row_header As HSSFRow = u_sheet.CreateRow(0)

        'HSSFRow  HSSFCell 應用
          '設定欄寬度
        u_sheet.SetColumnWidth(1, 3000)
        u_sheet.SetColumnWidth(2, 10000)
        u_sheet.SetColumnWidth(4, 3000)
        u_sheet.SetColumnWidth(5, 4500)
        u_sheet.SetColumnWidth(6, 3000)

        '== 標題 設定。 
        row_header.CreateCell(0).SetCellValue("序號")
        row_header.CreateCell(1).SetCellValue("供應商代碼")
        row_header.CreateCell(2).SetCellValue("供應商名稱")
        row_header.CreateCell(3).SetCellValue("單據號碼")
        row_header.CreateCell(4).SetCellValue("過帳日期")
        row_header.CreateCell(5).SetCellValue("所有人")
        row_header.CreateCell(6).SetCellValue("總計")

        '抓DB
        Dim conn As String = Get_Val(Me.DropDownList1.SelectedValue)
        ads.ConnectionString = conn
        ads.SelectCommand = Me.SqlDataSource1.SelectCommand
        Dim dv As Data.DataView = ads.Select(New DataSourceSelectArguments)
        dv = ads.Select(New DataSourceSelectArguments)

        For i As Integer = 0 To dv.Count - 1   '讀DB內資料放到excel欄位裡
            Dim row_body As HSSFRow = u_sheet.CreateRow(i + 1)
            row_body.CreateCell(0).SetCellValue(i + 1)
            row_body.CreateCell(1).SetCellValue(dv.Item(i).Row(0).ToString())
            row_body.CreateCell(2).SetCellValue(dv.Item(i).Row(1).ToString())
            row_body.CreateCell(3).SetCellValue(Val(dv.Item(i).Row(2).ToString()))
            row_body.CreateCell(4).SetCellValue(dv.Item(i).Row(3).ToString())
            row_body.CreateCell(5).SetCellValue(dv.Item(i).Row(4).ToString())
            row_body.CreateCell(6).SetCellValue(dv.Item(i).Row(5).ToString())
        Next

        workbook.Write(ms)
        '== Excel檔名,請寫在最後面 filename的地方 
        Response.AddHeader("Content-Disposition", String.Format("attachment;filename=" + Server.UrlEncode(file_name)))
        Response.ContentType = "application/vnd.ms-excel"
        Response.BinaryWrite(ms.ToArray())

        '== 釋放資源 
        workbook = Nothing   '== C#為 null 
        ms.Close()
        ms.Dispose()

Excel內容(參考) :



PS: 參考樣式 底為(黃色)

        ' 建立儲存格樣式。
        Dim style1 As HSSFCellStyle = workbook.CreateCellStyle()
        style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.YELLOW.index
        style1.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND

        Dim cell As HSSFCell = u_sheet.CreateRow(0).CreateCell(0)
        cell.CellStyle = style1
        cell.SetCellValue("序號")

       可參考本Blog ---【VB】使用NPOI元件來匯出Excel--DataTableToExcel

參考資源 :
http://msdn.microsoft.com/zh-tw/ee818993.aspx
NPOI教學(使用C#)

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄