using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Word = Microsoft.Office.Interop.Word; using System.Threading; using office = Microsoft.Office.Core; using System.Reflection; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { txtOpenPath.Text = Server.MapPath("~/File/excel.xls");//初始化文件打开路径 } } protected void btnSet_Click(object sender, EventArgs e) { if (txtPassword.Text.Trim() == "") { Response.Write("<script>alert('请输入密码!');</script>");//弹出提示信息 return; } Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();//实例化Excel对象 object missing = Missing.Value;//获取缺少的object类型值 //打开指定的Excel文件 Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(txtOpenPath.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //记录用户输入的密码 string P_str_password = txtPassword.Text; workbook.Password = P_str_password;//设置Excel密码 Response.Write("<script>alert('密码设置成功!');</script>");//弹出提示信息 excel.Application.DisplayAlerts = false;//不显示提示对话框 workbook.Save();//保存工作表 workbook.Close(false, missing, missing);//关闭工作表 } protected void btnClear_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();//实例化Excel对象 object missing = Missing.Value;//获取缺少的object类型值 //打开指定的Excel文件 Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(txtOpenPath.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workbook.Password = "";//设置Excel密码为空 Response.Write("<script>alert('密码清除成功!');</script>");//弹出提示信息 excel.Application.DisplayAlerts = false;//不显示提示对话框 workbook.Save();//保存工作表 workbook.Close(false, missing, missing);//关闭工作表 } }
EXCEL:为EXCEL设置和清除密码
最新推荐文章于 2022-01-26 18:13:28 发布