excel - 将 Listview 条目转换为 Excel,然后转换为 PDF
问题描述
帮我将列表视图条目转换为 excel 然后 pdf。或者只是帮我将列表视图条目转换为 PDF。我正在使用 Visual Basic 2012。这是我拥有的代码:
Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
'Export the listview to an Excel spreadsheet
SaveFileDialog1.Title = "Save Excel File"
SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.pdf)|*.pdf"
SaveFileDialog1.Filter = "PDF files (*.pdf)|*.pdf"
SaveFileDialog1.ShowDialog()
'exit if no file selected
If SaveFileDialog1.FileName = "" Then
Exit Sub
End If
'create objects to interface to Excel
Dim xls As New Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
'create a workbook and get reference to first worksheet
xls.Workbooks.Add()
book = xls.ActiveWorkbook
sheet = book.ActiveSheet
'step through rows and columns and copy data to worksheet
Dim row As Integer = 1
Dim col As Integer = 1
For Each item As ListViewItem In CustomerView.Items
' lblID.Text = SalesView.SelectedItems(0).Text
For i As Integer = 0 To item.SubItems.Count - 1
sheet.Cells(row, col) = item.SubItems(i).Text
col = col + 1
Next
row += 1
col = 1
Next
'save the workbook and clean up
book.SaveAs(SaveFileDialog1.FileName)
xls.Workbooks.Close()
xls.Quit()
releaseObject(sheet)
releaseObject(book)
releaseObject(xls)
End Sub
Private Sub releaseObject(ByVal obj As Object)
'Release an automation object
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
解决方案
推荐阅读
- sql - 如何根据 MS-Access (Sql) 中的表单的映射表和值生成 ID 序列?
- java - 使用 Stream 将列表中的元素移动到第一个位置
- c# - .NET Core 3.1 中实体的等价物是什么?
- c# - Devexpress Gridcontrol 主从对象列表的设计页面上的配置列,
- ruby - QuickBooks API 示例显示了“curl”的用法,但它实际上不起作用或者我错过了什么
- android - Android Studio 上的测验开发
- api - Alpaca API:如何为我的投资组合获取 5 天的数据?
- javascript - 使嵌入在一段时间内改变颜色 discord.js
- swift - @State 更新视图,但 @ObservedObject 不更新
- python - Append 方法在循环中覆盖以前的值