C# GemBox Excel 导入错误

发布于 2024-08-11 00:03:51 字数 562 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望笑 2024-08-18 00:03:52

GemBox.Spreadsheet 组件不会在 ExtractToDataTable() 方法中自动将数字转换为字符串。

这主要是因为文化问题;有人会期望数字 12.4 转换为“12.4”,而其他人则期望转换为“12,4”。

因此,如果您的 Excel 文件中有值为 int 类型的单元格,并且对应的列是 string 类型 ->将会抛出异常。要覆盖它,您可以使用 ExcelWorksheet.ExtractDataEvent

这是示例:

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

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:

// 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]);
饭团 2024-08-18 00:03:52

我遇到了一些问题,我通过显式访问工作表单元格克服了它

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 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;
半暖夏伤 2024-08-18 00:03:52

我也有同样的问题。我一直在使用类型化数据集,因此当我尝试填充其中一个表时,我遇到了该错误。

这里的问题是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文