HI,
when i am exporting to excel, in the cells where data comes, that particular cell format is getting changed, please suggest a solution.
Regards
Navnith
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Suthish NairRank #92 · 22.8K reputationPosted May 19, 2011, 9:50 PM EDT
Dinkar ChavhanRank #1,132 · 1.6K reputationPosted May 19, 2011, 8:27 PM EDT
Step 1: Open Visual Studio > File > New >Website > Under Templates, click ASP.NET WebSite and choose either Visual C# or Visual Basic as the language. Select a location and click Ok.
Step 2: We will create two excel sheets and add them to the project. One excel sheet will be created in Office 2003(.xls) and the other one using Office 2007(.xlsx). Add 4 columns called EID, EName, Age and City to the 'Sheet1'. Also add some data into the columns. Once these excel files are created, add them to your project. To add them to the project, right click project > Add Existing Item > Add the two excel files.
Step 3: We will now create a web.config file to store the connection string information. Right click project > Add New Item > Web.config. Add the following entries to the file
<connectionStrings>
<add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample1.xls;Extended Properties=Excel 8.0"/>
<add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.xlsx;Extended Properties=Excel 12.0"/>
connectionStrings>
As you can observe, the connection string for xlsx (Excel 2007) contains Microsoft.ACE.OLEDB.12.0 as the provider. This is the new Access database engine OLE DB driver and is also capable of reading Excel 2003.
Step 4: Add a GridView to the Default.aspx page. We will extract data from the excel file and bind it to the GridView.
Step 5: Let us now create a connection to the excel file and extract data from it. Before that add a reference to System.Data.OleDb namespcae;
Code followwing
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;"+"Data Source="+Server.MapPath("sample.xlsx")+";"+"Extended Properties=Excel 12.0;");
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", cn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
nav nith16 reputationPosted May 19, 2011, 8:36 PM EDT
nav nith16 reputationPosted May 19, 2011, 10:25 PM EDT