我需要一些幫助在datagridview中顯示excel的值。 我管理顯示值,但缺少一些值(列和行的值).i在我的Excel文件中有1000行,而數據網格視圖僅顯示333個項目。我有148列的數量,但它只顯示一些。有人可以告訴我問題是什麼。在DataGridView C中顯示excel工作簿的值#
這裏是我的代碼:
public partial class MainBagsakan : Form
[enter image description here][1]
String [email protected]"C:\Users\tjjtabije\Desktop\TestExcelUpdater\TestUnoREFARM.xlsx";
private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\t-jjtabije\\Desktop\\TestExcelUpdater\\TestUnoREFARM.xlsx;Extended Properties='Excel 12.0 Xml;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text'";
private void WorkOrderTab()
{
string filePath = Path.GetFullPath(WOmain);
string extension = Path.GetExtension(filePath);
string conStr, sheetName;
conStr = string.Empty;
//Get the name of the First Sheet.
using (OleDbConnection con = new OleDbConnection(Excel07ConString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
con.Close();
}
}
using (OleDbConnection con = new OleDbConnection(Excel07ConString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.Connection = con;
cmd.CommandText = "SELECT * [" +sheetName+ "]";
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
//Populate DataGridView.
WorkLoadDisp.DataSource = dt;
label1.Text = dt.Rows.Count.ToString();
}
}
}
}
你應該在'WorkLoadDisp.DataSource = dt;'處設置一個斷點,並檢查dt中的內容。我猜你可能沒有得到你認爲應該的所有數據。只是一個猜測。 – JohnG
嗨,先生。斷點指的是什麼?請進一步解釋我對編程新手抱歉。是的,我沒有得到有關Excel的所有數據。感謝您的幫助 –
如果您使用的是像Visual Studio這樣的IDE,您可以在一行代碼中設置斷點。 (rightClick-> Breakpoint)當你運行代碼時,它將停止在斷點處執行。當它停止時,你可以檢查像'dt'這樣的變量來查看它們包含的內容。想法是,你指向'DataGridView'顯示不正確,然而,在尋找'DataGridView'問題之前,確保你從Excel中獲得的數據是正確的。希望這是有道理的。 – JohnG