using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Reflection; using System.Runtime.InteropServices;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); xlApp.DisplayAlerts = false; //789就是密码 xlApp.Workbooks.Open("D:\\123.xlsx", Missing.Value, Missing.Value, Missing.Value, "789", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Workbook xlBook = xlApp.Workbooks[1]; if (xlBook.HasPassword) { xlBook.Unprotect(Missing.Value); } //第三个参数是密码 直接给个null 就是没有密码 xlBook.SaveAs("D:\\没有密码.xlsx", Missing.Value,null, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); xlApp.ActiveWorkbook.Close(false, Type.Missing, Type.Missing); xlApp.Quit(); IntPtr t = new IntPtr(xlApp.Hwnd); int k = 0; GetWindowThreadProcessId(t, out k); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); } [DllImport("User32.dll", CharSet = CharSet.Auto)] private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); } }
Word.Document doc=null;
app = new Word.ApplicationClass();
doc = app.Documents.Open(ref objfile, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);if(doc.HasPassword)
{
MessageBox.show("Doc Has Password");}doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);
ActiveDocument.Password=XXX '设置打开文件使用的密码
Documents.Unprotect
为文档设置密码
要只允许授权的审阅者查看或修改您的内容,您可以使用密码来保护整个文档。单击“Microsoft Office 按钮” ,然后单击“另存为”。
单击“工具”,然后单击“常规选项”。
执行下列一项或两项操作:
如果希望审阅者必须输入密码方可查看文档,请在“打开权限密码”框中键入密码。
如果希望审阅者必须输入密码方可保存对文档的更改,请在“修改权限密码”框中键入密码
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); xlApp.DisplayAlerts = false; //789就是密码
xlApp.Workbooks.Open("D:\\123.xlsx", Missing.Value, Missing.Value, Missing.Value, "789", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Workbook xlBook = xlApp.Workbooks[1];
if (xlBook.HasPassword)
{
xlBook.Unprotect(Missing.Value);
}
//第三个参数是密码 直接给个null 就是没有密码
xlBook.SaveAs("D:\\没有密码.xlsx", Missing.Value,null, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); xlApp.ActiveWorkbook.Close(false, Type.Missing, Type.Missing); xlApp.Quit(); IntPtr t = new IntPtr(xlApp.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
} [DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
}
}