2010-10-22 59 views
0

我的應用程序應該解析一個文本文件(相對容易)並創建一個excel電子表格報告。解析文本文件並創建一個excel報告

我應該編寫一個獨立的VB.NET應用程序來保存excel文件,還是應該使用VSTO?我不確定在開發簡易性,可用性問題,可用的API函數等方面是否存在差異。

是否有任何其他編程語言/接口/庫允許我快速開發涉及的Excel電子表格?我說這樣的事情功能,圖表等

謝謝

回答

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 
+0

請注意,我沒有使用excel,並且所有內容都在內存中完成。它快速發展。 – 2010-10-22 18:35:33

1

至於以何種方式,你開發這個應用程序,這真的取決於你。我會根據您希望使用的部署類型來確定它。

如果您選擇在Excel以外完成,那麼我會查看EPPlus

1

對於商業圖書館,您應該查看Aspose CellsSpreadsheetGear

我已經使用了一段時間,但我記得Aspose方法的重點是從代碼調用它們的庫,並且不需要Excel,而SpreadsheetGear更多地是模板驅動的。

+0

的SpreadsheetGear允許你開始使用模板並對其進行修改,或用新的空工作簿啓動和使用全面的API - 這恰好是非常類似於Excel的API。 – 2010-11-10 14:49:21