将DataGrid中的数据导出到Excel表格中
在线求助:
我将datagrid数据导出到excel中,出现 SpreadsheetClass没有定义的错误,我已经将ms web office9.0引用上;
要把DataGrid导出为Excel文件,需引用哪个程序集???
问题点数:20、回复次数:3Top
1 楼flygoldfish(长江支流)回复于 2006-05-04 12:59:21 得分 0
http://blog.csdn.net/flygoldfish/archive/2005/11/07/524305.aspxTop
2 楼boblaile(爱在13月32)回复于 2006-05-04 21:49:36 得分 0
添加引用com里的
有excel的
Dim xlSheet As Excel.Worksheet
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlDBtable As Excel.DataTable
Try
xlBook = xlApp.Workbooks.Add
xlSheet = xlBook.Worksheets("sheet1")
' xlSheet.Range("d1:j1").HorizontalAlignment()
'Dim i, j As Integer
Dim Table As New DataTable
Table = CType(datagrid1.DataSource, DataTable) '将datagrid中的数据填充到DataTable中
Dim col As DataColumn
Dim colIndex As Integer
For Each col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = col.ColumnName '获取列名
Next
Dim row As DataRow
Dim rowIndex As Integer
rowIndex = 1 '从第二行开始填充数据
Dim rowint As Integer
' For rowint = 0 To Table.Rows.Count
For Each row In Table.Rows '获取数据
rowIndex = rowIndex + 1
colIndex = 0
For Each col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = row(col.ColumnName)
Next
Next
'while
' If xlApp.CanRecordSounds Then
' Dim xlp As New Excel.Application
Dim strflname As String
Savedl.Title = "保持为"
Savedl.Filter = "xls工作薄|*.xls"
If Savedl.ShowDialog = Windows.Forms.DialogResult.OK Then
If Savedl.FileName <> "" Then
strflname = Savedl.FileName()
Else
Exit Sub
End If
Else
Exit Sub
End If
' Dim dt As New DataTable
' dt = CType(datagrid1.DataSource, DataTable)
' xlp.Cells(1, 1) = dt.Rows.Item(1)
' xlp.Cells() = Me.datagrid1.DataSource
xlBook.SaveAs(strflname, xlApp.ActiveWorkbook.FileFormat, "", "", xlApp.ActiveWorkbook.ReadOnlyRecommended, xlApp.ActiveWorkbook.CreateBackup, Excel.XlSaveAsAccessMode.xlShared.xlShared, xlApp.ActiveWorkbook.ConflictResolution, False, "", "")
Catch ex As Exception
MsgBox(ex.Message)
Finally
xlBook.Close()
xlApp.Quit()
xlApp = Nothing
End Try
Top
3 楼jikemjin()回复于 2006-05-05 13:17:54 得分 0
问题已经解决,谢谢了!Top




