[VB.NET]如何将DataGridView中的数据存入Excel文件中?

来源:互联网 发布:linux 查看网络端口 编辑:程序博客网 时间:2024/05/16 17:44
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>
如何将DataGridView中的数据存入Excel文件中?
如何将DataGridView中的数据存入Excel文件中?
__________________________________________________________________________
要的是在Form中的实现方法!!
先谢谢了
__________________________________________________________________________
等的急死了,先回去吃饭,回家晚上等着!
__________________________________________________________________________
顶一下
__________________________________________________________________________
怎么联系你
__________________________________________________________________________
Public Sub ExportDataGridViewToExcel(ByVal dataGridview1 As DataGridView)
Dim saveFileDialog As SaveFileDialog = New SaveFileDialog
saveFileDialog.Filter = Execl files (*.xls)|*.xls
saveFileDialog.FilterIndex = 0
saveFileDialog.RestoreDirectory = True
saveFileDialog.CreatePrompt = True
saveFileDialog.Title = 導出Excel文件到
saveFileDialog.ShowDialog()
Dim myStream As Stream
myStream = saveFileDialog.OpenFile
Dim sw As StreamWriter = New StreamWriter(myStream, System.Text.Encoding.Unicode)
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
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()
Catch e As Exception
MessageBox.Show(e.ToString)
Finally
sw.Close()
myStream.Close()
End Try
__________________________________________________________________________
我先试试!!
有问题再请教
__________________________________________________________________________
你好,谢谢你
我是一个菜鸟,使用你上面的代码,能导出excel,但是导出EXCEL之后,有一段错误,
system.NullreferenceException未将对象引用设置到对象实例 这个不知道什么意思!
怎么解决?
__________________________________________________________________________
不好意思,我也是个菜鸟,我用上面那个可以的,没有错误。。。。。。。你可以网上找一下其他的,有很多的,
__________________________________________________________________________
http://topic.csdn.net/t/20040113/08/2658753.html 你可以看看这
__________________________________________________________________________
Dim p As Progress

Private Sub Export_Progress(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Export.ProgressEventArgs)
If e.Phase = DevExpress.XtraGrid.Export.ExportPhase.Link Then
p.ProgressBarControl1.Position = e.Position
p.Update()
End If
End Sub

Dim save As New SaveFileDialog
Dim filename As String

save.Title = 导出Excel
save.Filter = Excel|*.xls
save.FileName = EmployerInfo
If save.ShowDialog() = DialogResult.OK Then
filename = save.FileName
Dim cursor As Cursor = cursor.Current
cursor.Current = Cursors.WaitCursor
Dim provider As IExportProvider = New ExportXlsProvider(filename)
Dim link As DevExpress.XtraGrid.Export.BaseExportLink = Me.GridView1.CreateExportLink(provider)
CType(link, DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = False

p = New Progress
p.Show()
动态加载事件
AddHandler link.Progress, New DevExpress.XtraGrid.Export.ProgressEventHandler(AddressOf Export_Progress)
link.ExportTo(True)
p.Close()
p = Nothing
provider.Dispose()
删除事件
RemoveHandler link.Progress, New DevExpress.XtraGrid.Export.ProgressEventHandler(AddressOf Export_Progress)

Cursor.Current = Cursor

Dim ofd As New OpenFileDialog
If MessageBox.Show( 您要打开文件吗? , 提示 , MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Dim process As New Process
process.StartInfo.FileName = filename
process.Start(filename)
End If

End If
__________________________________________________________________________
Option Strict Off
Imports System
Imports System.Reflection For Missing.Value and BindingFlags
Imports System.Runtime.InteropServices For COMException
Imports Microsoft.Office.Interop.Excel
Module ModCommon
Public Sub GridToExcel(ByVal DTview As DataGridView)
Dim App As Application
Dim Workbooks As Workbooks
Dim workbook As _Workbook
Dim sheets As Sheets
Dim worksheet As _Worksheet
Dim I As Integer, J As Integer
Try
App = New Application() Microsoft.Office.Interop.Excel.Application
If App Is Nothing Then
MsgBox( ERROR: EXCEL couldn t be started! , MsgBoxStyle.Information)
Environment.ExitCode = 0
Exit Sub
End If
App.Visible = True
Workbooks = App.Workbooks
workbook = Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
sheets = workbook.Worksheets
worksheet = sheets.Item(1)
If worksheet Is Nothing Then
MsgBox( ERROR: worksheet == null , MsgBoxStyle.Information)
End If
For I = 0 To DTview.Rows.Count - 1
For J = 0 To DTview.ColumnCount - 1
worksheet.Cells(I + 1, J + 1) = DTview.Rows(I).Cells(J).Value
Next J
Next I
Catch ex As Exception
MsgBox( 对不起,导入Excel文件出错! , MsgBoxStyle.Information)

End Try
End Sub
End Module
__________________________________________________________________________