在C#中使用OleDb读取Excel数据时出现错误(此表包含在此电子表格中定义的单元格范围之外的单元格)

我是学习C#的初学者。 我想从Excel文件读取数据,但我不知道为什么我不断收到错误:

'该表格包含超出此电子表格中定义的单元格范围之外的单元格'

我以前尝试过同样的方法,并没有得到任何错误,但是这是为了一个预先存在的Excel文件。 但是在这种情况下,我先创build了excel文件,然后才从中读取。 当我从列行“A12”开始读取时,就是这样。

我想读取从A12开始的列行的主要原因是确定是否有空单元,以后可以用来input数据; 这个过程需要重复,每次需要将新的数据添加到excel文件中。我希望你能理解我正在做的事情。

这里是代码:

// Initializing C# - Excel Export Method: Microsoft.Office.Interop.Excel.Application m_excelLoanReceiptWrite = new Microsoft.Office.Interop.Excel.Application(); m_excelLoanReceiptWrite.Application.Workbooks.Add(Type.Missing); // Writing Data to Excel m_excelLoanReceiptWrite.Cells[1, 1] = "Loan Receipt"; m_excelLoanReceiptWrite.Cells[3, 1] = "Name"; m_excelLoanReceiptWrite.Cells[5, 1] = "ESM ID No."; m_excelLoanReceiptWrite.Cells[7, 1] = "Mobile No."; m_excelLoanReceiptWrite.Cells[3, 4] = "Loan Date & Time:"; m_excelLoanReceiptWrite.Cells[5, 4] = "Return Date & Time:"; m_excelLoanReceiptWrite.Cells[7, 4] = "Venue:"; m_excelLoanReceiptWrite.Cells[11, 1] = "Recipient's Loan Item List"; m_excelLoanReceiptWrite.Cells[12, 1] = "No."; m_excelLoanReceiptWrite.Cells[12, 2] = "Item Name"; m_excelLoanReceiptWrite.Cells[12, 3] = "Item Barcode"; //Opening and Saving into Excel File: m_excelLoanReceiptWrite.ActiveWorkbook.SaveCopyAs(m_excelLoanReceiptFileFullPath); //Specifying Excel File Name and Location via textBoxExcelLoanItemTrackerLocation.Text m_excelLoanReceiptWrite.ActiveWorkbook.Saved = true; //check tomake sure excel file has been saved //Close Excel File after Write and Save process: m_excelLoanReceiptWrite.Quit(); /*************************** xlswrite sequences to Excel Receipt file for each Loan <ESMID>.xls <End>**********************/ /*************************** Data Extraction from post-written Loan Receipt Excel File <Begin>*****************************/ //Creating a connection directory to access the Excel "LoanItemTracker" file string m_pathSourceExcelLoanReceipt = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + m_excelLoanReceiptFileFullPath + @";Extended Properties=""Excel 8.0; HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0"""; OleDbConnection m_pathConnectionExcelLoanReceipt = new OleDbConnection(m_pathSourceExcelLoanItemTracker); /**********************Excel File "LoanReceipt" Numbering Column data Extraction <Begin> *************************/ string m_commandLoanReceiptNumColumn = "SELECT * FROM [A12:A]"; // SELECT * FROM [A12:A] means selecting the entire 'A' column starting from coordinate A12 DataSet m_loanReceiptNumColumn = new DataSet(); // Extracting all the Number Column information from the Excel file "LoanReceipt" using the connection directory and the preset excel command for row selection OleDbDataAdapter m_loanReceiptNumColumnAdapter = new OleDbDataAdapter(m_commandLoanReceiptNumColumn, m_pathConnectionExcelLoanReceipt); m_loanReceiptNumColumnAdapter.Fill(m_loanReceiptNumColumn); DataTable m_loanReceiptNumColumnMatrix; // Declare a Table Matrix to save the excel data contained in the variable m_loanReceiptNumColumnMatrix int m_loanReceiptNumColumnRowCount; // Integer variable to count the rows of the matrix variable m_loanReceiptNumColumnMatrix m_loanReceiptNumColumnMatrix = m_loanReceiptNumColumn.Tables[0]; // Saving data in m_loanReceiptNumColumn into the Data Table variable m_loanReceiptNumColumnMatrix m_loanReceiptNumColumnRowCount = m_loanReceiptNumColumnMatrix.Rows.Count; // identifying the number of rows in the matrix variable m_loanReceiptNumColumnMatrix /**********************Excel File "LoanReceipt" Numbering Column data Extraction <End> *************************/ 

在工作簿中使用OleDbConnection时,需要在evverey标题行和每个标题列(如数据库)中包含数据。

尝试这个

如果你有空单元格首先填充这些将一些数据来标记空单元格。