我是初学者,学习C#。我正在尝试从excel文件中读取数据,但我不知道为什么我一直收到错误:
'此表包含超出此电子表格“
中定义的单元格范围的单元格我之前尝试过相同的方法,并没有收到任何错误,但这是针对预先存在的excel文件。但在这种情况下,我首先创建excel文件,然后只读取它。事情就是当我从列行“A12”开始读取它时。
我想从A12开始读取列行的主要原因是确定是否有空单元格,以后可用于输入数据;每次需要将新数据添加到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> *************************/
答案 0 :(得分:1)
将OleDbConnection与工作簿一起使用时,您需要在evverey标题行和每个标题列(如数据库)中包含数据。
试试这个
如果你有空单元格,首先填写那些将要标记的数据 空单元格。