如何将excel导入datagridview?我试过这个以下内容 [英] How do I import excel into datagridview? I have tried this follwing
本文介绍了如何将excel导入datagridview?我试过这个以下内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望从本地驱动器导入Excel工作表并在数据网格中查看。
我尝试过:
public void Loadexcel()
{
string FileName =PartList.xlsx;
String name =Sheet1;
String connectionString =;
if(FileName.Substring(FileName.LastIndexOf('。'))。ToLower()==。xlsx)
connectionString =Provider = Microsoft.ACE.OLEDB.12.0 ;数据源=+ FileName +;扩展属性= \Excel 12.0; HDR = YES; IMEX = 0 \;
else
connectionString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ FileName +;扩展属性= \Excel 8.0; HDR = YES; IMEX = 0 \ ;
// string connectionString = String.Format(@Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties =Excel 12.0 XML; HDR = YES; IMEX = 1;,D:\\PartList.xlsx);
// string connectionString = String.Format(@Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties =Excel 8.0; HDR = YES; IMEX = 1;,txtPath.Text);
string query = String.Format(select * from [{0} $],name);
OleDbConnection con = new OleDbConnection(connectionString);
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(query,con);
ds = new System.Data.DataSet();
adapter.Fill(ds);
DataTable dtView = ds.Tables [0];
if(dtView.Rows.Count> 0)
{
dgvPartlist.Rows.Clear();
dgvPartlist.Rows.Add(dtView.Rows.Count);
int i = 0;
foreach(dtView.Rows中的DataRow drow)
{
//dgrdReciver.Rows[i].Cells[\"SerialNo\"].Value = j;
dgvPartlist.Rows [i] .Cells [Sl_No]。Value = drow [Sl_No];
dgvPartlist.Rows [i] .Cells [Description]。Value = drow [Description];
dgvPartlist.Rows [i] .Cells [Item]。Value = drow [Item];
dgvPartlist.Rows [i] .Cells [BEL_Part_No]。Value = drow [BEL_Part_No];
i ++;
}
}
}
解决方案
,名称);
OleDbConnection con = new OleDbConnection(connectionString);
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(query,con);
ds = new System.Data.DataSet();
adapter.Fill(ds);
DataTable dtView = ds.Tables [0 ];
if(dtView.Rows.Count> 0)
{
dgvPartlist.Rows.Clear();
dgvPartlist.Rows.Add(dtView.Rows.Count) ;
int i = 0;
foreach(dtView.Rows中的DataRow drow)
{
//dgrdReciver.Rows[i].Cells[\"SerialNo\"].Value = j;
dgvPartlist.Rows [i] .Cells [Sl_No]。Value = drow [Sl_No];
dgvPartlist.Rows [i] .Cells [Descrip 。值=卓尔[描述];
dgvPartlist.Rows [i] .Cells [Item]。Value = drow [Item];
dgvPartlist.Rows [i] .Cells [BEL_Part_No]。Value = drow [BEL_Part_No];
i ++;
}
}
}
也许你可以使用 EPPlus .NET for .NET GitHub - JanKallman / EPPlus:使用.NET创建高级Excel电子表格 [ ^ ]
参见 [ ^ ]。
I want import Excel sheet from local drive and view in datagrid.
What I have tried:
public void Loadexcel() { string FileName = "PartList.xlsx"; String name = "Sheet1"; String connectionString = ""; if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx") connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR= YES;IMEX=0\""; else connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=0\""; //string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 XML;HDR=YES;IMEX=1;""", "D:\\PartList.xlsx"); //string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text); string query = String.Format("select * from [{0}$]", name); OleDbConnection con = new OleDbConnection(connectionString); System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(query, con); ds = new System.Data.DataSet(); adapter.Fill(ds); DataTable dtView = ds.Tables[0]; if (dtView.Rows.Count > 0) { dgvPartlist.Rows.Clear(); dgvPartlist.Rows.Add(dtView.Rows.Count); int i = 0; foreach (DataRow drow in dtView.Rows) { //dgrdReciver.Rows[i].Cells["SerialNo"].Value = j; dgvPartlist.Rows[i].Cells["Sl_No"].Value = drow["Sl_No"]; dgvPartlist.Rows[i].Cells["Description"].Value = drow["Description"]; dgvPartlist.Rows[i].Cells["Item"].Value = drow["Item"]; dgvPartlist.Rows[i].Cells["BEL_Part_No"].Value = drow["BEL_Part_No"]; i++; } } }
解决方案
", name); OleDbConnection con = new OleDbConnection(connectionString); System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(query, con); ds = new System.Data.DataSet(); adapter.Fill(ds); DataTable dtView = ds.Tables[0]; if (dtView.Rows.Count > 0) { dgvPartlist.Rows.Clear(); dgvPartlist.Rows.Add(dtView.Rows.Count); int i = 0; foreach (DataRow drow in dtView.Rows) { //dgrdReciver.Rows[i].Cells["SerialNo"].Value = j; dgvPartlist.Rows[i].Cells["Sl_No"].Value = drow["Sl_No"]; dgvPartlist.Rows[i].Cells["Description"].Value = drow["Description"]; dgvPartlist.Rows[i].Cells["Item"].Value = drow["Item"]; dgvPartlist.Rows[i].Cells["BEL_Part_No"].Value = drow["BEL_Part_No"]; i++; } } }
Maybe you can use the EPPlus library for .NET GitHub - JanKallman/EPPlus: Create advanced Excel spreadsheets using .NET[^]
See Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
这篇关于如何将excel导入datagridview?我试过这个以下内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文