【发布时间】:2017-01-23 13:26:00
【问题描述】:
我需要一些帮助来在 datagridview 中显示 excel 的值。 我设法显示值,但缺少一些值(列和行的值)。我的 excel 文件中有 1000 行,数据网格视图中仅显示 333 个项目。我有 148 列,但它只显示其中的一部分。谁能告诉我问题出在哪里。
这是我的代码:
public partial class MainBagsakan : Form
[enter image description here][1]
String WOmain=@"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中的内容。我猜你可能没有得到你认为应该得到的所有数据。只是猜测。 -
您好,先生。断点是什么意思?请进一步解释,对不起,我是编程新手。是的,我没有得到excel上的所有数据。谢谢你的帮助
-
如果您使用的是像 Visual Studio 这样的 IDE,您可以在一行代码上设置断点。 (右键单击-> 断点)当您运行代码时,它将在断点处停止执行。当它停止时,您可以检查您的变量,如
dt以查看它们包含的内容。想法是您指向DataGridView未正确显示,但这很可能是,但是,在寻找DataGridView问题之前……确保您从 excel 获得的数据是正确的。希望这是有道理的。 -
我已经做了断点,我看不懂它在说什么。
-
我建议您查看帮助部分。在 VS 中,调试时在底部有选项卡以显示局部变量值。
标签: c# excel datagridview