Using C# to manipulate Excel

H

HSalim

Hi

I want to open an Excel workbook, sort the data, remove subtotals and save
it.
Then I want to import the data into a dataset - I can do that using ADO or
perhaps reading each row and inserting it into a datatable.

I have used Excel VBA to outline what I want to achieve but I am having
difficulty translating the syntax.
Any assistance translating this code would be greatly appreciated.

Thanks in advance

REgards
Habib


----------- VBA Code: -----

Sub Cleanfile()
Dim l As Long
Worksheets(1).Select
Range("A1:O1").Value = Array("Name", "Address1", "Address2", "Address3",
"City", "State", "Zip", "StartDate" "Num")
l = Range("A65536").End(xlUp).Row
Range("A" & l).Select

Rows(2).Resize(l - 1).Sort Key1:=Range("H2"), order1:=xlDescending,
Header:=xlNo

l = Range("A65536").End(xlUp).Row
Range("A" & l).Select

For i = 2 To l
If Not IsDate(Cells(i, 8).Value) Then Cells(i, 8).Value = Date
If Not IsNumeric(Cells(i, 9).Value) Then
Rows(i).ClearContents

Else
If Cells(i, 9).Value = 0 Then Rows(i).ClearContents

If Left(Cells(i, 1).Value, 8) = "SubTotal" Then
Rows(i).ClearContents

ElseIf Left(Cells(i, 1).Value, 11) = "Grand_Total" Then
Rows(i).ClearContents

End If
End If
Next

l = Range("A65536").End(xlUp).Row
Range("A" & l).Select

Rows(2).Resize(l - 1).Sort Key1:=Range("H2"), order1:=xlDescending,
Header:=xlNo
Rows(l).Select
ActiveWorkbook.SaveAs "Demo.xls"

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top