zoukankan      html  css  js  c++  java
  • excel 保护Sheet

     private void OpenForEditSheet(string strFilePath, string strSheetName)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbooks xlWorkbooks;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
            System.Type tyWorkbooks;
            System.Reflection.MethodInfo[] methods;
            object objFilePath;
            object oMissing = System.Reflection.Missing.Value;
            strFilePath = Server.MapPath(strFilePath);
            if (!System.IO.File.Exists(strFilePath))
            {
                throw new System.IO.FileNotFoundException();
                return;
            }
            try
            {
                xlApp.Visible = true;
                xlWorkbooks = xlApp.Workbooks;
                tyWorkbooks = xlWorkbooks.GetType();
                methods = tyWorkbooks.GetMethods();
                objFilePath = strFilePath;
                object Nothing = System.Reflection.Missing.Value;
                xlWorkbook = xlApp.Workbooks.Open(strFilePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[strSheetName];
                foreach (Worksheet sheet in xlWorkbook.Worksheets)
                {
                    sheet.Protect("123", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                }
                foreach (Worksheet sheet in xlWorkbook.Worksheets)
                {
                    if (sheet.Name.ToUpper() == strSheetName.ToUpper())
                    {
                        sheet.Unprotect("123");
                        
                    }
                }
                xlWorksheet.Activate();
                //xlWorksheet.Visible = XlSheetVisibility.xlSheetVisible;
                //xlWorkbook.Close(oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //if (xlApp != null)
                //{
                //    xlApp.Quit();
                //    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                //    xlApp = null;
                //}
                //GC.Collect();
            }
        }
  • 相关阅读:
    【数据结构】优先队列和堆
    【ACM非算法部分】读入优化
    【解题报告】[动态规划]RQNOJ
    【解题报告】[动态规划]RQNOJ
    【解题报告】[动态规划]-PID69 / 过河卒
    【解题报告】[动态规划]
    【解题报告】[动态规划]RQNOJ PID2 / 开心的金明
    扫描线矩形周长的并 POJ1177
    fft模板 HDU 1402
    离线树状数组 hihocoder 1391 Countries
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/1945134.html
Copyright © 2011-2022 走看看