如何在c#中将excel文件导入datagridview [英] how to import excel file to datagridview in c#
本文介绍了如何在c#中将excel文件导入datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好观众...我尝试在使用控件DataGridView的基于C#的窗口窗体中导入Excel文件...所以任何想法或观点请分享我....谢谢。
Hello viewers...I m try to Import Excel File in a C# based Window Form in using control DataGridView...so any Idea or views please share me....thank you.
推荐答案
//using C# code..Import Excel File in using open file dialog and save Excel sheet name in combo box...
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select file";
openDialog.InitialDirectory = @"c:\";
openDialog.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
openDialog.FilterIndex = 1;
openDialog.RestoreDirectory = true;
if (openDialog.ShowDialog() == DialogResult.OK)
{
if (openDialog.FileName != "")
{
strExcelPathName = openDialog.FileName;
cmbExcelSheet.DataSource = GetSheetNames(openDialog.FileName);
}
else
{
MessageBox.Show("chose Excel sheet path..", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
then ..在按钮中输入此代码单击事件以在DataGridview中显示....
then..write this code in button Click Event to show in DataGridview....
if (System.IO.File.Exists(strExcelPathName))
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""",strExcelPathName);
//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}
,cmbExcelSheet.SelectedItem);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query,connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable dtView = dataSet.Tables [ 0 ];
if (dtView.Rows.Count > 0 )
{
dgrdReciver.Rows.Clear();
dgrdReciver.Rows.Add(dtView.Rows.Count);
int i = 0 ;
foreach (DataRow drow in dtView.Rows)
{
// dgrdReciver.Rows [i] .Cells [SerialNo]。Value = j;
dgrdReciver.Rows [i] .Cells [ Addressid]。值= drow [ ADRSID];
dgrdReciver.Rows [i] .Cells [ ReciverName]。Value = drow [ NAME];
dgrdReciver.Rows [i] .Cells [ companyName]。Value = drow [ COMPANY];
dgrdReciver.Rows [i] .Cells [ RecvAddress]。Value = drow [ ADDRESS];
dgrdReciver.Rows [i] .Cells [ ReciverState]。Value = drow [ STATE];
dgrdReciver.Rows [i] .Cells [ ReciverCity]。Value = drow [ CITY];
dgrdReciver.Rows [i] .Cells [ RecvCountryCode]。Value = drow [ CONTRY CODE];
dgrdReciver.Rows [i] .Cells [ ReciverPostal]。Value = drow [ 邮政编码];
dgrdReciver.Rows [i] .Cells [ RecvContactNo]。Value = drow [ CONTACT NO];
i ++;
}
}
}
else
{
MessageBox.Show ( 没有选择文件);
}
",cmbExcelSheet.SelectedItem); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); DataTable dtView = dataSet.Tables[0]; if (dtView.Rows.Count > 0) { dgrdReciver.Rows.Clear(); dgrdReciver.Rows.Add(dtView.Rows.Count); int i = 0; foreach (DataRow drow in dtView.Rows) { //dgrdReciver.Rows[i].Cells["SerialNo"].Value = j; dgrdReciver.Rows[i].Cells["Addressid"].Value = drow["ADRSID"]; dgrdReciver.Rows[i].Cells["ReciverName"].Value = drow["NAME"]; dgrdReciver.Rows[i].Cells["companyName"].Value = drow["COMPANY"]; dgrdReciver.Rows[i].Cells["RecvAddress"].Value = drow["ADDRESS"]; dgrdReciver.Rows[i].Cells["ReciverState"].Value = drow["STATE"]; dgrdReciver.Rows[i].Cells["ReciverCity"].Value = drow["CITY"]; dgrdReciver.Rows[i].Cells["RecvCountryCode"].Value = drow["CONTRY CODE"]; dgrdReciver.Rows[i].Cells["ReciverPostal"].Value = drow["POSTAL CODE"]; dgrdReciver.Rows[i].Cells["RecvContactNo"].Value = drow["CONTACT NO"]; i++; } } } else { MessageBox.Show("No File is Selected"); }
请参阅..
http://stackoverflow.com/questions/11417231/importing-any-excel-spreadsheet-into-a-datagridview- c-sharp [ ^ ]
http ://csharp.net-informations.com/datagridview/csharp-datagridview-load-excel.htm [ ^ ]
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/70600f48-4269-4001-b f5d-94d6565a6d71 / [ ^ ]
See..
http://stackoverflow.com/questions/11417231/importing-any-excel-spreadsheet-into-a-datagridview-c-sharp[^]
http://csharp.net-informations.com/datagridview/csharp-datagridview-load-excel.htm[^]
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/70600f48-4269-4001-bf5d-94d6565a6d71/[^]
这篇关于如何在c#中将excel文件导入datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文