我正在尝试将excel VBA代码转换为c#。此代码必须在excel外部执行,我将转换我的excel宏来执行此操作。这是转换的代码
ActiveWindow.Zoom = 85
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
这是我到目前为止所得到的:
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = true;
excelApp.ActiveWindow.Zoom = 85;
excelApp.Range["A1"].Select();
我无法弄清Range("A1").Select
以下的其余部分。对此有任何帮助
答案 0 :(得分:2)
with语句只是一个VB的东西,用c#你必须创建一个变量并显式调用该变量中的每个属性
ActiveWindow.Zoom = 85;
Range["A1"].Select();
Range(Selection, Selection.End(xlToRight)).Select();
Selection.HorizontalAlignment = xlCenter;
Selection.VerticalAlignment = xlBottom;
Selection.WrapText = true;
Selection.Orientation = 0;
Selection.AddIndent = false;
Selection.IndentLevel = 0;
Selection.ShrinkToFit = false;
Selection.ReadingOrder = xlContext;
Selection.MergeCells = false;
Cells.Select();
(这只是一个示例,请根据需要调整您的COM处理)
编辑:将括号更改为第二行的方括号。
答案 1 :(得分:0)
您需要命名空间“Microsoft.Office.Interop.Excel”。
using Microsoft.Office.Interop.Excel;
MyClass{
...
var app = new Application { Visible = true, ScreenUpdating = true, DisplayAlerts = false };
Workbook book = app.Workbooks.Open(path); //path is the fullFileName as string
Worksheet sheet = book.Worksheets[1];
Range range = sheet.get_Range("A1", "A1");
range.Select();
range.HorizontalAlignment = Constants.xlCenter;
range.VerticalAlignment = Constants.xlBottom;
range.WrapText = true;
range.Orientation = 0;
range.AddIndent = false;
range.IndentLevel = 0;
range.ShrinkToFit = false;
range.ReadingOrder = Constants.xlContext;
range.MergeCells = false;
}
享受它!