vb.net数据导出 vbnet recordset
[vb.net] 怎么把listview中数据 导出保存到成excel文件
我回答你的第一个问题,因为后面的看不懂你的意思
成都创新互联公司网站建设公司一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!专注中小微企业官网定制,成都网站设计、成都网站制作,塑造企业网络形象打造互联网企业效应。
函数:
Public Function ListViewtoExcel(ByVal LV As ListView) As Boolean
Try
If LV.Items.Count = 0 Then '判断记录数,如果没有记录就退出
MessageBox.Show("没有记录可以导出", "没有可以导出的项目", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return False
End If
'如果有记录就导出到Excel
Dim AP_Excel As Object '定义Excel对象
Dim wk As Object '定义Workbook对象
AP_Excel = CreateObject("Excel.Application") '创建Excel对象
wk = AP_Excel.workbooks.add()
Dim i As Integer, u As Integer = 0, v As Integer = 0 '定义循环变量,行变量,列变量
For i = 1 To LV.Columns.Count '把表头写入Excel
wk.worksheets(1).cells(1, i) = LV.Columns(i - 1).Text.Trim
Next
Dim str(LV.Items.Count - 1, LV.Columns.Count - 1) '定义一个二维数组
For u = 0 To LV.Items.Count - 1 '行循环
For v = 0 To LV.Columns.Count - 1 '列循环
If v LV.Items(u).SubItems.Count Then '如果该行该列项存在
str(u, v) = LV.Items(u).SubItems(v).Text.Trim
Else '如果该行该列项不存在
str(u, v) = ""
End If
Next
Next
wk.worksheets(1).range("A2").Resize(LV.Items.Count, LV.Columns.Count).Value = str '把数组一起写入Excel
wk.worksheets(1).Cells.EntireColumn.AutoFit() '自动调整Excel列
' yy.worksheets(1).name = LV.TopLeftHeaderCell.Value.ToString '表标题写入作为Excel工作表名称
AP_Excel.visible = True '设置Excel可见
wk = Nothing '销毁组建释放资源
AP_Excel = Nothing '销毁组建释放资源
Return True '返回真
Catch ex As Exception '错误处理
MessageBox.Show(Err.Description.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) '出错提示
Return False '返回假
End Try
End Function
调用函数:如 ListViewtoExcel(Me.ListView1)
vb中如何将数据导出到excel
介绍
下面通过一步一步的介绍,如何通过VB.NET来读取数据,并且将数据导入到Excel中。
第一步:
打开VS开发工具,并且添加引用。
然后选择。
Microsoft Excel 12.0 object library and。
Microsoft Excel 14.0 object library。
第二步:
创建一个Excle在你的电脑中。
第三步:
在VS中写入如下代码:
Imports System.Data
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel。
Public Class excel
‘添加按钮
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Try
'创建连接
Dim cnn As DataAccess = New DataAccess(CONNECTION_STRING)
Dim i, j As Integer
'创建Excel对象
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
' 打开某一个表单
xlWorkSheet = xlWorkBook.Sheets("sheet1")
' sql查询
' xlWorkBook.Sheets.Select("A1:A2")
Dim sql As String = "SELECT * FROM EMP"
' SqlAdapter
Dim dscmd As New SqlDataAdapter(sql, cnn.ConnectionString)
' 定义数据集
Dim ds As New DataSet
dscmd.Fill(ds)
‘添加字段信息到Excel表的第一行
xlWorkSheet.Cells(1, 1).Value = "First Name"
xlWorkSheet.Cells(1, 2).Value = "Last Name"
xlWorkSheet.Cells(1, 3).Value = "Full Name"
xlWorkSheet.Cells(1, 4).Value = "Salary"
' 将数据导入到excel
For i = 0 To ds.Tables(0).Rows.Count - 1
'Column
For j = 0 To ds.Tables(0).Columns.Count - 1
' this i change to header line cells
xlWorkSheet.Cells(i + 3, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
'HardCode in Excel sheet
' this i change to footer line cells
xlWorkSheet.Cells(i + 3, 7) = "Total"
xlWorkSheet.Cells.Item(i + 3, 8) = "=SUM(H2:H18)"
' 保存到Excel
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
'弹出对话框显示保存后的路径
MsgBox("You can find the file D:\vbexcel.xlsx")
Catch ex As Exception
End Try
End Sub
' Function of Realease Object in Excel Sheet
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
复制代码。
第四步:
看到如下导出结果。
vb.net datagridview显示的数据导出到其他数据库
一行一行的insert into到另一个数据库就行了。一般不使用批量写入,字符串太长
VB.NET怎么将DataGridview里的内容导出到EXCEL去
不好意思 现在才看到;Private Sub 导出EXCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim xlApp, xlBook, xlSheet As Object
xlapp = CreateObject("Excel.Application")
xlbook = xlapp.Workbooks.Add
xlsheet = xlbook.Worksheets(1)
'Dim xlapp As New Excel.Application
'Dim xlbook As Excel.Workbook
'Dim xlsheet As Excel.Worksheet
Dim rowindex, colindex As Integer rowindex = 1 '行
colindex = 0 '列
'xlbook = xlapp.Workbooks.Open("c:\EXCEL.xls") '打开EXCEL文件
xlsheet = xlapp.Worksheets("sheet1") '打开sheet1那页 Dim a As New DataSet
Dim table As New System.Data.DataTable table = DataSet11.Tables("INVMATLISTA") '填充表 Dim row As DataRow '定义row为表格的行
Dim col As DataColumn '定义col为表格的列 '把表格的每一列写到EXCEL去
For Each col In table.Columns
colindex = colindex + 1
xlapp.Cells(1, colindex) = col.ColumnName Next
'把表格的每一行写到EXCEL去
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(col.ColumnName)
Next
Next
xlapp.Visible = True
End Sub 前提要先引用一个Microsoft.Office.Interop.Excel.dll然后在最上面先输入Imports Microsoft.Office.Interop; 就可以了
当前名称:vb.net数据导出 vbnet recordset
网站URL:http://ybzwz.com/article/doipghc.html