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

前端之家收集整理的这篇文章主要介绍了[VB.NET]如何将DataGridView中的数据存入Excel文件中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将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 __________________________________________________________________________ 原文链接:/vb/263600.html

猜你在找的VB相关文章