这个尖锐的教程指导如何使用 C# 插入 Excel 背景图像。它提供了有关编写应用程序时所需的配置和语言结构的信息。使用 C#** 添加 **Excel 背景图像后,您将能够将其保存为 XLSX、XLS 或任何其他受支持的格式。
使用 C# 插入 Excel 背景图像的步骤
- 建立环境添加Aspose.Cells for .NET插入工作表背景图片
- 使用 Workbook 类对象创建新电子表格以添加背景图片
- 访问要添加图像作为背景的第一个 worksheet
- 从要设置的图像文件中读取所有字节
- 通过提供字节数组来设置工作表的 BackgroundImage 属性
- 在选定的工作表中保存具有背景图像的结果工作簿
这些步骤描述了使用 C#* 插入电子表格背景图像的过程。介绍了添加图像背景所需的所有必要类、方法和属性。该过程非常简单,因为图像文件的所有字节都被读入字节数组并设置为所选工作表的 BackgroundImage 属性。
使用 C# 为 Excel 工作表插入背景图片的示例代码
using Aspose.Cells; | |
using System; | |
using System.IO; | |
namespace KbExample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to set Excel sheet background image in c# | |
{ | |
// Initialize license | |
Aspose.Cells.License lic = new Aspose.Cells.License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Create a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get a reference to the first worksheet for adding a background image | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Read the background image bytes and save in an array | |
byte[] imageData = File.ReadAllBytes(@"Background.jpg"); | |
// Set the sheet background-image property | |
sheet.BackgroundImage = imageData; | |
//Save the excel file. | |
workbook.Save(@"WorkbookWithBackground.xlsx"); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
此代码演示了使用 C# 添加 Excel 图像背景的过程。首先,创建或加载目标工作簿,并在要设置背景图像的位置访问对所需工作表的引用。您可以根据要求从文件中读取字节,也可以从数据库或 Web API 中获取字节。
本教程指导我们将背景图像插入到工作簿中的工作表中。如果您想了解在 Excel 单元格中添加图像的过程,请参阅 如何使用 C# 在 Excel 中添加图像 上的文章。