Exporting and Importing data
How to exporting and importing data from sql-server database through excel sheet in asp.net.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
carle laiPosted Sep 22, 2009, 10:37 PM
the code for import excel :
code:
private DataTable getDataFromXLS(string path, string blad,string headers)
{
try
{
string strConnectionString =string.Empty;
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+path+";" +
"Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;IMEX=1;" + (char)34;
OleDbConnection cnCSV =new OleDbConnection (strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand (@"SELECT " +headers+ " FROM ["+blad+"]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter();
daCSV.SelectCommand = cmdSelect;
DataTable dtCSV = new DataTable ();
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
Posted Aug 31, 2009, 2:52 PM