阅读EPPlus 4.5.3中的形状和文本框



我正在尝试使用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);
}

我今天刚刚遇到这个问题,并解决了它。您必须迭代工作表。首先要确定是否有任何绘图;形状";。根据我对ExcelVBA的了解,你不能把文本放在图像/图片上——它必须是一个形状。如果我错了,有人可以纠正我。

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

相关内容

  • 没有找到相关文章

最新更新