C#锁定EXCEL工作表

对Excel操作时,由于使用权限的不同,可能对表格的操作权限也不一样。EXCEL提供了保护工作表以及允许编辑单元格功能。相应的在C#中就可以对Excel表格进行操作。

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

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

 

 public void CreateExcel()
    {
        //创建一个Excel文件
        Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
        Microsoft.Office.Interop.Excel.Worksheet excelSheet = null;
        myExcel.Application.Workbooks.Add(true);

        //让Excel文件可见
        myExcel.Visible = true;

        myExcel.Cells[1, 4] = "普通报表";

        //逐行写入数据
        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 7; j++)
            {
                //以单引号开头,表示该单元格为纯文本
                myExcel.Cells[2 + i, 1 + j] = "'" + i;
            }
        }

        try
        {
            string excelTemp ="c:\\a.xls";       

            //excelWorkbook = myExcel.Workbooks[1];
            excelWorkbook = myExcel.ActiveWorkbook;
            excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.ActiveSheet;
           
            //设定允许操作的单元格
            Microsoft.Office.Interop.Excel.AllowEditRanges ranges = excelSheet.Protection.AllowEditRanges;
            ranges.Add("Information",
                myExcel.Application.get_Range("B2", "B2"),
                Type.Missing);

            //保护工作表
            excelSheet.Protect("MyPassword", Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, true, Type.Missing, Type.Missing);

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


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

            GC.Collect();
        }
    }

 

转载于:https://www.cnblogs.com/renfeng/archive/2012/05/17/2506250.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Microsoft.Office.Interop.Excel 库来利用 C# 拆分 Excel 工作。以下是一个示例代码,演示如何将一个工作拆分成多个工作,每个工作包含指定数量的行: ```csharp using System; using System.IO; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Excel; namespace ExcelSplitter { class Program { static void Main(string[] args) { string inputFile = "input.xlsx"; int rowsPerSheet = 100; //每个工作包含的行数 //启动 Excel 应用程序 Application excel = new Application(); excel.DisplayAlerts = false; //打开 Excel 文件 Workbook workbook = excel.Workbooks.Open(Path.GetFullPath(inputFile)); //获取原始工作 Worksheet originalSheet = workbook.Sheets[1]; //计算拆分后的工作数量 int totalRows = originalSheet.UsedRange.Rows.Count; int totalSheets = (int)Math.Ceiling((double)totalRows / rowsPerSheet); //循环创建新工作 for (int i = 1; i <= totalSheets; i++) { //新建工作 Worksheet newSheet = workbook.Sheets.Add(Type.Missing, originalSheet, Type.Missing, Type.Missing); newSheet.Name = "Sheet" + i; //复制指定行数的数据到新工作 int startRow = (i - 1) * rowsPerSheet + 1; int endRow = Math.Min(i * rowsPerSheet, totalRows); Range sourceRange = originalSheet.Range["A" + startRow, "Z" + endRow]; Range destinationRange = newSheet.Range["A1", Type.Missing]; sourceRange.Copy(destinationRange); } //保存并关闭 Excel 文件 workbook.Save(); workbook.Close(); excel.Quit(); //释放 COM 对象 Marshal.ReleaseComObject(originalSheet); Marshal.ReleaseComObject(workbook); Marshal.ReleaseComObject(excel); } } } ``` 请注意,您需要在项目中添加对 Microsoft.Office.Interop.Excel 的引用。此外,该示例代码假定 Excel 文件的第一个工作包含要拆分的数据。如果您的情况不同,请相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值