读书人

winform下DataGrid导出excel有关问题

发布时间: 2012-01-24 23:11:54 作者: rapoo

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

[解决办法]
发给你了,试试看

读书人网 >VB Dotnet

热点推荐