我有一个带有宏的Excel文件(.xlsm)。我想将这个Excel文件上传到服务器上并让C#做它而不是VBA宏。宏看起来很简单,但我不太清楚将其转换为C#。以下是宏代码:
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
因为我打算在服务器上运行它,所以我正在寻找托管库,所以我不必在服务器上安装Office。这就是我所看到的,甚至还有Linq的支持:http://epplus.codeplex.com
关于如何开始这个的任何想法?
答案 0 :(得分:1)
这是重构的代码:
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