C# GemBox Excel 导入错误
我正在尝试使用 GemBox 将 excel 文件导入到数据表中,但不断收到此错误:
在 SourceRowIndex: 1 和 SourceColumnIndex: 1 处提取到 DataTable 时数据值无效。
据我所知,我的代码是正确的,并且我的代码是正确的。文件是文件很好。有人有什么想法吗?
谢谢。
ExcelWorksheet Ew = ExFi.Worksheets[0];
for (int i = 0; i < Ew.Columns.Count; ++i)
{
if (Ew.Rows[0].Cells[0, i].Value != null)
dsTable.Columns.Add(Ew.Rows[0].Cells[0, i].Value.ToString(), typeof(string));
}
try
{
Ew.ExtractToDataTable(dsTable, Ew.Rows.Count, ExtractDataOptions.StopAtFirstEmptyRow, Ew.Rows[1], Ew.Columns[0]);
}
I am trying to import an excel file into a data table using GemBox and I keep getting this error:
Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1.
As far as I can tell my code is correct and my file is file fine. Does anyone have any ideas?
Thanks.
ExcelWorksheet Ew = ExFi.Worksheets[0];
for (int i = 0; i < Ew.Columns.Count; ++i)
{
if (Ew.Rows[0].Cells[0, i].Value != null)
dsTable.Columns.Add(Ew.Rows[0].Cells[0, i].Value.ToString(), typeof(string));
}
try
{
Ew.ExtractToDataTable(dsTable, Ew.Rows.Count, ExtractDataOptions.StopAtFirstEmptyRow, Ew.Rows[1], Ew.Columns[0]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GemBox.Spreadsheet 组件不会在 ExtractToDataTable() 方法中自动将数字转换为字符串。
这主要是因为文化问题;有人会期望数字 12.4 转换为“12.4”,而其他人则期望转换为“12,4”。
因此,如果您的 Excel 文件中有值为 int 类型的单元格,并且对应的列是 string 类型 ->将会抛出异常。要覆盖它,您可以使用 ExcelWorksheet.ExtractDataEvent。
这是示例:
GemBox.Spreadsheet component doesn't automatically convert numbers to strings in ExtractToDataTable() method.
That's mainly because of the culture issues; someone would expect that number 12.4 is converted to "12.4" and someone else to "12,4".
So if your Excel file has cell with the value of type int, and corresponding column is of type string -> an exception would be thrown. To override that, you can use ExcelWorksheet.ExtractDataEvent.
Here's sample:
我遇到了一些问题,我通过显式访问工作表单元格克服了它
I had the some issue, i overcame it by explicitly accessing worksheet cells
我也有同样的问题。我一直在使用类型化数据集,因此当我尝试填充其中一个表时,我遇到了该错误。
这里的问题是 Excel 文件中的小数。最初,我将具有十进制值的列分配给 System.Decimal 并引发了该错误。
解决方案是将列的类型更改为 System.Double。为什么?我不知道,但它确实有效。
I had the same problem. I'd been using Typed DataSets so when I tried to populate one of my tables I had that error.
The problem here is the decimal numbers in the Excel file. Initialy I assigned the columns with decimal values to System.Decimal and that error was thrown.
The solution is to change the Type of the Column to System.Double. Why? I don't know, but it worked.