你是怎么调用的,把代码贴出来看看
我用下面的代码测试没问题:
Dim dt As New Data.DataTable
dt.Columns.Add("a")
dt.Columns.Add("b")
dt.Columns.Add("c")
Dim r As DataRow
r = dt.NewRow()
r.Item(0) = "1"
r.Item(1) = "2"
r.Item(2) = "3"
dt.Rows.Add(r)
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports OWC
Public Class DataGridToExcel
Inherits System.Web.UI.Page
Protected WithEvents xlfile As System.Web.UI.WebControls.TextBox
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents ExportDataGrid2Excel As System.Web.UI.WebControls.Button
Protected WithEvents ExportDataBase2Excel As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected mengxianhui As New HtmlGenericControl()
Private cnn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="_
+ Server.MapPath("Test.mdb"))
Private sql As OleDbCommand = New OleDbCommand("SELECT TOP 50 Title,Author FROM Document", cnn)
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
Label1.Text = "请输入要保存得文件名字:"
ExportDataGrid2Excel.Text = "由DataGrid生成Excel文件"
ExportDataBase2Excel.Text = "数据库直接生成Excel文件"
DataGrid1.Columns(0).HeaderStyle.HorizontalAlign = HorizontalAlign.Center
DataGrid1.Columns(0).HeaderText = "文章名称"
DataGrid1.Columns(1).HeaderText = "作者"
DataGrid1.Columns(0).HeaderStyle.Font.Bold = True
DataGrid1.Style.Add("font-size", "9pt")
mengxianhui.InnerText = "【孟宪会之精彩世界】- 将DataGrid输出到Excel文件"
Me.BindDataGrid()
End Sub
Private Sub BindDataGrid()
cnn.Open()
Dim reader As OleDbDataReader = sql.ExecuteReader()
Me.DataGrid1.DataSource = reader
Me.DataGrid1.DataBind()
reader.Close()
cnn.Close()
End Sub
Private Sub WriteDataGrid2Excel()
Dim xlsheet As New SpreadsheetClass()
cnn.Open()
Dim reader As OleDbDataReader = Me.sql.ExecuteReader()
Dim numbercols As Integer = reader.FieldCount
Dim row As Integer = 2
Dim i As Integer = 0
' 输出标题
For i = 0 To numbercols - 1
xlsheet.ActiveSheet.Cells(1, i + 1) = reader.GetName(i).ToString()
Next
' 输出字段内容
While (reader.Read())
For i = 0 To numbercols - 1
xlsheet.ActiveSheet.Cells(row, i + 1) = reader.GetValue(i).ToString()
Next
row = row + 1
End While
reader.Close()
cnn.Close()
Try
xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + Me.xlfile.Text,_
OWC.SheetExportActionEnum.ssExportActionNone)
Catch e As System.Runtime.InteropServices.COMException
Response.Write("错误:" + e.Message)
End Try
End Sub
Private Sub WriteDataGrid2Excel2()
Dim xlsheet As New SpreadsheetClass()
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)
Catch e As System.Runtime.InteropServices.COMException
Response.Write("错误:" + e.Message)
End Try
End Sub
Private Sub ExportDataGrid2Excel_Click(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles ExportDataGrid2Excel.Click
If (Me.xlfile.Text.Trim() <> "") Then
Me.WriteDataGrid2Excel2()
End If
End Sub
Private Sub ExportDataBase2Excel_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ExportDataBase2Excel.Click
If (Me.xlfile.Text.Trim() <> "") Then
Me.WriteDataGrid2Excel()
End If
End Sub
Public Function fBindData(ByVal ldataset1 As DataSet)
'Dim a As DataGrid
'a = New DataGrid
Me.DataGrid1.Items(0).Cells(0).FindControl("")
DataGrid1.DataSource = ldataset1
DataGrid1.DataBind()
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "" ' 从Content-Type header中去除charset设置
' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid1.RenderControl(hw) ' 获取control的HTML
Response.Write(tw.ToString()) ' 把HTML写回浏览器
Response.End()
End Function
Public Class Form1
Inherits System.Windows.Forms.Form
Private connstr, selectcmd As String
Private conn As OleDbConnection
Private cmd As OleDbCommand
Private myreader As OleDbDataReader
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim excelApp As Object
excelApp = CreateObject("Excel.Application")
Dim excelBook As Object
excelBook = excelApp.Workbooks.Add
Dim excelWorksheet As Object
excelWorksheet = excelBook.Worksheets(1)
excelApp.Visible = False
SaveFileDialog1.Title = "将结果保存为"
SaveFileDialog1.Filter = "excel *.xls|*.xls"
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
If SaveFileDialog1.FileName <> "" Then
Dim n As Integer
Dim i As Integer = 2
With excelWorksheet
Do While myreader.Read()
For n = 0 To myreader.FieldCount - 1
.Range(Chr(65 + n) & "1").Value = myreader.GetName(n)
.Range(Chr(65 + n) & "1").ColumnWidth = 15
.Range(Chr(65 + n) & i.ToString).Value = myreader.Item(n)
Next n
i += 1
Loop
End With
excelBook.saveas(SaveFileDialog1.FileName)
End If
End If
excelApp.quit()
excelApp = Nothing
MsgBox("恭喜")
conn.close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connstr = "provider=microsoft.jet.oledb.4.0;data source=d:\dl\dl.mdb"
selectcmd = "select * from users"
conn = New OleDbConnection(connstr)
conn.Open()
cmd = New OleDbCommand(selectcmd, conn)
myreader = cmd.ExecuteReader
End Sub
' Add a Chart for the selected data.
For Each c As DataColumn In dt.Columns
sData &= c.ColumnName & vbTab
Next
sData &= vbCr
For Each r As DataRow In dt.Rows
For Each c As DataColumn In dt.Columns
sData &= r(c).ToString & vbTab
Next
sData &= vbCr
Next
System.Windows.Forms.Clipboard.SetDataObject(sData)
oWS.Range("A1").Select()
oWS.Paste()
Catch ex As Exception
If oExcel Is Nothing Then
MsgBox("创建Excel实例失败,可能是没有安装Excel程序。")
Else
MsgBox(ex.Message)
oBook.Close(False)
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End If
End Try
End Sub