[VB.NET]winform下DataGrid导出excel问题

来源:互联网 发布:如何在淘宝上传照片 编辑:程序博客网 时间:2024/04/30 04:35
VB.NET源码-156个实用实例哦……<script type="text/javascript"><!--google_ad_client = "pub-8333940862668978";/* 728x90, 创建于 08-11-30 */google_ad_slot = "4485230109";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
winform下DataGrid导出excel问题
在http://topic.csdn.net/t/20050919/14/4279392.html找到的代码,在我的程序
中提示 "未处理的“System.StackOverflowException”类型的异常 ",请大家帮忙看看,谢谢。

Private Function GetDataFromDB()
Try
Dim selEmployeeContract As String = "Select * from 预定单 "
Dim dsEmployeeContract As DataSet = New DataSet '' '' ''这里有错 ''
dsEmployeeContract.Clear()
dsEmployeeContract = GetDataFromDB(selEmployeeContract)
If dsEmployeeContract Is Nothing Then
MsgBox( "没有数据,无法导出! ", MsgBoxStyle.OKOnly)
Exit Function
Else
Dim xlsapp As New Excel.Application
xlsapp.Workbooks.Add()
xlsapp.Visible = True
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, dsEmployeeContract.Tables(0).Columns.Count)).Select()
xlsapp.Selection.Merge()
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, 1)).Font.Size = 15
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, dsEmployeeContract.Tables(0).Columns.Count)).Font.Bold = True
xlsapp.Cells(1, 1) = "预定单 "
Dim i As Int16
For i = 1 To dsEmployeeContract.Tables(0).Columns.Count
xlsapp.Cells(2, i) = dsEmployeeContract.Tables(0).Columns(i - 1).ColumnName
Next
Dim rowindex As Integer = 3
Dim colindex As Integer
Dim col As DataColumn
Dim row As DataRow
Dim nxh As Integer = 1
For Each row In dsEmployeeContract.Tables(0).Rows
colindex = 1
For Each col In dsEmployeeContract.Tables(0).Columns
If colindex = 1 Then
xlsapp.Cells(rowindex, colindex) = RTrim(Convert.ToString(row(col.ColumnName)))
Else
xlsapp.Cells(rowindex, colindex) = RTrim(Convert.ToString(row(col.ColumnName)))
End If
colindex += 1
Next
rowindex += 1
nxh += 1
Next
xlsapp.Range(xlsapp.Cells(2, 1), xlsapp.Cells(dsEmployeeContract.Tables(0).Rows.Count + 2, dsEmployeeContract.Tables(0).Columns.Count)).Font.Size = 9
xlsapp.Range(xlsapp.Cells(2, 6), xlsapp.Cells(dsEmployeeContract.Tables(0).Rows.Count + 2, 4)).NumberFormat = "yyyy-MM-dd "
xlsapp.Range(xlsapp.Cells(2, 7), xlsapp.Cells(dsEmployeeContract.Tables(0).Rows.Count + 2, 5)).NumberFormat = "yyyy-MM-dd "
xlsapp.Columns.AutoFit()
End If
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical)
End Try
End Function

Private Sub Button1_Click(...)
GetDataFromDB()
End Sub
__________________________________________________________________________
我已经导入了Microsoft Excel11.0 object library
__________________________________________________________________________
该回复于2008-12-27 11:24:42被管理员或版主删除
__________________________________________________________________________
我这里正好在网上找了一个,可以导到EXCEL的不过有些问题,我还没搞清楚。就是对导出生成的EXCEL是那个版本
Public Sub ExportDataGridViewToExcel(ByVal dataGridview1 As DataGridView, ByVal excelName As String)
Dim saveFileDialog As SaveFileDialog = New SaveFileDialog
saveFileDialog.Filter = "Execl files (*.xls)|*.xls "
saveFileDialog.FilterIndex = 0
saveFileDialog.FileName = excelName
saveFileDialog.InitialDirectory = Application.StartupPath & "/excel "
saveFileDialog.RestoreDirectory = True
saveFileDialog.Title = "生成电子表格 "
If saveFileDialog.ShowDialog = DialogResult.OK Then
Dim myStream As Stream
Try
myStream = saveFileDialog.OpenFile
Dim sw As StreamWriter = New StreamWriter(myStream, System.Text.Encoding.Default)
Dim str As String = " "
Try
Dim i As Integer = 0
While i < dataGridview1.ColumnCount
If i > 0 Then
str += " " & Microsoft.VisualBasic.Chr(9) & " "
End If
str += dataGridview1.Columns(i).HeaderText
i = i + 1
End While
sw.WriteLine(str)
Dim j As Integer = 0
While j < dataGridview1.Rows.Count - 1
Dim tempStr As String = " "
Dim k As Integer = 0
While k < dataGridview1.Columns.Count
If k > 0 Then
tempStr += " " & Microsoft.VisualBasic.Chr(9) & " "
End If
tempStr += dataGridview1.Rows(j).Cells(k).Value.ToString
k = k + 1
End While
sw.WriteLine(tempStr)
j = j + 1
End While
sw.Close()
myStream.Close()
MsgBox( "生成电子表格完成 ", MsgBoxStyle.Information)
Catch e As Exception
MessageBox.Show(e.ToString)
Finally
sw.Close()
myStream.Close()
End Try
Catch ex As Exception
MsgBox( "保存的EXCEL正处于打开使用状态! ", MsgBoxStyle.Information)
End Try
Else
Return
End If
End Sub
__________________________________________________________________________
一个导入到EXCEL的工具,不防一试

SQL导出到EXCEL http://www.onlinedown.net/soft/44040.htm
__________________________________________________________________________
试下这个:
Dim connStr As String = 连接数据库语句
Dim con As New OleDbConnection(connStr)
Dim SqlStr As String = "select * from 表名 "
Dim da As New OleDbDataAdapter(SqlStr, con)
Dim ds As New DataSet
Try
con.Open()
da.Fill(ds)

Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim rowIndex As Integer = 1
Dim colIndex As Integer = 0
xlBook = xlApp.Workbooks.Add(True)
Dim Table As New DataTable()
Table = ds.Tables(0)
Dim Col As DataColumn
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
Dim Row As DataRow
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.ItemArray(colIndex - 1)
Next
Next
xlApp.Visible = True
Catch ex As Exception
MsgBox( "数据库连接失败! " & ex.ToString().Trim(), MsgBoxStyle.Exclamation, "系统提示: ")
Finally
con.Close()
End Try
__________________________________________________________________________
同楼上,也是用FOR导出的~~~
__________________________________________________________________________
发给你了,试试看
__________________________________________________________________________
原创粉丝点击