How to read and write data from excel sheet using c# .net?
How to read and write data from excel sheet using c# .net? Give me the articles and solution?
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.
Joseph BarnesPosted Feb 13, 2013, 1:35 AM
I don't recommend you to use Microsoft automation to read and write Excel files. It is very slow on large files.
In my project I used Excel .NET component to read data from a spreadsheet and export it to the database. It works excellent.
Lasantha PWPosted Jan 4, 2011, 6:32 AM
protected void Button1_Click(object sender, EventArgs e)
{
// manage csv file
DataSet ds = loadCVS(-1);
DataTable table = ds.Tables[0];
foreach (DataRow row in table.Rows)
{
//iterate through datatable
}
}
}
public DataSet loadCVS(int noofrows)
{
DataSet ds = new DataSet();
try
{
// Creates and opens an ODBC connection
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string sql_select;
OdbcConnection conn;
conn = new OdbcConnection(strConnString.Trim());
conn.Open();
//Creates the select command text
if (noofrows == -1)
{
sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
}
else
{
sql_select = "select top " + noofrows + " * from [" + this.FileNevCSV.Trim() + "]";
}
//Creates the data adapter
OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
//Fills dataset with the records from CSV file
obj_oledb_da.Fill(ds, "csv");
//closes the connection
conn.Close();
}
catch (Exception e) //Error
{
}
return ds;
}
Sandeep ShekhawatPosted Jan 3, 2011, 1:49 AM
Read from .xls file (Microsoft Execl 2003).
Use Following namespace
using System.Data.OleDb;
Make a connection from .xls file(Excel File)
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"~/ExcelFileFolderName/" + Filename.xls) + ";Extended Properties=Excel 8.0;");
Retrieve data from Filename.xls file
OleDbDataAdapter dap = new OleDbDataAdapter("select * from [Sheet1$]", con);
Now excel file table insert in DataSet
DataSet ds = new DataSet();
dap.Fill(ds);
Now fruther any working can be DataSet
and DataSet show by GridView using GridView
DataSource property and DataBind() method.
Gomathi PalaniswamyPosted Jan 3, 2011, 12:24 AM
check http://social.msdn.microsoft.com/Forums/en/vsto/thread/b6e8a28c-6760-4e86-a1aa-e2ce9ec36380
Mamta MPosted Jan 3, 2011, 12:14 AM
Refer this article and implement similarly for Excel
http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx
Krishna GaradPosted Jan 3, 2011, 12:09 AM