如何在vb.net中将元素数组写入XLS文件

时间:2011-06-13 14:34:10

标签: .net vb.net

我有一个包含多个元素的数组。现在我需要创建一个xls文件,需要将数组的所有成员写入xls文件,就像每个单元格的一个成员一样。

    Dim input()={"ex1","ex2","ex3","ex4","ex5"}

现在我需要创建examle.xls文件并将上面的input()元素写入example.xls 请提出任何建议

1 个答案:

答案 0 :(得分:0)

CSV文件是否合适?如果使用System.IO命名空间创建一个简单的文本文件就足够了。如果没有,那么您需要使用Excel COM Interop库。

如果你想使用Excel,那么你需要做这样的事情

Private Function ExcelFileCreated(ByVal filename As String, ByVal FileDate As Date) As Boolean

'Create the Excel object declaration' create a excel application 
Dim objExcel As Microsoft.Office.Interop.Excel.Application = Nothing
'create a excel workbooks object
Dim objBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
'create a workbook object
Dim objBook As Microsoft.Office.Interop.Excel.Workbook = Nothing
'create a excel sheets object
Dim objSheets As Microsoft.Office.Interop.Excel.Sheets = Nothing
'create a excel sheet object
Dim objSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
'create a excel range object
Dim objRange As Microsoft.Office.Interop.Excel.Range = Nothing
'Create a new object of the Excel application object
objExcel = New Microsoft.Office.Interop.Excel.Application
objExcel.Visible = False
objExcel.DisplayAlerts = False
'Adding a collection of Workbooks to the Excel object
objBook = CType(objExcel.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
objBooks = objExcel.Workbooks
objSheet = CType(objBooks(1).Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
objSheets = objBook.Worksheets
'Adding multiple worksheets to workbook 
objSheets.Add(Count:=1)
'Summary log file sheet            
'adding first sheet
objBook = objBooks.Item(1)
objSheet = CType(objSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
'Assigning the worksheet name 
objSheet.Name = "Sheet Name"

objExcel.Cells(1, 1).Value = "ex1" 
objExcel.Cells(7, 2).Value = "ex2"
objExcel.Cells(7, 3).value = "ex4"
objExcel.Cells(7, 4).value = "ex4"
objExcel.Cells(7, 5).value = "ex5"

    Marshal.ReleaseComObject(objSheet)
    Marshal.ReleaseComObject(objSheets)

    'Saving the Workbook as a normal workbook format under log location
    Try
        If System.IO.Directory.Exists("c:\Temp") = False Then
            System.IO.Directory.CreateDirectory("c:\Temp")
        End If
        objBook.SaveAs(filename, _
                       Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, _
                       System.Reflection.Missing.Value, _
                       System.Reflection.Missing.Value, _
                       False, _
                       False, _
                       Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, _
                       False, _
                       False, _
                       System.Reflection.Missing.Value, _
                       System.Reflection.Missing.Value, _
                       System.Reflection.Missing.Value)
        objExcel.ActiveWorkbook.Close()
        Marshal.ReleaseComObject(objExcel)
        Return True

    Catch ex As Exception
        Return False
    End Try

End Function