简体   繁体   English

C# 中的 ExcelDataReader - 如何使用行和列坐标引用单个单元格

[英]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.

相关问题 如何使用 ExcelDataReader 从 C# 中的 excel 文件中逐行(按行)获取数据 - How to fetch line by line (row wise) data from excel file in c# using ExcelDataReader C# 错误与 ExcelDataReader - C# error With ExcelDataReader 如何使用C#在Azure函数中添加ExcelDataReader和Excel数据集的依赖项 - How to add the dependencies of ExcelDataReader and Excel dataset in Azure functions using C# 使用行索引和列索引C#获取单元格值 - Get cell value using row index and column index c# 使用C#将单元格地址更改为Excel工作表中的行,列 - Change a cell address to row,column in Excel sheet using C# 如何使用c#中的行索引和列索引从datagridview获取单元格值? - How do I get the cell value from a datagridview using row index and column index in c#? 如何使用 C# 在 Excel.xlsx 文件中基于行列组合在单元格中输入值? - How to enter a value in a cell based on row-column combination in an Excel .xlsx file using C#? Epplus中行和列中的最后一个单元格 - C# - Last cell in row and column in Epplus - C# C#随机坐标是线性的 - C# Random of cordinates is linear 将C#与Open XML SDK 2.5结合使用如何将单元格的值单个字符设置为特定的单个字体颜色 - using C# with the Open XML SDK 2.5 how can you set a cell's value individual characters to specific individual font colours
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM