[VB.NET]如何将DataGridView中的数据存入Excel文件中?
来源:互联网 发布:linux 查看网络端口 编辑:程序博客网 时间:2024/05/16 17:44
<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
__________________________________________________________________________
如何将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.NET]如何将DataGridView中的数据存入Excel文件中?
- VB.net 将dataGridView中的数据导出到excel
- 如何将datagridview中的数据导出到Excel中(转)
- 如何将datagridview中的数据导出到Excel中
- 在VS2005中,将DataGridView中的数据导入Excel文件
- [VB.NET]如何清除DataGridView中的数据?
- vb.net 将DataGridView数据导入到Excel的方式
- [VB.NET]请问如何将从数据库提出来的一列数据存入一个数组中?
- 将DataGridView中的数据导入Excel中
- 将excel中的数据导入datagridview中
- 如何将VB中的数据导出excel,word中去!
- 将DataGridview中的数据存入数据库
- VB.NET DataGridView中的数据导入到Excel
- 将DataGridView中的数据导入Excel中,并显示Excel
- vb.net 读取EXCEL文件中的数据
- C#中怎么从如何将DataGridView中的数据导出到Excel中
- 在VS2005中,将DataGridView中的数据导入到Excel文件中
- vb.net excel数据导入datagridview显示
- 基于Hibernate3.2 Hibernate tools 的使用
- 文本安装everest linux
- Hibernate常见的20个问题
- [VB.NET]如何在Datagridview中进行列间的计算?
- [VB.NET]VB2005中有Table控件吗?
- [VB.NET]如何将DataGridView中的数据存入Excel文件中?
- [VB.NET]TabControl与DataGridView能并用吗 求助
- [VB.NET]送分问题!!!我在窗体上利用鼠标动态的画矩形,就像QQ截图的矩形选框那样.可是怎么清除之前画的矩形留下的痕迹?
- 微软虚拟机Server 2008 Hyper-V
- ARM920T协处理器(CP15)指令表
- [VB.NET]菜鸟求助:运行代码错误?
- [VB.NET]用WebRequest,WebResponse,获取网页源代码,还可以更快吗?
- [VB.NET]如何解决类似VFP宏替换的问题?
- java Date总结