Imports System.IO Imports OfficeOpenXml Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click 'テンプレートファイルの場所 Dim strFileTemplate As String = Server.MapPath("templateFile.xlsx") Dim strFileName As String = "newFile.xlsx" Dim strFilePath As String = Server.MapPath(strFileName) Dim templateFile As New FileInfo(strFileTemplate) Dim newFile As New FileInfo(strFilePath) Dim strData() As String = _ {"鉛筆", "消しゴム", "ボールペン", "サインペン", "修正ペン"} If newFile.Exists Then newFile.Delete() 'ファイルが存在する場合は削除します。 newFile = New FileInfo(strFilePath) End If 'ExcelPackage クラスを利用してデータセットのデータをシートにマッピング Using xlPackage As ExcelPackage = _ New ExcelPackage(newFile, templateFile)
Dim worksheet As ExcelWorksheet = _ xlPackage.Workbook.Worksheets("Sheet1") Dim ColumnIndex As Integer = 0 Dim rowIndex As Integer = 0 For i = 1 To strData.Length worksheet.Cells(i + 1, 1).Value = strData(i - 1) Next i xlPackage.Save() End Using Response.Redirect(strFileName) End Sub |