开发者

Best way reading from dirty excel sheets

开发者 https://www.devze.com 2022-12-24 01:39 出处:网络
I have to manipulate some Excel documents with C#.It\'s a batch process with no user interaction.It\'s going to parse data into a database, then output nice reports.The data is very dirty and cannot b

I have to manipulate some Excel documents with C#. It's a batch process with no user interaction. It's going to parse data into a database, then output nice reports. The data is very dirty and cannot be ready using ADO. The data is nowhere near a nice table format.

Best is defined as the most stable(updates less likely to break)/ clear(succinct) code. Fast doesn't matter. If it runs in less than 8 hours I'm fine.

I have the logic to find the data worked out. All I need to make it run is basic cell navigation and getvalue type functions. Give me X cell value as string, if it matches Y value with levenshtein distance < 3, then give me Z cell value.

My question is, what is the best way开发者_如何学C to dig into the excel?

VSTO? Excel Objects Library? Third Option I'm not aware of?


VSTO is kind of a pain because of permissions and the fact that your dll becomes hooked to the document you're using. Assuming you're not actually changing the files, and ADO is definitely not an option, I would say that automation through the Excel COM interfaces is your best bet. It lets you program the way you normally would for any other application, and gives you just as many options for data extraction as VSTO.


The Office programs can be loaded as objects in .NET. The following is the coding stub that I used to load Excel into VB6. The code is essentially going to be the same regardless of which MS language you use.

Dim xlApp As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

On Error Resume Next
wb = xlApp.Workbooks.Open("c:\testdata.xls")
If Err.Number > 0 Then
    If Err.Number = 1004 Then
        MsgBox("File not found")
    Else
        MsgBox("Error " & Err.Number & " occurred.")
    End If
    Exit Sub
End If

ws = wb.Sheets("Sheet1")
Text1.Text = ws.Cells(1, 1).Value

wb = Nothing
ws = Nothing
xlApp = Nothing


Well try to see stack over flow question Convert Excel Range to ADO.NET DataSet or DataTable, etc

0

精彩评论

暂无评论...
验证码 换一张
取 消