Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tempexcel As New Excel.Application
Dim tempchart As New Excel.Chart
tempexcel.Visible = True
Dim wkbNew As Excel.Workbook
CreateNewWorkbook(tempexcel, wkbNew, "从Datagrid导入到Excel", 1)
End Sub
Function CreateNewWorkbook(ByVal excelstr As Excel.Application, ByVal wkbnew As Excel.Workbook, Optional ByVal strBookName As String = "", Optional ByVal intNumSheets As Integer = 3) As Workbook
Dim intOrigNumSheets As Integer
'On Error GoTo CreateNew_Err
intOrigNumSheets = excelstr.Application.SheetsInNewWorkbook
If intOrigNumSheets <> intNumSheets Then
excelstr.Application.SheetsInNewWorkbook = intNumSheets
End If
wkbnew = excelstr.Workbooks.Add
Dim tempsheet As Excel.Worksheet
tempsheet = wkbnew.Worksheets(1)
tempsheet.Name = "employee"
Dim rng1 As Excel.Range
Dim rng2 As Excel.Range
rng1 = tempsheet.Rows(1)
rng1.Font.FontStyle = "楷体_GB2312"
rng1.Font.Color = RGB(100, 200, 230)
Dim i As Integer
Dim j As Integer
For j = 0 To dt.Rows.Count - 1''''''''dt是datagrid的datasource
For i = 0 To dt.Columns.Count - 1
'tempsheet.Cells(j + 2, i + 1) = DataArray(j, i)
Try
tempsheet.Cells(j + 2, i + 1) = dt.Rows.Item(j).Item(i)
Catch ex As Exception
tempsheet.Cells(j + 2, i + 1) = dt.Rows.Item(j).Item(i)
End Try
Next
Next
If Len(strBookName) = 0 Then strBookName = excelstr.Application.GetSaveAsFilename
CreateNewWorkbook = wkbnew
excelstr.Application.SheetsInNewWorkbook = intOrigNumSheets
End Function