Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OleDb C#将数据写入Excel时出错:此表包含在此电子表格中定义的单元格范围之外的单元格_C#_Excel_Oledb - Fatal编程技术网

使用OleDb C#将数据写入Excel时出错:此表包含在此电子表格中定义的单元格范围之外的单元格

使用OleDb C#将数据写入Excel时出错:此表包含在此电子表格中定义的单元格范围之外的单元格,c#,excel,oledb,C#,Excel,Oledb,我不知道为什么我总是出错: '此表包含定义的单元格范围之外的单元格 在这个电子表格的 我正在尝试将从传感器串行接收的数据写入Excel文档。我尝试过使用“INSERT-INTO”和“UPDATE”语句,这两种语句似乎都不起作用。我希望有人能阐明问题所在,并为可能的解决方案提供提示 我最终通过对代码进行大量调整,部分解决了这个问题,但现在的问题是,在程序将数据写入excel文件的第一行和第二行之后,出现了相同的错误,当关于写入第三行时,它反而输出错误;但是请注意,扩展名为.xls的excel文件会

我不知道为什么我总是出错:

'此表包含定义的单元格范围之外的单元格 在这个电子表格的

我正在尝试将从传感器串行接收的数据写入Excel文档。我尝试过使用“INSERT-INTO”和“UPDATE”语句,这两种语句似乎都不起作用。我希望有人能阐明问题所在,并为可能的解决方案提供提示


我最终通过对代码进行大量调整,部分解决了这个问题,但现在的问题是,在程序将数据写入excel文件的第一行和第二行之后,出现了相同的错误,当关于写入第三行时,它反而输出错误;但是请注意,扩展名为.xls的excel文件会出现此问题

但当我尝试使用.xlsx文件时,程序似乎只能在第一行中写入,然后在尝试在第二行中写入时输出相同的错误。对我可能做错了什么有什么建议吗

谢谢

代码如下:

private void ProcessRxdSerialData(string rxdSerialData, int rxdSerialVal)
    {

        try
        {
            if (!string.IsNullOrEmpty(rxdSerialData))
            {
                Console.WriteLine(rxdSerialData);
                HandleRxdSerialDataResponse(rxdSerialData);

                TimeElapsed = TimeElapsed + 1;
                m_mainForm.PlotGraphs(rxdSerialVal, TimeElapsed);

                string columnSelect = "A";

                m_mainForm.XlsxWrite(rxdSerialData, m_mainForm.DataStorageFileLocation, columnSelect);
            }

        } //try

        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }



 public void XlsxWrite(string rxdSerialData, string dataStorageLocation, string columnSelect)
    {
        try
        {
            Invoke(new MethodInvoker(
                   delegate
                   {
                       if ((checkBoxEnableWriteToExcel.Checked) &&
                           (!string.IsNullOrEmpty(textBoxDataStorageLocation.Text)))
                       {
                           RowCount = RowCount + 1;
                           WriteRxdSerialDataToExcel(rxdSerialData, dataStorageLocation, RowCount, columnSelect);
                       }
                       else
                       {
                           RowCount = 0;
                       }
                   }));
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }


public void WriteRxdSerialDataToExcel(string rxdSerialData, string dataStorageLocation, int timeElapse, string columnSelect)
    {
        int row = timeElapse;
        string column = columnSelect;

        //Converting Row integer to string
        string rowStr = row.ToString();

        //Open Excel File to conduct Write/Update process
        string fileLocation = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataStorageLocation +
                                  @";Extended Properties=""Excel 12.0; HDR=No;IMEX=3;ImportMixedTypes=Text;TypeGuessRows=0"""; //for reading data IMEX = 1, for writting data IMEX = 3
        OleDbConnection pathConnection = new OleDbConnection(fileLocation);
        pathConnection.Open();

        //write the data into the Excel Spread sheet
        string rowColumnCoordinate = String.Concat(column, rowStr);
        string excelWrite = String.Format("UPDATE [Sheet1${0}:{0}] SET F1='{1}'", rowColumnCoordinate, rxdSerialData);
        OleDbCommand command = new OleDbCommand(excelWrite, pathConnection);
        command.ExecuteNonQuery();
        pathConnection.Close();
    }

代码中不清楚,但我怀疑您试图从[0,0]单元格编写excel,并且excel是基于[1,1]的。我对此进行了检查,但似乎不是这样,还有其他建议吗?不多。Excel2007(12.0)大约有1000000行和16000列,而Excel2003只有16000行和256列。如果您的数据库足够大,这可能是原因。我最终设法部分地修改了代码,但现在的问题是,相同的错误发生了,但就在程序将数据写入excel文件的第一行和第二行之后,当关于写入第三行时,它反而输出错误;但是请注意,扩展名为.xls的excel文件会出现此问题。但当我尝试使用.xlsx文件时,程序似乎只能在第一行中写入,然后在尝试在第二行中写入时输出相同的错误。我还编辑了我的帖子,并添加了与我之前发布的帖子相关的其他方法。希望这将有助于澄清问题。谢谢