c# - 使用 Gembox.Spreadsheet 提取到数据表的无效数据值

标签 c# excel gembox-spreadsheet

我正在使用 Gembox 尝试将 Excel 数据导入应用程序,但我似乎无法让 ExtractToDataTable 方法正常工作。

异常信息是这样的

"Invalid Data Value when extracting to DataTable at SourceRowIndex:1 and SourceColumnIndex:4"

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    string path = openFileDialog1.FileName;
    dataGridView1.DataSource = PopulateDataTable(PopulateExcelWorkSheet(path)).DefaultView;
}

public ExcelWorksheet PopulateExcelWorkSheet(string path)
{
    path = String.Empty;
    // DataTable daTable = new DataTable();
    ExcelFile exFile = new ExcelFile();
    exFile.LoadXlsx(@"filepath", XlsxOptions.None);

    ExcelWorksheet ws = exFile.Worksheets["Sheet1"];
    return ws;
}

public DataTable PopulateDataTable(ExcelWorksheet ws)
{
    DataTable daTable = CreateDataTable(ws.Rows.Count, ws.Columns.Count);
    int rowsInWs = ws.Rows.Count;
    int colsInWS = ws.Columns.Count;
    try
    {
        ws.ExtractToDataTable(daTable, rowsInWs, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[1], ws.Columns[1]);
    }
    catch {MessageBox.Show("ARGH"); }
    string mew = daTable.Rows[0][0].ToString();
    int rowCount = daTable.Rows.Count;
    int columnCount = daTable.Columns.Count;
    string uhm = String.Format("Rows in dataTable : {0} , Columns in dataTable {1} , Rows in WS: {2} , Columns in WS {3}", rowCount.ToString(), columnCount.ToString(), rowsInWs.ToString(), colsInWS.ToString());

    MessageBox.Show(uhm);

    string mes = ws.Rows[0].Cells[3].Value.ToString();
    MessageBox.Show(mes);
    return daTable;
}

public DataTable CreateDataTable(int rows, int columns)
{
    DataTable skeleton = new DataTable();

    for (int i = 0; i <= rows; i++)
    {
        skeleton.Rows.Add();
    }

    for (int x = 0; x <= columns; x++)
    {
        skeleton.Columns.Add();
    }
    return skeleton;
}

最佳答案

这是我正在使用的代码,它对我来说运行良好:

private DataTable ReadExcelFile(string flatFilePath, bool firstRowHasHeaders)
    {
        SpreadsheetInfo.SetLicense("MY KEY");
        ExcelFile excelFile = new ExcelFile();
        excelFile.LoadXls(flatFilePath);

        int unnamed = 0;

        int cols;
        string[] columns;

        int curRow = 0;
        int curCol = 0;

        DataTable dataTable = new DataTable();
        ExcelWorksheet worksheet = excelFile.Worksheets[0];

        for (cols = 0; cols < worksheet.Rows[0].AllocatedCells.Count; cols++)
        {
            if (firstRowHasHeaders)
            {
                if (worksheet.Rows[0].Cells[cols].Value != null)
                    dataTable.Columns.Add(worksheet.Rows[0].Cells[cols].Value.ToString());
                else
                {
                    dataTable.Columns.Add("Unnamed Column " + (++unnamed));
                }

                curRow = 1;
            }
            else
            {
                dataTable.Columns.Add("Column " + (cols + 1));
            }
        }

        for (; curRow < worksheet.Rows.Count; curRow++)
        {
            columns = new string[cols];
            for (curCol = 0; curCol < cols; curCol++)
            {
                if (worksheet.Rows[curRow].Cells[curCol].Value == null)
                    columns[curCol] = "";
                else
                    columns[curCol] = worksheet.Rows[curRow].Cells[curCol].Value.ToString();
            }
            dataTable.Rows.Add(columns);
        }

        return dataTable;
    }

当然,所有内容都作为字符串添加到数据表中,这对于我们的目的来说是完全可以接受的。

关于c# - 使用 Gembox.Spreadsheet 提取到数据表的无效数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234053/

相关文章:

excel - 如何使用 Python 将 .xlsm(启用宏的 Excel 文件)转换为 .xlsx?

excel - 在 Excel 中的两个日期之间的每行中生成日期

c# - 在 C# 中编译表单

c# - 在 C# 中使用 Regex 提取 XML 值

Excel 宏消失了

c# - Gembox 电子表格 C# : It is a way to detect if a cell has border?

c# - Gembox - 获取 ExcelCell 的地址

c# - GemBox.Spreadsheet 最后使用的行

c# - ASP.NET MVC 更新模型不起作用?

c# - 动态向我的 View 添加新行