如何获得“第一”使用 C# 和 SDK 编写 OOXML 工作表?
所以! :) 简单的问题——可能有人问过,但我找不到它。 我正在使用 Open XML SDK 和 C# 从 XLSX 检索数据。 我想要获得“第一个”工作表(就像您在 Excel 中看到的第一个工作表一样),但是当我使用...
WorkbookPart wbPart = workBook.WorkbookPart;
//Now let's find the dimension of the first worksheet
string sheetArea = wbPart.WorksheetParts.First().Worksheet.SheetDimension.Reference.Value;
不幸的是,在全新的 XLSX 中,这会拉出“Sheet3”而不是“Sheet1”。我事先不知道工作表名称,也不能强制用户提交只有一张工作表的工作簿或指定工作表名称。我目前的要求是抓住第一张纸。
有人可以帮忙吗? :)
编辑:我想通了!但我在 7 小时内无法回答自己的问题,所以...
我通过挖掘其他 SO 问题的答案发现了这一点:
Open XML SDK 2.0 - 如何更新电子表格中的单元格?
本质上,一个工作示例可能是这 :
(wbPart.GetPartById(wbPart.Workbook.Sheets.Elements<Sheet>().First().Id.Value) as WorksheetPart).Worksheet.SheetDimension.Reference.Value
SO! :) Simple question -- it's probably been asked, but I could not find it.
I am retrieving data from an XLSX using the Open XML SDK and C#.
I want to get the "first" sheet (as in the first one you would see in Excel), but when I use...
WorkbookPart wbPart = workBook.WorkbookPart;
//Now let's find the dimension of the first worksheet
string sheetArea = wbPart.WorksheetParts.First().Worksheet.SheetDimension.Reference.Value;
Unfortunately, in a brand-new XLSX this pulled "Sheet3" instead of "Sheet1". I do not know the sheet name ahead of time nor can I force the user to submit a workbook with only one sheet or specify sheet name. My present requirements are to grab the first sheet.
Can someone please help? :)
EDIT: I figured it out! But I can't answer my own question for 7 hours, so...
I found this by digging through answers on this other SO question:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
In essence, a working example might be this :
(wbPart.GetPartById(wbPart.Workbook.Sheets.Elements<Sheet>().First().Id.Value) as WorksheetPart).Worksheet.SheetDimension.Reference.Value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,类似:
应该返回第一个工作表。工作簿工作表后代应始终根据它们在工作簿中出现的顺序进行排序,至少根据我的经验。
如果您希望第一个可见,请使用:
As far as I know, something like:
Should return the first worksheet. The workbook Sheet descendants should always be sorted based on the order they appear in the workbook, at least in my experience.
If you wish to get the first visible, use: