[英]ExcelDataReader changing columns name
I am reading an Excel file using ExcelDataReader我正在使用 ExcelDataReader 读取 Excel 文件
Dim entireExcel As DataTableCollection
Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel Workbook|*.xlsx|Excel 97-2003 Workbook|*.xls"}
If ofd.ShowDialog() = DialogResult.OK Then
txtFileName.Text = ofd.FileName
Using stream = File.OpenRead(ofd.FileName)
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
Dim reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
entireExcel = reader.AsDataSet(New ExcelDataSetConfiguration() With {
.ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
.UseHeaderRow = True}}).Tables
End Using
End If
End Using
It all works fine, with one exception: The column names in the sheets are not the real ones in Excel.一切正常,但有一个例外:工作表中的列名不是 Excel 中的真实列名。 If the column is a duplicate of a column to the left, it adds _1, _2, etc., to the name.
如果该列是左侧列的副本,则会在名称中添加 _1、_2 等。 If the column name is missing, it creates a name Column0, Column1, Column2, etc.
如果缺少列名,则创建名称 Column0、Column1、Column2 等。
How can I get the real names, or how can I prevent changing them?如何获得真实姓名,或者如何防止更改它们?
the reason for this behaviour is the implementation of DataSet throws a DuplicateNameException
if you try to set duplicate column names [1].这种行为的原因是,如果您尝试设置重复的列名 [1],DataSet 的实现会抛出
DuplicateNameException
。 ExcelDataReader works around this by generating the unique names for you. ExcelDataReader 通过为您生成唯一名称来解决此问题。
You need to find a workaround depending on your case.您需要根据您的情况找到解决方法。 F.ex set
UseHeaderRow
to false and read the column names manually from the data. F.ex 将
UseHeaderRow
设置为 false 并从数据中手动读取列名。 Or don't use AsDataSet()
at all, but instead rely on the reader's Read()
/ GetValue()
/ GetXXX()
methods.或者根本不使用
AsDataSet()
,而是依赖读者的Read()
/ GetValue()
/ GetXXX()
方法。
[1] https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn.columnname [1] https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn.columnname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.