OpenFileDialog of = new OpenFileDialog();
of.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
if (of.ShowDialog() == DialogResult.OK)
{
objstream = new StreamReader(of.FileName);
OleDbConnection ExcelConection = null;
OleDbDataAdapter ExcelCommand = null;
try
{
//OleStringBuilder =new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
string conn = @"Provider=Microsoft.jet.OLEDB.4.0;" +
@"Data Source=" + of.FileName + ";" +
@"Extended Properties=" + Convert.ToChar(34).ToString() + ";" +
@"Excel 12.0;HDR=Yes;IMEX=1" + Convert.ToChar(34).ToString();
ExcelConection = new OleDbConnection(conn);
ExcelCommand = new OleDbDataAdapter("Select * From [Sheet1$]", ExcelConection);
ExcelCommand.TableMappings.Add("table", "testtable");
DataSet ds = new DataSet();
ExcelCommand.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
ExcelConection.Close();
//Grid.DataBind();
ExcelConection.Close();
}
catch (Exception etc)
{
MessageBox.Show("error" + etc.Message);
}
}
}
}
i had a problem to show the excel in a datagrid when it reach to step to fill the data in the grid it jump to catch and didn't show the data in the table to the grid .
This post has been edited by mira-magdy: 28 August 2011 - 01:18 AM

New Topic/Question
Reply



MultiQuote








|