【.Net】如何使用C#代码锁定Excel文件
2007-04-30 00:30
831 查看
模拟Office工具栏中: 工具 -〉保护 -〉保护工作表 的功能
很简单,就是对Sheet组件调用 Protect 方法。
public string CreateExcel(string xmlFile, Object dataSource)
{
string excelTemp = string.Empty;
//COM Object
Excel.Application excelApp = null;
Excel.Workbook excelWorkbook = null;
try
{
excelApp = new Excel.Application();
excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
//Protect the sheet
string passWord = Guid.NewGuid().ToString();
excelSheet.Protect(passWord, true, true, true, true);
//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,
Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null);
}
catch (Exception ex)
{
throw;
}
finally
{
if (excelWorkbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
excelWorkbook = null;
}
if (excelApp != null)
{
excelApp.Workbooks.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
GC.Collect();
}
return excelTemp;
}
很简单,就是对Sheet组件调用 Protect 方法。
public string CreateExcel(string xmlFile, Object dataSource)
{
string excelTemp = string.Empty;
//COM Object
Excel.Application excelApp = null;
Excel.Workbook excelWorkbook = null;
try
{
excelApp = new Excel.Application();
excelWorkbook = excelApp.Workbooks.Open(excelPath, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
//Protect the sheet
string passWord = Guid.NewGuid().ToString();
excelSheet.Protect(passWord, true, true, true, true);
//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,
Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null);
}
catch (Exception ex)
{
throw;
}
finally
{
if (excelWorkbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
excelWorkbook = null;
}
if (excelApp != null)
{
excelApp.Workbooks.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
GC.Collect();
}
return excelTemp;
}
相关文章推荐
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- 如何使用C#代码创建快捷方式文件详解
- GemBox.ExcelLite破解版,可以在没有安装Office的情况,使用.net代码操作Excel文件
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- C#.NET常见问题(FAQ)-如何使用DataGridView跟Excel数据交互
- 使用C#选择文件夹、打开文件夹、选择文件或者如何使用C#选择文件夹
- C#开发中使用Npoi操作excel实例代码
- 要代码 c#中如何向其中插入文件 sql数据库中有一个text类型数据
- 程序中如何删除MO中当前正在使用的SHP文件(C#,MO2.4)
- [C#] 如何有效的使用C#读取文件
- [转]如何有效的使用C#读取文件
- Asp.net/c#+OleDb操作excel文件(二),数据传输使用Parameters
- 如何在C#中使用内嵌资源文件
- 如何在.Net的C#中制作DLL文件
- 如何使用Java代码获取文件、文件流或字符串的编码方式
- C#开发中使用Npoi操作excel实例代码
- 关于.NET程序中使用Excel对象后,如何结束残留的Excel进程。