日期:2014-05-20  浏览次数:20809 次

C#模拟Excel宏
Sub   Macro5()
'
'   Macro5   Macro
'   宏由   as   录制,时间:   2007-2-15
'

'
        Range( "E2:E13 ").Select
        With   Selection.Validation
                .Delete
                .Add   Type:=xlValidateList,   AlertStyle:=xlValidAlertStop,   Operator:=   _
                xlBetween,   Formula1:= "=$I$1:$I$5 "
                .IgnoreBlank   =   True
                .InCellDropdown   =   True
                .InputTitle   =   " "
                .ErrorTitle   =   " "
                .InputMessage   =   " "
                .ErrorMessage   =   " "
                .IMEMode   =   xlIMEModeNoControl
                .ShowInput   =   True
                .ShowError   =   True
        End   With
End   Sub

这个宏怎么用C#来模拟呀?

------解决方案--------------------
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
//.....................

Microsoft.Office.Interop.Excel.Application vExcel =
new Microsoft.Office.Interop.Excel.Application();
vExcel.Workbooks.Add(true);
vExcel.Visible = true;
Worksheet vWorksheet = (Worksheet)vExcel.Worksheets[1];
Range vRange = vWorksheet.get_Range( "E2 ", "E13 ");
vRange.Select();
vRange.Validation.Delete();
vRange.Validation.Add(XlDVType.xlValidateList,
XlDVAlertStyle.xlValidAlertStop,
XlFormatConditionOperator.xlBetween, "=$I$1:$I$5 ", null);

vRange.Validation.IgnoreBlank = true;
vRange.Validation.InCellDropdown = true;
vRange.Validation.InputTitle = " ";

vRange.Validation.ErrorTitle = " ";
vRange.Validation.InputMessage = " ";
vRange.Validation.ErrorMessage = " ";
vRange.Validation.IMEMode = (int)XlIMEMode.xlIMEModeNoControl;
vRange.Validation.ShowInput = true;
vRange.Validation.ShowError = true;

------解决方案--------------------
using Excel = Microsoft.Office.Interop.Excel;

Excel.Application oExcel = null;
Excel.Workbooks oWorkbooks = null;
Excel._Workbook oWorkbook = null;
Excel.Sheets oSheets = null;
Excel._Worksheet oSheet = null;
Excel.Range oRange = null;
//Excel.Font oFont = null;

// Frequenty-used variable for optional arguments.
object missing = System.Reflection.Missing.Value;

//Start Excel and get Application object.
oExcel = new Excel.Application();
//Excel项目可见
//oExcel.Visible = true;

//Get a new workbooks
oWorkbooks = (Excel.Workbooks)oExcel.Workbooks;
//Get a new workbook.