解析文本文件并创建Excel报告

时间:2010-10-22 17:58:23

标签: vb.net visual-studio excel vsto spreadsheet

我的应用程序应该解析文本文件(相对简单)并创建Excel电子表格报告。

我应该编写一个独立的VB.NET应用程序来保存excel文件,还是应该使用VSTO?我不确定在开发的容易程度,可用性问题,可用的API函数等方面是否存在任何差异。

是否有其他编程语言/接口/库可以让我快速开发一个涉及的Excel电子表格?我说的是功能,图表等等。

谢谢

3 个答案:

答案 0 :(得分:2)

您可以通过利用excel 2007格式(.xlsx)轻松完成此操作 这是我做的,你可以很容易地修改它。我基本上利用了xlsx文件实际上只是一个包含xml文件的zip文件。

我创建了一个名为Empty.xlsx的空excel文件,并将其作为资源添加到我的应用程序中。 (构建动作嵌入式资源)

我也使用标准zip和解压缩库,因为这就是你如何获得excel文件的部分。

以下是我如何获取数据表并创建excel文件..请注意,实际上并不需要Excel。

   Private Function CreateExcelReport(ByVal FilePath As String, ByVal tbl As DataTable) As FileInfo
    'Just loading the excel file from the assembly, you could do it from a file also
    Dim _assembly As Assembly = Assembly.GetExecutingAssembly
    Dim xlStream As New StreamReader(_assembly.GetManifestResourceStream("YourAssembly.Empty.xlsx"))
'Create a new fileinfo that will hold the outputed excel file with the data.
Dim fiRet As New FileInfo(FilePath)

'Im using Ionic Zip Reduced free library to break the slsx file into its subparts 
Using z As ZipFile = ZipFile.Read(xlStream.BaseStream)
    'Grab Sheet 1 out of the file parts and read it into a string.
    Dim myEntry As ZipEntry = z("xl/worksheets/sheet1.xml")
    Dim msSheet1 As New MemoryStream
    myEntry.Extract(msSheet1)
    msSheet1.Position = 0
    Dim sr As New StreamReader(msSheet1)
    Dim strXMLData As String = sr.ReadToEnd

    'Grab the data in the empty sheet and swap out the data that I want 
    Dim str2 As XElement = CreateSheetData(tbl)
    Dim strReplace As String = strXMLData.Replace("<sheetData/>", str2.ToString)
    z.UpdateEntry("xl/worksheets/sheet1.xml", strReplace)
    'This just rezips the file with the new data it doesnt save to disk
    z.Save(fiRet.FullName)
End Using

'Return a Fileinfo class to be saved to disk or DB or streamed to browser
    Return fiRet
End Function




Private Function CreateSheetData(ByVal dt As DataTable) As XElement

Dim sheedata As XElement = <sheetData></sheetData>

'Create Header Rows
Dim HeaderRow As New XElement(<row></row>)
For j = 0 To dt.Columns.Count - 1
    Dim c As New XElement(<c t="inlineStr"></c>)
    Dim _is As New XElement(<is></is>)
    Dim v As New XElement(<t></t>)
    v.Add(dt.Columns(j).ColumnName)
    _is.Add(v)
    c.Add(_is)
    HeaderRow.Add(c)
Next
sheedata.Add(HeaderRow)

'Create row for each datarow
For Each dr As DataRow In dt.Rows
    Dim newrow As New XElement(<row></row>)
    For j = 0 To dt.Columns.Count - 1
        Dim c As New XElement(<c t="inlineStr"></c>)
        Dim _is As New XElement(<is></is>)
        Dim v As New XElement(<t></t>)
        v.Add(dr(j).ToString)
        _is.Add(v)
        c.Add(_is)
        newrow.Add(c)
    Next
    sheedata.Add(newrow)

Next


    Return sheedata

End Function

答案 1 :(得分:1)

就您开发此应用程序的形式而言,它取决于您。我会根据您想要使用的部署类型进行调整。

如果你选择在excel之外做,那么我会调查EPPlus

答案 2 :(得分:1)

对于商业图书馆,您应该查看Aspose CellsSpreadsheetGear

我已经使用了一段时间,但我记得Aspose方法专注于从代码调用它们的库,并且不需要Excel,而SpreadsheetGear更多地是模板驱动的。