[英]ExcelDataReader in C# - How to reference an individual Cell using row and column cordinates
I'm reading an.xlsx spreadsheet into a C# console app with a view to outputting the content as a formatted xml file (to be picked up by another part of the system further down the line).我正在将一个 .xlsx 电子表格读入 C# 控制台应用程序,以便将内容输出为格式化的 xml 文件(由系统的另一部分进一步提取)。
The problem with the the.xslx file is that it's a pro-forma input document based on, and replacing, an old paper-based order form we used to provide to customers, and the input fields aren't organised as a series of similar rows (except in the lower part of the document which consists of up to 99 rows of order detail lines). .xslx 文件的问题在于它是一个形式上的输入文档,它基于并替换了我们过去提供给客户的旧的纸质订单,并且输入字段没有组织为一系列类似的行(文档下部除外,它由多达 99 行订单明细行组成)。 Some of the rows in the header part of the form/sheet are a mixture of label text AND data;
表格/表格的 header 部分中的某些行是 label 文本和数据的混合; same with the columns.
与列相同。
Effectively, what I need to do is to be able to cherry pick data from the initial dozen or so rows in order to poke data into the xml structure;实际上,我需要做的是能够从最初的十几行中挑选数据,以便将数据插入 xml 结构; the latter part of the document I can process by iterating over the rows for the order detail lines.
我可以通过遍历订单明细行的行来处理文档的后半部分。
I can't use Interop as this will end up as an Azure function - so I've used ExcelDataReader to convert the spreadsheet to a dataset, then convert that dataset to a new dataset entirely composed of string values.我不能使用 Interop,因为这最终会成为 Azure function - 所以我使用 ExcelDataReader 将电子表格转换为数据集,然后将该数据集转换为完全由字符串值组成的新数据集。 But I haven't been able to successfully point to individual cells as I had expected to be using syntax something like
但是我无法成功地指向单个单元格,因为我曾期望使用类似的语法
var cellValue = MyDataSet.Cell[10, 2];
I'd be grateful for any advice as to how I might get the result I need.对于如何获得所需结果的任何建议,我将不胜感激。
A Dataset has Tables and those have Rows which hold ColumnValues 数据集有表,而那些有保存ColumnValues的行
A WorkSheet transforms into a Table (with Columns) and the Cells transform to Rows and column values.工作表转换为表格(带有列),单元格转换为行和列值。
To find the cell value at [10,2] on the first Worksheet do:要在第一个工作表上找到 [10,2] 处的单元格值,请执行以下操作:
var cellValue = MyDataSet.Tables[0].Rows[10][2];
Remember that cellValue will be of type object
.请记住, cellValue 的类型为
object
。 Cast accordingly.相应地投射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.