标签:file string range 技术分享 spi dia orange mpi extra
本篇文章将介绍C# 如何处理Excel图形相关的问题,包括以下内容要点:注:Spire.xls能支持的图形种类很多,常见的Office Excel中的图形,这个类库也都能实现:
【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");
}
}
}
图形插入效果:
【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");
}
}
}
提取结果:
【C#】
using Spire.Xls;
namespace HideShapes_XLS
{
class Program
{
static void Main(string[] args)
{
//创建实例,加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//隐藏第3个图形
sheet.PrstGeomShapes[2].Visible = false;
//显示图形
//sheet.PrstGeomShapes[1].Visible = true;
//保存并打开文档
workbook.SaveToFile("HideShape.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("HideShape.xlsx");
}
}
}
设置效果:
【C#】
using Spire.Xls;
namespace RemoveShapes_XLS
{
class Program
{
static void Main(string[] args)
{
//实例化Workbook类对象,加载Excel文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//删除第一个图形
sheet.PrstGeomShapes[0].Remove();
//删除所有图形
//for (int i = sheet.PrstGeomShapes.Count-1; i >= 0; i--)
//{
// sheet.PrstGeomShapes[i].Remove();
//}
//保存并打开文件
workbook.SaveToFile("DeleteShape.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("DeleteShape.xlsx");
}
}
}
图形删除效果:
以上是关于“C#操作Excel中图形”的介绍,如需转载,请注明出处。
标签:file string range 技术分享 spi dia orange mpi extra
原文地址:http://blog.51cto.com/eiceblue/2141552