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

I want to export some data into excel. Data in the first,third and fifth column should taken from the database, second and fourth column should be null and should have a column width of 2.5 unit. How can I do it?

Thank you.
Posted
Comments
Maciej Los 14-Aug-12 8:21am    
What have you done till now?

There is no other way than to use interop for this kind of editiong:
http://support.microsoft.com/kb/316383[^]

Other supported Interops:
http://msdn.microsoft.com/en-us/library/15s06t57.aspx[^]

Now, off you go and program... ;)
 
Share this answer
 
I've used the following function for Excel export.

private void ExporttoExcel(DataTable table)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Write(@");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");

        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        //sets font
        HttpContext.Current.Response.Write("<font style="font-size:10.0pt; font-family:Calibri;">");
        HttpContext.Current.Response.Write("<br><br><br>");
        //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<table border="1" bgcolor="#ffffff" hold=" />          " bordercolor="#000000" cellspacing="0" cellpadding="0" style="font-size:10.0pt; font-family:Calibri; background:white;"> <tr>");
        ////am getting my grid's column headers
        //int columnscount = g .Columns.Count;

        //for (int j = 0; j < columnscount; j++)
        //{      //write in new column
        //    HttpContext.Current.Response.Write("<td>");
        //    //Get column headers  and make it as bold in excel columns
        //    HttpContext.Current.Response.Write("");
        //    HttpContext.Current.Response.Write(GridView_Result.Columns[j].HeaderText.ToString());
        //    HttpContext.Current.Response.Write("");
        //    HttpContext.Current.Response.Write("</td>");
        //}
        HttpContext.Current.Response.Write("</tr>");
        foreach (DataRow row in table.Rows)
        {//write in new row
            HttpContext.Current.Response.Write("<tr>");
            
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[0].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td style="width: 2.5px;">");
            HttpContext.Current.Response.Write(row[1].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[2].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td style="width: 2.5px;">");
            HttpContext.Current.Response.Write(row[3].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[4].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[5].ToString());
            HttpContext.Current.Response.Write("</td>");

            HttpContext.Current.Response.Write("</tr>");
        }
        HttpContext.Current.Response.Write("</table>");
        HttpContext.Current.Response.Write("</br></br></br></font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
 
Share this answer
 
v2

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