gpt4 book ai didi

c# - 在 EPPlus 4.5.3 中阅读形状和文本框

转载 作者:行者123 更新时间:2023-12-04 19:46:18 24 4
gpt4 key购买 nike

我正在尝试使用 EPPlus 版本 4.5.3 读取 excel 文件,我可以使用以下代码来执行此操作:

FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row; //get row count
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value?.ToString().Trim());
}
}
}
现在,我被困在的地方是形状。因此,我需要阅读的 excel 文件中有形状,这些形状中有我正在尝试阅读的文本。我曾尝试在互联网上搜索此问题,但似乎找不到任何内容。
我怎样才能读取这些数据?到目前为止我尝试过的代码:
foreach (var drawing in sheet.Drawings)
{
var type = drawing.GetType();
var data = drawing.ToString();
Console.WriteLine("Drawing Type:" + type + " Data: " + data);
}

最佳答案

我今天刚遇到这个问题并想通了。您必须首先迭代 worksheet.Drawings 集合以确定是否有任何图纸是“形状”。根据我对 Excel VBA 的了解,您不能将文本放在图像/图片上,它必须是一个形状。如果我错了,有人可以纠正我。

using (ExcelPackage excelPackage = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
foreach (ExcelDrawing dw in worksheet.Drawings)
{
if (dw.GetType().ToString() == "OfficeOpenXml.Drawing.ExcelShape")
{
ExcelShape shape = (ExcelShape)dw;
if (shape.RichText != null && shape.RichText.Count > 0)
{
foreach (ExcelParagraph item in shape.RichText)
{
Console.WriteLine("{0} - Rich Text Line: {1}", dw.Name, item.Text);
}
}
}
else
{ Console.WriteLine("{0} is not a shape, its a {1}", dw.Name, dw.GetType().ToString()); }
}
}
从那里应该很容易跳转来修改图片中的文本:
item.RichText[1].Text = "Updated Text";
输出:
Picture 1 is not a shape, its a OfficeOpenXml.Drawing.ExcelPicture
TextBox 1 - Rich Text Line: Inventory List
TextBox 1 - Rich Text Line: Some Company

关于c# - 在 EPPlus 4.5.3 中阅读形状和文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63524599/

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