关于vb.net导出xls的信息

[vb.net] 怎么把listview中数据 导出保存到成excel文件vb.net导出xls我回答你vb.net导出xls的第一个问题vb.net导出xls,因为后面的看不懂你的意思
函数vb.net导出xls:
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 vLV.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 = https://www.04ip.com/post/str'把数组一起写入Excel
wk.worksheets(1).Cells.EntireColumn.AutoFit() '自动调整Excel列
' yy.worksheets(1).name = LV.TopLeftHeaderCell.Value.ToString '表标题写入作为Excel工作表名称
AP_Excel.visible = True '设置Excel可见
【关于vb.net导出xls的信息】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.net 输出 excel这里有段VB6.0vb.net导出xls的vb.net导出xls,vb.net导出xls你可以参考 。
注意添加引用 。
Private Sub Command6_Click()
Dim i, j As Integer
Dim xlApplication As Excel.Application, xlWorkbook As Excel.Workbook, xlSheet
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApplication = GetObject(, "Excel.Application")
Set xlApp = CreateObject("Excel.Application")
If MsgBox("确认将文件信息导出到EXCEL中vb.net导出xls??", vbExclamationvbYesNo, "警告") = vbYes Then
If Err.Number0 Then Set xlApplication = CreateObject("Excel.Application")
Set xlWorkbook = xlApplication.Workbooks.Add
Set xlSheet = xlWorkbook.ActiveSheet
xlSheet.Cells(1, 2) = lblcl.Caption
xlSheet.Range("A1:E1").MergeCells = True
xlSheet.Range("A1:E1").HorizontalAlignment = xlCenter
xlSheet.Cells(2, 2).ColumnWidth = 18
For i = 1 To DataGrid1.Columns.Count
xlSheet.Cells(2, 1) = "编号"
xlSheet.Cells(2, i1) = DataGrid1.Columns(i).Caption
For j = 0 To DataGrid1.VisibleRows - 1
xlSheet.Cells(j3, 1) = j1
xlSheet.Cells(j3, i1) = DataGrid1.Columns(i).CellText(DataGrid1.RowBookmark(j))
Next j
Next i
xlApplication.Visible = True
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApplication = Nothing
'xlApp.Range("A2:L2").Columns.Interior.ColorIndex = 40
'xlApp.Range("A2:L2").Borders.LineStyle = xlContinuous
'xlApp.Visible = True
'xlApp.Range(xlSheet.Cells(2PartsRs.RecordCount1, 1), xlSheet.Cells(2PartsRs.RecordCount1, 8)).Columns.Interior.ColorIndex = 40
'xlApp.Range(xlSheet.Cells(2PartsRs.RecordCount1, 1), xlSheet.Cells(2PartsRs.RecordCount1, 8)).Borders.LineStyle = xlContinuous
Else
MsgBox "无信息可供您导出vb.net导出xls,请确认!", vbExclamationvbOKOnly, "警告"
End If
End Sub
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 = "https://www.04ip.com/post/First Name"
xlWorkSheet.Cells(1, 2).Value = "https://www.04ip.com/post/Last Name"
xlWorkSheet.Cells(1, 3).Value = "https://www.04ip.com/post/Full Name"
xlWorkSheet.Cells(1, 4).Value = "https://www.04ip.com/post/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(i3, j1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
'HardCode in Excel sheet
' this i change to footer line cells
xlWorkSheet.Cells(i3, 7) = "Total"
xlWorkSheet.Cells.Item(i3, 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导出xls和的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读