C#锁定EXCEL工作表

来源:互联网 发布:天刀捏脸数据男钟汉良 编辑:程序博客网 时间:2024/05/16 09:03
  1. public void CreateExcel()
  2.     {
  3.         //创建一个Excel文件
  4.         Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
  5.         Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
  6.         Microsoft.Office.Interop.Excel.Worksheet excelSheet = null;
  7.         myExcel.Application.Workbooks.Add(true);
  8.         //让Excel文件可见
  9.         myExcel.Visible = true;
  10.         myExcel.Cells[1, 4] = "普通报表";
  11.         //逐行写入数据
  12.         for (int i = 0; i < 11; i++)
  13.         {
  14.             for (int j = 0; j < 7; j++)
  15.             {
  16.                 //以单引号开头,表示该单元格为纯文本
  17.                 myExcel.Cells[2 + i, 1 + j] = "'" + i;
  18.             }
  19.         }
  20.         try
  21.         {
  22.             string excelTemp ="c://a.xls";        
  23.             //excelWorkbook = myExcel.Workbooks[1];
  24.             excelWorkbook = myExcel.ActiveWorkbook;
  25.             excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.ActiveSheet;
  26.             
  27.             //设定允许操作的单元格
  28.             Microsoft.Office.Interop.Excel.AllowEditRanges ranges = excelSheet.Protection.AllowEditRanges;
  29.             ranges.Add("Information",
  30.                 myExcel.Application.get_Range("B2""B2"),
  31.                 Type.Missing);
  32.             //保护工作表
  33.             excelSheet.Protect("MyPassword", Type.Missing, Type.Missing, Type.Missing,
  34.         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  35.         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  36.         Type.Missing, true, Type.Missing, Type.Missing);
  37.             //Realease the com object
  38.             System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
  39.             excelSheet = null;
  40.             //Save the result to a temp path
  41.             excelWorkbook.SaveAs(excelTemp, Excel.XlFileFormat.xlWorkbookNormal,
  42.                                  nullnullfalsefalse,
  43.                                  Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
  44.                                  Type.Missing, Type.Missing,Type.Missing,Type.Missing);
  45.         }
  46.         catch (Exception ex)
  47.         {
  48.             throw;
  49.         }
  50.         finally
  51.         {
  52.             if (excelWorkbook != null)
  53.             {
  54.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
  55.                 excelWorkbook = null;
  56.             }
  57.             if (myExcel != null)
  58.             {
  59.                 myExcel.Workbooks.Close();
  60.                 myExcel.Quit();
  61.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
  62.                 myExcel = null;
  63.             }
  64.             GC.Collect();
  65.         }
  66.     }
对Excel操作时,由于使用权限的不同,可能对表格的操作权限也不一样。EXCEL提供了保护工作表以及允许编辑单元格功能。相应的在C#中就可以对Excel表格进行操作。

主要用Protect()方法保护工作表,Worksheet.Protection.AllowEditRanges设置允许编辑的单元格。

下面的代码示例演示如何实现对EXCEL进行保护的操作。

原文地址:http://www.cnblogs.com/lisa-zhang/articles/1275869.html