开发者

Convert Excel macro to C#?

开发者 https://www.devze.com 2023-04-08 06:09 出处:网络
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 un

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\" & 开发者_StackOverflow社区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
0

精彩评论

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