Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to export my data into excel

I have below code which is perfectly working.

C#
protected void btnExport_Click(object sender, EventArgs e)
   {
       Handler h1 = new Handler();
       DataSet ds = new DataSet();
       int RoleId = Convert.ToInt16(Request.Cookies["RoleId"].Value);
       if (RoleId == 1)
           ds=h1.get_MilestoneReport(1, Convert.ToInt16(Request.Cookies["BB"].Value),"");
       else if (RoleId == 3)
           ds=h1.get_MilestoneReport(2, Convert.ToInt16(Request.Cookies["BB"].Value),"");
       string attachment = "attachment; filename=MileStoneReport.xls";
       Response.ClearContent();
       Response.AddHeader("content-disposition", attachment);
       Response.ContentType = "application/vnd.ms-excel";
       string tab = "";
       string tab1 = "";
       foreach (DataTable table in ds.Tables)
       {
           foreach (DataColumn column in table.Columns)
           {
               Response.Write(tab1 + column.ColumnName);
               tab1 = "\t";
           }
       }
       tab = "\n";
       foreach (DataRow dr in ds.Tables[0].Rows)
       {
           for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
           {
               Response.Write(tab + dr[i].ToString());
               tab = "\t";
           }
           tab = "\n";
       }
       Response.End();

   }



Now the issue is, data which is coming in excel is not coming properly what i mean is
width issue, i want to put proper width for each column and some of the values are coming as
VB
2.07135E+12

if i double click that cell its coming right, here it is 2071351010002.

Somebody please help me in formatting this excel so that it makes sense to people.


Thanks Regards,
SUNIL.
Posted
Comments
Pheonyx 16-Jul-13 3:16am    
I imagine that you will need to write a proper exporting routine rather than the one you are using there. There are various free class librarys around that require a bit more work but produce properly structured Excel documents in either XLS or XLSX formats and from the ones I have looked at so far have the ability to auto size columns within worksheets.
sunil mali 16-Jul-13 6:04am    
hi sir,
can u please tell which library i should use?

 
Share this answer
 
As mentioned above OLEDB is NOT the right answer to your request.
OleDb is a DataProvider and has no ability to customize the style of displaying data (however data types are possible).

With OLEDB you just deliver the data 'as is'
similar to your code at the beginning.

Take a look here: Can I format color excel cells in net by oledb-connection and without ms-office-installation[^]

maybe you want to use this excel library
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900