您好,登錄后才能下訂單哦!
本篇文章將介紹C# 如何處理Excel圖形相關(guān)的問題,包括以下內(nèi)容要點(diǎn):
1、繪制圖形
注:Spire.xls能支持的圖形種類很多,常見的Office Excel中的圖形,這個(gè)類庫(kù)也都能實(shí)現(xiàn):
【C#】
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core;
namespace Add_shapes_to_Excel
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建實(shí)例
Workbook workbook = new Workbook();
//獲取第一個(gè)工作表
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";//添加文本
//添加“禁止”標(biāo)志的圖形,并填充漸變顏色
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);
//設(shè)置圖形陰影效果
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)
{
//創(chuàng)建實(shí)例,加載Excel工作表
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//提取指定形狀中的文本內(nèi)容,并將提取到的文本保存到指定文檔
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");
}
}
}
提取結(jié)果:
【C#】
using Spire.Xls;
namespace HideShapes_XLS
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建實(shí)例,加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//隱藏第3個(gè)圖形
sheet.PrstGeomShapes[2].Visible = false;
//顯示圖形
//sheet.PrstGeomShapes[1].Visible = true;
//保存并打開文檔
workbook.SaveToFile("HideShape.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("HideShape.xlsx");
}
}
}
設(shè)置效果:
【C#】
using Spire.Xls;
namespace RemoveShapes_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化Workbook類對(duì)象,加載Excel文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//刪除第一個(gè)圖形
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");
}
}
}
圖形刪除效果:
以上是關(guān)于“C#操作Excel中圖形”的介紹,如需轉(zhuǎn)載,請(qǐng)注明出處。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。