gpt4 book ai didi

c# - 使用 OpenXML 将背景图像添加到 Excel

转载 作者:行者123 更新时间:2023-12-04 20:52:30 25 4
gpt4 key购买 nike

我正在使用 Open XML 创建 Excel。我想将背景图像添加到 Excel。
不要告诉我将图像添加到 Excel 并与 OpenXML 生产力工具进行比较。
请让我知道提取方法来做到这一点?

最佳答案

我已经深入研究了使用 OpenXML 将背景图像应用于 Excel 并且我正在共享代码。

class Program
{
static void Main(string[] args)
{
//pathe of the Excel file to add background image
string document = @"Book1.xlsx";
// Open the document for editing.
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(document, true))
{
// Get existing sheets from workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();
// Loop through all sheets and add image as background
foreach (var sheet in sheets.Elements<Sheet>())
{
WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);
CreateWorksheetPart(worksheetPart);
}
}
}

public static void CreateWorksheetPart(WorksheetPart part)
{
ImagePart imagePart1 = part.ImageParts.FirstOrDefault();
if (imagePart1 == null)
imagePart1 = part.AddNewPart<ImagePart>("image/jpeg", "rId1");
string str = part.GetIdOfPart(imagePart1);
GenerateImagePart1Content(imagePart1);
GeneratePartContent(part, str);

}
private static void GenerateImagePart1Content(ImagePart imagePart1)
{
using (var stream = new FileStream("WatermarkDoc.png", FileMode.Open))
{
imagePart1.FeedData(stream);
}
}

private static void GeneratePartContent(WorksheetPart part, string idPart)
{
Worksheet worksheet1 = part.Worksheet;
Picture picture1 = new Picture() { Id = idPart };
worksheet1.Append(picture1);
}
}
使用以下工具在 openXML 中查找更多类。
https://www.microsoft.com/en-us/download/details.aspx?id=30425 ?

关于c# - 使用 OpenXML 将背景图像添加到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com