我用過的,引用孟子大哥的例子,可以
Private Sub WritetoExcel()
Dim FileName As String
Dim FileExtName As String = ".xls"
Dim strMonth As String, strDay As String, k As Integer
'Dim xlsheet
strMonth = CStr(Month(Now()))
strDay = CStr(Day(Now()))
For k = 1 To 2 - Len(strMonth)
strMonth = "0" & strMonth
Next
For k = 1 To 2 - Len(strDay)
strDay = "0" & strDay
Next
FileName = "Ensure"
FileName = FileName & Year(Now()) & strMonth & strDay & FileExtName
'Response.Write(FileName)
'Response.End()
Dim xlsheet As New SpreadsheetClass
'xlsheet = CreateObject("OWC11.Spreadsheet")
Dim i As Integer = 0
Dim j As Integer = 0
'Response.End()
' 輸出標題
Dim oItem As DataGridColumn
For Each oItem In DataGrid1.Columns
xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText
'xlsheet.ActiveSheet.Range(xlsheet.ActiveSheet.Cells(1, 1), xlsheet.ActiveSheet.Cells(1, i + 1)).Font.Bold = True
'設置格式
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "red"
i = i + 1
Next
Dim numbercols As Integer = DataGrid1.Items.Item(0).Cells.Count
' 輸出欄位內容
For j = 0 To DataGrid1.Items.Count - 1
For i = 0 To numbercols - 1
xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue"
'xlsheet.Range("A2:B14").WrapText = True
'xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns()
xlsheet.ActiveSheet.Cells(j + 2, i + 1) = DataGrid1.Items.Item(j).Cells(i).Text.Replace(" ", " ")
Next
Next
Try
'xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + Me.xlfile.Text, OWC.SheetExportActionEnum.ssExportActionNone)
'xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + FileName, OWC.SheetExportActionEnum.ssExportActionNone)
'xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + FileName, OWC.SheetExportActionEnum.ssExportActionNone)
'二、定义一个输入流
Dim temp As String = ""
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
'System.IO.StringWriter(oStringWriter = New System.IO.StringWriter)
'System.Web.UI.HtmlTextWriter(oHtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter))