![](/img/trans.png)
[英]Create Excel bar chart from a data source in C# code instead of a spreadsheet range using Aspose.Cells
[英]Error (This table contains cells that are outside the range of cells defined in this spreadsheet) when Reading Excel Data using OleDb in C#
我是學習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> *************************/
在工作簿上使用OleDbConnection時,您需要在evverey標頭行和每個標頭列(如數據庫)中包含數據。
嘗試這個
如果首先有空單元格,則將填充一些數據以標記空單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.