How to dynamically generate multiple worksheets in npoi

由於 BOM 表的單階物料,就要開一個新工作表 ,存放該階層所有的零件。
在展開時,利用遞迴呼叫,重覆的將零件的尾階物料清單,存放到新的工作表


下面的Demo 
動態生成多個工作表
再填入指定儲存格


'-------------------------------------------------------------------------
Imports System.IO
Imports NPOI.HSSF.UserModel

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim MS As MemoryStream = New MemoryStream()
        Dim WB As HSSFWorkbook = New HSSFWorkbook()

        For i As Int16 = 1 To 5
            '動態開啟新的工作表
            Dim mySheet As HSSFSheet = WB.CreateSheet("Sheet" & i)
            Dim myRow As HSSFRow = mySheet.CreateRow(i)
            For j As Int16 = 0 To 5
                '將資料填入指定的儲存格
                Dim myCell As HSSFCell = myRow.CreateCell(j)
                myCell.SetCellValue(i * j)
            Next
        Next

        WB.Write(MS)
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=myName.xls"))
        Response.BinaryWrite(MS.ToArray())

        WB = Nothing
        MS.Close()
        MS.Dispose()

    End Sub
End Class

沒有留言:

張貼留言