1. 绘制图形

【C#】

using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core;

namespace Add_shapes_to_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建实例
            Workbook workbook = new Workbook();
            //获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            //添加“太阳”形状的图形,并填充颜色
            IPrstGeomShape Triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 170, 160, PrstGeomShapeType.Sun);
            Triangle.Fill.ForeColor = Color.Orange;
            Triangle.Fill.FillType = ShapeFillType.SolidColor;            
            Triangle.Text = "IT'S A SUNNY DAY";//添加文本

            //添加“禁止”标志的图形,并填充渐变颜色
            IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 7, 140, 140, PrstGeomShapeType.NoSmoking);
            heart.Fill.ForeColor = Color.Red;
            heart.Fill.FillType = ShapeFillType.Gradient;

            //添加云朵形状的图形
            IPrstGeomShape Cloud = sheet.PrstGeomShapes.AddPrstGeomShape(15, 2, 160, 160, PrstGeomShapeType.Cloud);           
            //设置图形阴影效果
            Cloud.Shadow.Angle = 90;
            Cloud.Shadow.Distance = 10;
            Cloud.Shadow.Size = 100;
            Cloud.Shadow.Color = Color.SteelBlue;
            Cloud.Shadow.Blur = 30;
            Cloud.Shadow.Transparency = 1;
            Cloud.Shadow.HasCustomStyle = true;

            //添加五角星形状的图形,并加载图片来填充图形
            IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(15, 7, 160, 160, PrstGeomShapeType.Star5);
            cloud.Fill.CustomPicture(Image.FromFile("sm.png"), "sm.png");
            cloud.Fill.FillType = ShapeFillType.Picture;

            //保存并打开文档
            workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("AddShapes.xlsx");
        }
    }
}

图形插入效果:

图片 1

1.插入Excel批注

【C#】

步骤1:实例化一个Workbook类实例并加载Excel文档

Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");

步骤2:获取第一个工作表

Worksheet sheet = workbook.Worksheets[0];

步骤3:插入文本批注

string comment = "注意:n 责任人兼设备维护人";//设置批注文本
ExcelFont font = workbook.CreateFont();//设置批注字体格式
font.FontName = "Calibri";
font.Color = Color.Black;
font.IsBold = true;
CellRange range = sheet.Range["I3"];//添加批注到指定单元格
range.Comment.RichText.Text = comment;
range.Comment.Width = 200;
range.Comment.Height = 50;
range.Comment.RichText.SetFont(10, 10, font);

步骤4:插入图片批注

//加载图片,将图片插入到指定单元格的批注
Image image = Image.FromFile("logo.png");
sheet.Range["B2"].Comment.Fill.CustomPicture(image, "logo.png");
sheet.Range["B2"].Comment.Height = image.Height;
sheet.Range["B2"].Comment.Width = image.Width;

步骤5:保存文档

workbook.SaveToFile("AddComment.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("AddComment.xlsx");

批注插入效果(如下图):

图片 2

全部代码:

图片 3图片 4

using System;
using Spire.Xls;
using System.Drawing;

namespace ModifyComment_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Workbook类实例并加载Excel文档
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("test.xlsx");

            //获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            //设置批注文本
            string comment = "注意:n 责任人兼设备维护人";

            //设置批注字体
            ExcelFont font = workbook.CreateFont();
            font.FontName = "Calibri";
            font.Color = Color.Black;
            font.IsBold = true;

            //添加批注到指定单元格
            CellRange range = sheet.Range["I3"];
            range.Comment.RichText.Text = comment;
            range.Comment.Width = 200;
            range.Comment.Height = 50;
            range.Comment.RichText.SetFont(10, 10, font);

            //加载图片,将图片插入到指定单元格的批注
            Image image = Image.FromFile("logo.png");
            sheet.Range["B2"].Comment.Fill.CustomPicture(image, "logo.png");
            sheet.Range["B2"].Comment.Height = image.Height;
            sheet.Range["B2"].Comment.Width = image.Width;

            //保存并打开文档
            workbook.SaveToFile("AddComment.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("AddComment.xlsx");
        }
    }
}

View Code

 

4.添加链接Unc路径

【C#】

//创建一个Workbook类对象,加载Excel文档,获取第二个工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktopSample.xlsx");
Worksheet sheet = workbook.Worksheets[1];

//添加Unc链接至A1单元格,设置连接类型为Unc,添加显示文本及链接路径
CellRange range = sheet.Range["A1"];
HyperLink UncLink = sheet.HyperLinks.Add(range);
UncLink.Type = HyperLinkType.Unc;
UncLink.TextToDisplay = "地址";
UncLink.Address = "\192.168.1.118";

//保存文档
workbook.SaveToFile("LinktoUnc.xls");

效果示例:

图片 5

以上是关于添加Excel超链接的全部内容。

<本文完>

如需转载,请注明出处。

示例代码(供参考)

3.删除Excel批注

【C#】

//实例化Wordbook类实例并加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("Comments.xlsx");

//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];

//删除工作表中的第2个批注
sheet.Comments[1].Remove();

//保存并打开文档
workbook.SaveToFile("RemoveComment.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("RemoveComment.xlsx");

 

 

以上全部为本篇文章的全部内容。

如需转载请注明出处。

<本文完>

 

3.添加链接到指定单元格

【C#】

//新建一个Excel类对象,加载Excel文档,获取第一个工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktopSample.xlsx");
Worksheet sheet = workbook.Worksheets[0];

//获取指定单元格,链接至指定文档中的特定单元格
CellRange range = sheet.Range["E2"];
HyperLink WbLink = sheet.HyperLinks.Add(range);
WbLink.Type = HyperLinkType.Workbook;
WbLink.TextToDisplay = "已清算";
WbLink.Address = "A账目明细!A1";

//保存并打开文档
workbook.SaveToFile("LinktoCell.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("LinktoCell.xlsx");

效果展示:

图片 6

2.提取图形中的文本和图片

【C#】

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Spire.Xls;
using Spire.Xls.Core;

namespace Extract_text_and_image_from_Excel_shape
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建实例,加载Excel工作表
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("test.xlsx");

            //获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            //提取指定形状中的文本内容,并将提取到的文本保存到指定文档
            IPrstGeomShape shape1 = sheet.PrstGeomShapes[0];
            string s = shape1.Text;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(s);
            File.WriteAllText("ExtractText.txt", sb.ToString());
            System.Diagnostics.Process.Start("ExtractText.txt");

            //提取指定图形中的图片,并保存图片到指定文件
            IPrstGeomShape shape2 = sheet.PrstGeomShapes[3];
            Image image = shape2.Fill.Picture;
            image.Save("ShapeImage.png", ImageFormat.Png);
            System.Diagnostics.Process.Start("ShapeImage.png");
        }
    }
}

提取结果:

图片 7

代码示例(供参考)

2.添加链接到文档

【C#】

//实例化一个Workbook类并加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktopSample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];

//设置超链接到指定单元格
CellRange range = sheet.Range["E2"];
HyperLink FileLink = sheet.HyperLinks.Add(range);
FileLink.Type = HyperLinkType.File;
FileLink.TextToDisplay = sheet.Range["E2"].Text;
FileLink.Address = @"C:UsersAdministratorDesktoptest.docx";

//保存并打开文档
workbook.SaveToFile("FileLink.xlsx");
System.Diagnostics.Process.Start("FileLink.xlsx");

效果示例:

图片 8

发表评论

电子邮件地址不会被公开。 必填项已用*标注