1
我有下面的代碼將導入我的excel文件。但由於某種原因,它不會顯示原始電子表格中的所有數據。導入Excel文件到datagridview中
原來這裏是電子表格:
這裏是輸出:
出於某種原因,一些受益期月下的列丟失或不出現。
private void btnLoadExcel_Click(object sender, EventArgs e){
string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text +
";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(pathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + txtSheet.Text+ "$]",conn);
conn.Open();
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.AutoResizeColumns();
dataGridView1.DataSource = dt;
for (int i = 0; i < dataGridView1.Columns.Count - 1; i++)
{
dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
dataGridView1.Columns[dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
int colw = dataGridView1.Columns[i].Width;
dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[i].Width = colw;
}
}