简体   繁体   English

C#GemBox Excel导入错误

[英]C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: 我正在尝试使用GemBox将excel文件导入到数据表中,但不断出现此错误:

Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. 提取到SourceRowIndex:1和SourceColumnIndex:1的DataTable时的无效数据值。

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]);
}

GemBox.Spreadsheet component doesn't automatically convert numbers to strings in ExtractToDataTable() method. GemBox.Spreadsheet组件不会自动将数字转换为ExtractToDataTable()方法中的字符串。

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". 有人希望数字12.4转换为“ 12.4”,而其他人则转换为“ 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. 因此,如果您的Excel文件具有单元格,其值的类型为int,并且对应的列的类型为string->,则会引发异常。 To override that, you can use ExcelWorksheet.ExtractDataEvent . 要覆盖它,可以使用ExcelWorksheet.ExtractDataEvent

Here's sample: 这是示例:

// Create new ExcelFile
ExcelFile ef = new ExcelFile();

// Add sheet
ExcelWorksheet ws = ef.Worksheets.Add("Sheet1");
// Fill sheet
for (int i = 0; i < 5; i++)
{
    ws.Cells[i, 0].Value = i;   // integer value
    ws.Cells[i, 1].Value = "Row: " + i; // string value
}

// Initialize DataTable
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("text", typeof(string));

// Manage ExtractDataError.WrongType error
ws.ExtractDataEvent += (sender, e) =>
{
    if (e.ErrorID == ExtractDataError.WrongType)
    {
        e.DataTableValue = e.ExcelValue == null ? null : e.ExcelValue.ToString();
        e.Action = ExtractDataEventAction.Continue;
    }
};

// Extract data to DataTable
ws.ExtractToDataTable(dt, 1000, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[0], ws.Columns[0]);

I had the some issue, i overcame it by explicitly accessing worksheet cells 我遇到了一些问题,我通过显式访问工作表单元格克服了它

DataTable dtResult = new DataTable();
int nRows = ws.Rows.Count;
int nCols = 3; //change this according to number of columns in your sheet, for some reason ws.columns.count returns 0
for (int i = 0; i < nCols ; i++)
    dtResult.Columns.Add();

for (int i = 0; i < nRows; i++)
{
    if (ws.Cells[i, 0].Value != null)
        dtResult.Rows.Add(ws.Cells[i, 0].Value.ToString(), ws.Cells[i, 1].Value.ToString(), ws.Cells[i, 2].Value.ToString());
}

return dtResult;

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. 这里的问题是Excel文件中的十进制数字。 Initialy I assigned the columns with decimal values to System.Decimal and that error was thrown. 最初,我将具有十进制值的列分配给System.Decimal,并抛出该错误。

The solution is to change the Type of the Column to System.Double. 解决方案是将“列的类型”更改为System.Double。 Why? 为什么? I don't know, but it worked. 我不知道,但是有效。

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

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