简体   繁体   中英

Convert Excel macro to C#?

I have an Excel file (.xlsm) with a macro in it. I want to upload this Excel file onto a server and have C# do it's work on it instead of the VBA macro. The macro looks pretty simple, but I do not understand it well enough to convert it to C#. Below is the macro code:

Sub publishpages()

    'calculate how many iterations
    x = 0
    Sheets("pagegen").Select
    Range("n1").Select
    Range("n1").Copy
    numberOfPages = ActiveCell.Value

    'step through and select each sample
    For x = 0 To numberOfPages

        Sheets("listsample").Select
        Range("A2").Select
        ActiveCell.Offset(x, 0).Range("A1").Select
        Selection.Copy
        Sheets("pagegen").Select
        Range("l1").Select
        ActiveSheet.Paste

        'name folder and filename
        Sheets("pagegen").Select
        Range("ac2").Select
        Range("ac2").Copy
        foldername = ActiveCell.Value

        'publish pages
        Range("d3:q80").Select
        Application.CutCopyMode = False
        Selection.Copy
        ActiveWorkbook.PublishObjects.Add(xlSourceRange, "C:\Temp\" & foldername, "pagegen", "$d$3:$q$80", xlHtmlStatic, "sampleweb11 current_22", "").Publish (True)

    Next x

End Sub

Because I plan to run this on a server, I am looking for a managed library so I do not have to install Office on the server. This is what I am looking at and it even has Linq support: http://epplus.codeplex.com

Any idea on how to start this?

Here's the refactored code:

Option Explicit
Sub publishpages()
    Dim x As Long, numberOfPages As Long
    Dim folderName As String
    numberOfPages = Sheets("pagegen").Range("n1").Value

    For x = 0 To numberOfPages
        Sheets("pagegen").Range("l1") = Sheets("listsample").Range("A2").Offset(x, 0).Range("A1")
        folderName = Sheets("pagegen").Range("ac2")
        ActiveWorkbook.PublishObjects.Add(xlSourceRange, "C:\Temp\" & folderName, "pagegen", "$d$3:$q$80", xlHtmlStatic, "sampleweb11 current_22", "").Publish (True)
    Next x
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM