当前位置: 移动技术网 > IT编程>开发语言>c# > C# 添加、读取、删除Excel文档属性

C# 添加、读取、删除Excel文档属性

2019年10月22日  | 移动技术网IT编程  | 我要评论

在文档属性中,可以设置诸多关于文档的信息,如创建时间、作者、单位、类别、关键词、备注等摘要信息以及一些自定义的文档属性。下面将通过c#程序来演示如何设置,同时对文档内的已有信息,也可以实现读取或删除等操作。

示例大纲:

1. 添加文档属性

  1.1 添加摘要信息

  1.2 添加自定义文档信息

2. 读取文档属性

3. 删除文档信息

  3.1 删除所有摘要信息、自定义文档属性

  3.2 删除指定摘要信息、自定义文档属性

 

使用工具:spire.xls for .net pack

获取方法1:通过官网下载包。下载后,解压文件,安装bin文件夹下的程序。安装后,将安装路径下bin文件夹下的spire.xls.dll文件添加引用至vs项目程序。如下所示:

 

 

获取方法2可通过下载。

 

c# 示例

【示例】添加文档属性

using spire.xls;
using 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);
            workbook.customdocumentproperties.add("更新时间", datetime.now);

            //保存文档
            workbook.savetofile("addproperties.xlsx", fileformat.version2010);
        }
    }
}

文档属性添加效果:

 

【示例2】读取文档信息

using spire.xls;
using spire.xls.collections;
using spire.xls.core;
using system;

namespace readproperties
{
    class program
    {
        static void main(string[] args)
        {
            //加载excel文档
            workbook wb = new workbook();
            wb.loadfromfile("addproperties.xlsx");

            //获取文档属性
            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】删除文档属性

using spire.xls;

namespace deleteproperties
{
    class program
    {
        static void main(string[] args)
        {
            //加载工作簿
            workbook workbook = new workbook();
            workbook.loadfromfile("addproperties.xlsx");

            //删除摘要及自定义文档属性
            workbook.documentproperties.clear();//删除所有摘要信息
            workbook.customdocumentproperties.clear();//删除所有自定义文档属性
            
            //保存文档
            workbook.savetofile("deleteproperties.xlsx", fileformat.version2013);

            /*//删除指定摘要及自定义文档属性
            workbook.documentproperties.author = "";//设置指定摘要信息为空,删除摘要内容
            workbook.customdocumentproperties.remove("联系电话");//删除指定名称的自定义文档属性
            workbook.savetofile("deletecustomdocumentproperties.xlsx", fileformat.version2013);*/
        }
    }
}

文档属性删除结果:

 

(本文完)

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网