我想编辑/更改文本框中的Excel工作表中的文本。我遍历sharedstringtable中的所有条目,但我找不到它。
var sharedStringTablePart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
foreach (SharedStringItem ss in sharedStringTablePart.SharedStringTable.Elements<SharedStringItem>())
{
if (ss.InnerText == text)
{
//Do somethinhg
}
}
有人可以帮忙吗?
答案 0 :(得分:6)
文本框中包含的文本不存储在sharedstringtable下(我知道它很混乱)。希望这会有所帮助:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(excelFile, true))
{
string sheetName = "Sheet1";
Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
if (sheet == null)
{
// The specified worksheet does not exist.
return null;
}
string relationshipId = sheet.Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
var ocaElems = worksheetPart.DrawingsPart.WorksheetDrawing.Elements<OneCellAnchor>();
foreach (OneCellAnchor oneCellAnchor in ocaElems)
{
var shapes = oneCellAnchor.Elements<A.Shape>();
foreach (var shape in shapes)
{
var text = shape.TextBody.InnerText;
if(text == mytext)
{
//You found the text
}
}
}
}