C# 使用Gembox.Spreadsheet提取到datatable的数据值无效

C# 使用Gembox.Spreadsheet提取到datatable的数据值无效,c#,excel,gembox-spreadsheet,C#,Excel,Gembox Spreadsheet,我正在使用Gembox尝试将Excel数据导入应用程序,但我似乎无法使ExtractToDataTable方法正常工作 异常消息为 “在SourceRowIndex:1和SourceColumnIndex:4提取到DataTable时,数据值无效” 这是我的密码: private void button1_Click(object sender, EventArgs e) { string path = openFileDialog1.FileName; dataGridView

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

异常消息为

“在SourceRowIndex:1和SourceColumnIndex:4提取到DataTable时,数据值无效”

这是我的密码:

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 void按钮1\u单击(对象发送者,事件参数e)
{
字符串路径=openFileDialog1.FileName;
dataGridView1.DataSource=PopulateDataTable(PopulateExcelWorkSheet(path)).DefaultView;
}
公共Excel工作表填充Excel工作表(字符串路径)
{
path=String.Empty;
//DataTable daTable=新的DataTable();
ExcelFile exFile=新建ExcelFile();
LoadXlsx(@“filepath”,XlsxOptions.None);
Excel工作表ws=exFile.Worksheets[“Sheet1”];
返回ws;
}
公共数据表填充数据表(Excel工作表)
{
DataTable daTable=CreateDataTable(ws.Rows.Count、ws.Columns.Count);
int rowsInWs=ws.Rows.Count;
int colsInWS=ws.Columns.Count;
尝试
{
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(“数据表中的行:{0},数据表中的列{1},WS:{2},WS{3}中的列”,rowCount.ToString(),columnCount.ToString(),rowsInWs.ToString(),colsInWS.ToString());
MessageBox.Show(uhm);
字符串mes=ws.Rows[0]。单元格[3]。Value.ToString();
MessageBox.Show(mes);
返回数据表;
}
公共数据表CreateDataTable(int行、int列)
{
DataTable skeleton=新DataTable();

对于(int i=0;i,以下是我正在使用的代码,该代码对我来说运行良好:

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;
    }
private DataTable ReadExcelFile(字符串flatFilePath,bool firstrowhashreaders)
{
SpreadsheetInfo.SetLicense(“我的密钥”);
ExcelFile ExcelFile=新建ExcelFile();
LoadXls(flatFilePath);
int unnamed=0;
int cols;
字符串[]列;
int curRow=0;
int curCol=0;
DataTable=新的DataTable();
Excel工作表=Excel文件。工作表[0];
对于(cols=0;cols<工作表.行[0].AllocatedCells.Count;cols++)
{
if(第一行标题)
{
if(工作表.Rows[0]。单元格[cols]。值!=null)
dataTable.Columns.Add(工作表.Rows[0].Cells[cols].Value.ToString());
其他的
{
dataTable.Columns.Add(“未命名列”+(++Unnamed));
}
curRow=1;
}
其他的
{
Add(“Column”+(cols+1));
}
}
对于(;curRow
当然,所有内容都作为字符串添加到数据表中,这对于我们来说是完全可以接受的。

您也可以尝试使用API将DataGridView导出/导入到GemBox.Spreadsheet中的Excel文件中

你可以找到

var工作簿=ExcelFile.Load(“input.xlsx”);
//从ExcelFile到DataGridView。
DataGridViewConverter.ExportToDataGridView(
workbook.Worksheets.ActiveWorksheet,
此文件为.dataGridView1,
新的ExportToDataGridViewOptions(){ColumnHeaders=true});

谢谢,伙计,我会给你买一杯虚拟啤酒。唯一的问题是它在150个条目时停止填充,有什么想法吗?我从来没有遇到过。我猜是许可证限制(你是否在方法的第一行分配了许可证密钥?)或AllocatedCells.Count未正确检测到您的数据。我使用的是免费版本,您认为可能是这样吗?刚刚看到行限制是因为免费…不过感谢代码,为我节省了大量的精力。您认为有可能将600行工作表分成4个表,然后将它们连接起来吗?