在文档属性中,可以设置诸多关于文档的信息,如创建时间、作者、单位、类别、关键词、备注等摘要信息以及一些自定义的文档属性。下面将通过C#程序来演示如何设置,同时对文档内的已有信息,也可以实现读取或删除等操作。
示例大纲:
2. 读取文档属性
3. 删除文档信息
使用工具:Spire.XLS for .NET pack
获取方法1:通过官网下载包。下载后,解压文件,安装Bin文件夹下的程序。安装后,将安装路径下Bin文件夹下的Spire.Xls.dll文件添加引用至vs项目程序。如下所示:
C# 示例
【示例】添加文档属性
using Spire.Xls; System; namespace AddProperties { class Program { static void Main(string[] args) { //加载Excel文档 Workbook workbook = new Workbook(); workbook.LoadFromFile("test.xlsx"); 设置摘要 workbook.DocumentProperties.Author = Mara; workbook.DocumentProperties.Title = 摘要; workbook.DocumentProperties.Keywords = 摘要,属性; workbook.DocumentProperties.Category = 展示文档; workbook.DocumentProperties.Company = 冰蓝科技; workbook.DocumentProperties.Comments = 请勿修改; workbook.DocumentProperties.Subject = 测试; workbook.DocumentProperties.Manager = Tom; 设置自定义属性 workbook.CustomDocumentProperties.Add(_MarkAsFinal",true); workbook.CustomDocumentProperties.Add(联系电话81705109更新时间,DateTime.Now); 保存文档 workbook.SavetoFile(AddProperties.xlsx属性添加效果:
【示例2】读取文档信息
Spire.Xls.Collections; Spire.Xls.Core; ReadProperties { 加载Excel文档 Workbook wb = Workbook(); wb.LoadFromFile(获取文档属性 Console.WriteLine(摘要信息:); Console.WriteLine(标题: " + wb.DocumentProperties.Title); Console.WriteLine(主题: wb.DocumentProperties.Subject); Console.WriteLine(作者: wb.DocumentProperties.Author); Console.WriteLine(管理者: wb.DocumentProperties.Manager); Console.WriteLine(公司: wb.DocumentProperties.Company); Console.WriteLine(类别: wb.DocumentProperties.Category); Console.WriteLine(关键字: wb.DocumentProperties.Keywords); Console.WriteLine(备注: wb.DocumentProperties.Comments); 获取自定义属性 Console.WriteLine(\n自定义属性:); for (int i = 0; i < wb.CustomDocumentProperties.Count; i++) { Console.WriteLine(wb.CustomDocumentProperties[i].Name + : wb.CustomDocumentProperties[i].Value); } Console.Read(); } } }文档属性读取结果:
【示例3】删除文档属性
Spire.Xls; DeleteProperties { 加载工作簿 Workbook workbook = 删除摘要及自定义文档属性 workbook.DocumentProperties.Clear();删除所有摘要信息 workbook.CustomDocumentProperties.Clear();删除所有自定义文档属性 DeleteProperties.xlsx/*//删除指定摘要及自定义文档属性 workbook.DocumentProperties.Author = "";//设置指定摘要信息为空,删除摘要内容 workbook.CustomDocumentProperties.Remove("联系电话");//删除指定名称的自定义文档属性 workbook.SavetoFile("DeleteCustomDocumentProperties.xlsx",FileFormat.Version2013);*/ } } }
(本文完)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。