模拟Office工具栏中: 工具 -〉保护 -〉保护工作表   的功能
很简单,就是对Sheet组件调用 Protect 方法。
 public string CreateExcel(string xmlFile, Object dataSource)
       public string CreateExcel(string xmlFile, Object dataSource)
 {
       {
 string excelTemp = string.Empty;
            string excelTemp = string.Empty;

 //COM Object
            //COM Object
 Excel.Application excelApp = null;
            Excel.Application excelApp = null;
 Excel.Workbook excelWorkbook = null;
            Excel.Workbook excelWorkbook = null;

 try
            try
 {
            {
 excelApp = new Excel.Application();
                excelApp = new Excel.Application();
 excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false,  Excel.XlPlatform.xlWindows, "", true, false, 0, true);
                excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false,  Excel.XlPlatform.xlWindows, "", true, false, 0, true);

 //Protect the sheet
                //Protect the sheet
 string passWord = Guid.NewGuid().ToString();
                string passWord = Guid.NewGuid().ToString();
 excelSheet.Protect(passWord, true, true, true, true);
                excelSheet.Protect(passWord, true, true, true, true);

 //Realease the com object
                //Realease the com object
 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
 excelSheet = null;
                excelSheet = null;


 //Save the result to a temp path
                //Save the result to a temp path
 excelWorkbook.SaveAs(excelTemp, Excel.XlFileFormat.xlWorkbookNormal,
                excelWorkbook.SaveAs(excelTemp, Excel.XlFileFormat.xlWorkbookNormal,
 null, null, false, false,
                                     null, null, false, false,
 Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null);
                                     Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null);
 }
            }
 catch (Exception ex)
            catch (Exception ex)
 {
            {
 throw;
                throw;
 }
            }
 finally
            finally
 {
            {
 if (excelWorkbook != null)
                if (excelWorkbook != null)
 {
                {
 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
 excelWorkbook = null;
                    excelWorkbook = null;
 }
                }
 if (excelApp != null)
                if (excelApp != null)
 {
                {
 excelApp.Workbooks.Close();
                    excelApp.Workbooks.Close();
 excelApp.Quit();
                    excelApp.Quit();
 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
 excelApp = null;
                    excelApp = null;
 }
                }

 GC.Collect();
                GC.Collect(); 
 }
            }

 return excelTemp;
            return excelTemp;
 }
        }
 
很简单,就是对Sheet组件调用 Protect 方法。
 public string CreateExcel(string xmlFile, Object dataSource)
       public string CreateExcel(string xmlFile, Object dataSource) {
       { string excelTemp = string.Empty;
            string excelTemp = string.Empty;
 //COM Object
            //COM Object Excel.Application excelApp = null;
            Excel.Application excelApp = null; Excel.Workbook excelWorkbook = null;
            Excel.Workbook excelWorkbook = null;
 try
            try {
            { excelApp = new Excel.Application();
                excelApp = new Excel.Application(); excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false,  Excel.XlPlatform.xlWindows, "", true, false, 0, true);
                excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false,  Excel.XlPlatform.xlWindows, "", true, false, 0, true);
 //Protect the sheet
                //Protect the sheet string passWord = Guid.NewGuid().ToString();
                string passWord = Guid.NewGuid().ToString(); excelSheet.Protect(passWord, true, true, true, true);
                excelSheet.Protect(passWord, true, true, true, true);
 //Realease the com object
                //Realease the com object System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet); excelSheet = null;
                excelSheet = null;

 //Save the result to a temp path
                //Save the result to a temp path excelWorkbook.SaveAs(excelTemp, Excel.XlFileFormat.xlWorkbookNormal,
                excelWorkbook.SaveAs(excelTemp, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false,
                                     null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null);
                                     Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null); }
            } catch (Exception ex)
            catch (Exception ex) {
            { throw;
                throw; }
            } finally
            finally {
            { if (excelWorkbook != null)
                if (excelWorkbook != null) {
                { System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); excelWorkbook = null;
                    excelWorkbook = null; }
                } if (excelApp != null)
                if (excelApp != null) {
                { excelApp.Workbooks.Close();
                    excelApp.Workbooks.Close(); excelApp.Quit();
                    excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelApp = null;
                    excelApp = null; }
                }
 GC.Collect();
                GC.Collect();  }
            }
 return excelTemp;
            return excelTemp; }
        } 
                    
                     
                    
                 
                    
                 
         
       
 
             
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号