2013年6月5日 星期三

ASP.NET Encrypt Excel加密

ASP.NET Encrypt Excel加密

C#:
private void EncryptExcel(string FileName)
    {
        try
        {
            FileName = @"D:\Inetpub\PORTAL\Test\MyFilests.xlsx";

            Microsoft.Office.Interop.Excel.Application xlApp = null;
            Workbook wb = null;
            Worksheet ws = null;
            Range aRange = null;
            xlApp = new Microsoft.Office.Interop.Excel.Application();

            //打開Server上的Excel檔案
            xlApp.Workbooks.Open(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //第一個Workbook
            wb = xlApp.Workbooks[1];
            //設密碼
            wb.Password = "123456";
            //另存新檔
            wb.SaveAs(@"D:\Inetpub\PORTAL\Test\NewPasswordx.xlsx");
            //取得worksheet
            ws = (Worksheet)wb.Worksheets[1];
            Label1.Text = ws.Name;
            //開啟檔案
            System.Diagnostics.Process.Start(@"D:\Inetpub\PORTAL\Test\NewPasswordx.xlsx");

        }
        catch (Exception)
        {

            throw;
        }
    }

沒有留言:

張貼留言