C#/.NET添加、读取、删除Excel文档属性Spire.XLS你值得拥有

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

 

示例大纲:

1. 添加文档属性

  1.1 添加摘要信息

  1.2 添加自定义文档信息

2. 读取文档属性

3. 删除文档信息

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

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

 

使用工具:Spire.XLS for .NET pack

获取方法1:通过官网下载包(https://www.e-iceblue.cn/Downloads/Spire-XLS-NET.html)。下载后,解压文件,安装 Bin 文件夹下的程序。安装后,将安装路径下 Bin 文件夹下的 Spire.Xls.dll 文件添加引用至 vs 项目程序。如下所示:

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

 

获取方法2可通过Nuget下载()

地址:https://www.nuget.org/packages/Spire.XLS/

 

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);
        }
    }
}

文档属性添加效果:

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

 

【示例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();
        }
    }
}



 

文档属性读取结果:
C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有
 

【示例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);*/
        }
    }
}



 

文档属性删除结果:

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有原文地址:

https://www.cnblogs.com/Yesi/p/11713788.html

 

我们来看看一些比较常见的图表

2.4.1、饼状图

ExcelChartType.Pie

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

ExcelChartType.Pie3D

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

2.4.2、连线图

ExcelChartType.Line3D

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

ExcelChartType.LineStacked

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

2.4.3、区域图

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

2.4.4、雷达图

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

2.4.5、圆形柱状图

C#/.NET 添加、读取、删除 Excel 文档属性 Spire.XLS 你值得拥有

3、其他功能介绍

关于 Spire.XLS 的其他亮点功能,博主也还在研究,已经知道的一些常用功能比如(1)支持单元格合并、冻结、注释;

(2)数据库方式的导入导出;

(3)Sheet 页的复制、切割、显示、隐藏等;

(4)页眉页脚的设置;

(5)数据的分组、排序;

(6)像 Excel 插入图片,设置图片样式等

这些功能有些已经实现,有些还在研究,等以后有机会再发出来供大家参考。因为篇幅问题,这篇先到这里吧。更多功能可以查看下篇。

 

更多的效果参考地址:

https://www.cnblogs.com/landeanfen/p/5888973.html

© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...