如何将listview中的数据导出到VB.NET中的excel和pdf [英] How will I export the data from listview to excel and pdf in VB.NET

查看:184
本文介绍了如何将listview中的数据导出到VB.NET中的excel和pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天人们!



我可以请求你的帮助吗? :)我的问题是如何在不使用数据库或水晶报告的情况下将listview中的数据直接导出为ex​​cel和PDF。我有可能这样做吗?



谢谢! :)



请检查链接下面的图片



1. 查看图片:listview to excel和pdf [ ^ ]



我尝试过:



我尝试使用水晶报告并且工作正常,但我需要直接导出它而不使用第三方程序(水晶报告)

解决方案

来自listview到Excel的.csv文件并不难 - 事实上,如果你看这里 FileHelpers Library [ ^ ]您可以将其用于工作的1/2 - 看起来您仍然需要迭代您的列表视图并构建一个对象列表,其中一个对象表示一行和列中的信息(或者可能是listview - > DataTable mi ght是一个选项)



PDF - 几个选项 - 这是 PDF文件编写器C#类库(版本1.19.0增强功能:文档链接) [ ^ ]或iTextSharp ...我不记得Uzi的库用来格式化一个PDF表格,一段时间查看他的例子可以帮助你优化导出到csv步骤的数据结构。



有很多方法可以给猫皮肤

公用Sub export_me_to_excel(BYVAL列表作为ListView的)
尝试
尺寸objExcel作为新Excel.Application
尺寸bkWorkBook作为工作簿
Dim shWorkSheet As Worksheet
Dim chartRange As Excel.Range
Dim i As Integer
Dim j As Integer

objExcel =新Excel.Application
bkWorkBook = objExcel.Workbooks.Add
shWorkSheet = CTYPE(bkWorkBook.ActiveSheet,工作表)
shWorkSheet.DisplayRightToLeft =真

chartRange = shWorkSheet.Range( A1, E2)
chartRange.Merge()
chartRange.FormulaR1C1 = xlval

chartRange.Horizo​​ntalAlignment = 2
chartRange.VerticalAlignment = 2

For i = 0 To list.Columns.Count - 1
shWorkSheet.Cells(5,i + 1)= list.Columns(i).Text
shWorkSheet .Columns.AutoFit()
shWorkSheet.Columns.Horizo​​ntalAlignment = Excel.Constants.xlCenter
shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,Excel.XlColorIndex.xlColorIndexAutomatic,Excel中.XlColorIndex.xlColorIndexAutomatic)

下一个
For i = 0 To list.Items.Count - 1
for j = 0 To list.It ems(i).SubItems.Count - 1
shWorkSheet.Cells(i + 6,j + 1)= list.Items(i).SubItems(j).Text
shWorkSheet.Columns.AutoFit( )
shWorkSheet.Columns.Horizo​​ntalAlignment = Excel.Constants.xlCenter
shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlColorIndex.xlColorIndexAutomatic)

下一个
下一个

objExcel.Visible = True
Catch ex As Exception
MsgBox(ex.Message)
End试试
End Sub


Good day Folks!

Can I ask for your help? :) My question is how will I export the data from listview to excel and PDF directly without using the database or crystal report. Is there a possible way that I can do that?

Thank you! :)

Kindly check for the picture in the linkbelow

1. View image: listview to excel and pdf[^]

What I have tried:

I tried using crystal report and works fine but I need to export it directly without using third party program (crystal reports)

解决方案

Going from a listview to a .csv file for Excel isnt hard - in fact, if you look here FileHelpers Library[^] you can probably use that for 1/2 the work - it looks like you'll still need to iterate your listview and build an a list of objects, where a object represents a row and the information within the columns (or perhaps, listview -> DataTable might be an option)

PDF - a couple of options - there's this PDF File Writer C# Class Library (Version 1.19.0 Enhancement: Document links)[^] or iTextSharp ... I cant recall what Uzi's library uses to format a PDF table, some time looking at his examples may help you optimise the data-structure for the export to csv step.

There's many ways to skin a cat


Public Sub export_me_to_excel(ByVal list As ListView)
    Try
        Dim objExcel As New Excel.Application
        Dim bkWorkBook As Workbook
        Dim shWorkSheet As Worksheet
        Dim chartRange As Excel.Range
        Dim i As Integer
        Dim j As Integer

        objExcel = New Excel.Application
        bkWorkBook = objExcel.Workbooks.Add
        shWorkSheet = CType(bkWorkBook.ActiveSheet, Worksheet)
        shWorkSheet.DisplayRightToLeft = True

        chartRange = shWorkSheet.Range("a1", "e2")
        chartRange.Merge()
        chartRange.FormulaR1C1 = xlval

        chartRange.HorizontalAlignment = 2
        chartRange.VerticalAlignment = 2

        For i = 0 To list.Columns.Count - 1
            shWorkSheet.Cells(5, i + 1) = list.Columns(i).Text
            shWorkSheet.Columns.AutoFit()
            shWorkSheet.Columns.HorizontalAlignment = Excel.Constants.xlCenter
            shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)

        Next
        For i = 0 To list.Items.Count - 1
            For j = 0 To list.Items(i).SubItems.Count - 1
                shWorkSheet.Cells(i + 6, j + 1) = list.Items(i).SubItems(j).Text
                shWorkSheet.Columns.AutoFit()
                shWorkSheet.Columns.HorizontalAlignment = Excel.Constants.xlCenter
                shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)

            Next
        Next

        objExcel.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub


这篇关于如何将listview中的数据导出到VB.NET中的excel和pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆