1、嵌入VBA代码
首先将vba代码保存成txt文本格式,然后用epplus去调用这个txt文本文件即可,非常简单,当然要想vba调用成功,前提是你的vba代码是没问题的喽!
worksheet.CodeModule.Name = "sheet" ; worksheet.CodeModule.Code = File.ReadAllText(@"VBA-Codevba.txt"), Encoding.Default);
2、Excel加密和锁定
对于一些不希望别人随便改的excel可以对其进行锁定和加密,这样别人只能看不能改了,除非有密码。
worksheet.Protection.IsProtected = true;//设置是否进行锁定 worksheet.Protection.SetPassword("yk");//设置密码 worksheet.Protection.AllowAutoFilter = false;//下面是一些锁定时权限的设置 worksheet.Protection.AllowDeleteColumns = false; worksheet.Protection.AllowDeleteRows = false; worksheet.Protection.AllowEditScenarios = false; worksheet.Protection.AllowEditObject = false; worksheet.Protection.AllowFormatCells = false; worksheet.Protection.AllowFormatColumns = false; worksheet.Protection.AllowFormatRows = false; worksheet.Protection.AllowInsertColumns = false; worksheet.Protection.AllowInsertHyperlinks = false; worksheet.Protection.AllowInsertRows = false; worksheet.Protection.AllowPivotTables = false; worksheet.Protection.AllowSelectLockedCells = false; worksheet.Protection.AllowSelectUnlockedCells = false; worksheet.Protection.AllowSort = false;
3、属性设置
针对整个Excel本身的一些其他设置
epk.Workbook.Properties.Title = "inventory";//设置excel的标题 epk.Workbook.Properties.Author = "mei";//作者 epk.Workbook.Properties.Comments = "this is a test";//备注 epk.Workbook.Properties.Company = "ABC";//公司
4、下拉框
设置下拉框时首先需要设置下拉框显示的数据区域块并将其命名。
var val = worksheet.DataValidations.AddListValidation(worksheet.Cells[7, 8].Address);//设置下拉框显示的数据区域 val.Formula.ExcelFormula = "=parameter";//数据区域的名称 val.Prompt = "下拉选择参数";//下拉提示 val.ShowInputMessage = true;//显示提示内容