简体   繁体   English

(C#)OpenXML使用正确的列数据类型将xlsx导入DataTable

[英](C#) OpenXML import xlsx into DataTable with correct column data type

I am using OpenXML library to import excelsheet(.xslx) into a DataTable using C#. 我正在使用OpenXML库使用C#将excelsheet(.xslx)导入到DataTable中。

The first row of the xlsx contains the column name, so i start adding the column in the DataTable: xlsx的第一行包含列名称,因此我开始在DataTable中添加该列:

foreach (Cell cell in row.Descendants<Cell>())
{
    dt.Columns.Add(GetXlsxCellValue(sharedStringTableList, cell));
}

Then i loop all the other rows and i fill the DataTable by adding any single row to my DataTable in a similar way with any cell in the current row. 然后,我循环所有其他行,并通过以与当前行中任何单元格相似的方式将任何一行添加到我的DataTable中来填充DataTable。

Now, imagine that my excel has two columns like this 现在,假设我的excel有两个这样的列

excelExample excel示例

The first row contain the column name, so the first cell has a DataType = string (the text is stored in sharedStringTableList). 第一行包含列名,因此第一单元格的DataType =字符串(文本存储在sharedStringTableList中)。

All the other rows contains ONLY Number in the first column (and, in excel, the entire column it's also format as "number"). 所有其他行在第一列中仅包含Number(在excel中,整列也将其格式设置为“ number”)。

At the end, in my DataTable, the first column has the correct name ("Number") but the column DataType is STRING . 最后,在我的DataTable中,第一列具有正确的名称(“ Number”),但DataType列为STRING

The question: how can i create my DataTable with the correct column DataType set in the xlsx at the beginning? 问题:如何使用开头在xlsx中设置的正确列DataType创建我的DataTable? The method "Columns.Add()" allow you to specify the column dataType, but in the first row all the cell have DataType = string, so i need to check the column DataType in other way. 方法“ Columns.Add()”允许您指定列dataType,但是在第一行中所有单元格都具有DataType = string,因此我需要以其他方式检查列DataType。

Thanks in advance, 提前致谢,

Andrea 安德里亚

... ...

Well, You are going to have to determine the cell type and then create the column based on that. 好了,您将必须确定单元格类型,然后基于该单元格创建列。 Try look at this post. 尝试看看这篇文章。 Afterwards, you should be able to create your columns based upon the cell type (after you translate it to .NET types). 之后,您应该能够根据单元格类型创建列 (将其转换为.NET类型之后)。 However, if you know your types in advanced and want your data as a List of T, I'd suggest looking at something like ClassToExcel.OpenXml or a similar wrapper library to save yourself the trouble of writing the code. 但是,如果您知道高级的类型,并且希望将数据作为T的列表,则建议您查看ClassToExcel.OpenXml之类的东西或类似的包装器库,从而省去编写代码的麻烦。

There is a library called ClosedXML that is a friendly wrapper for OpenXML I found much easier to use than straight OpenXML. 有一个名为ClosedXML的库,它是OpenXML的友好包装器,我发现它比直接的OpenXML易于使用。 There is an Enum (XLCellValues) that tells you what the excel data type is. 有一个Enum(XLCellValues)告诉您什么是excel数据类型。 This can be used to help you build the correct data column type. 这可以用来帮助您构建正确的数据列类型。

Here is an example of how to navigate and parse the rows and columns into a simple class using closed xml, pretty similar to your example (but for a class instead of data table). 是一个示例,说明如何使用封闭的xml将行和列导航和解析为一个简单的类,与您的示例非常相似(但适用于类而不是数据表)。 There is also a method in that link/example that maps C# sharp types to excel column types that you could use in reverse (XLCellValues enum). 在该链接/示例中,还有一种方法可以将C#尖锐类型映射到可以反向使用的excel列类型(XLCellValues枚举)。 It gets more complicated if you don't trust the given excel data type. 如果您不信任给定的excel数据类型,它将变得更加复杂。

If you have a simple target class you are trying to extract out of the excel this nuget could work out of the box, but I think you want a DataTable output. 如果您有一个简单的目标类,则尝试从excel中提取该nuget可以直接使用,但我认为您需要DataTable输出。

Hope this helps. 希望这可以帮助。

您必须查看以下内容,才能将整个列数据类型设置为text:

Worksheet.Column(9).Style.NumberFormat.Format = "@";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM