Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
I am seeking to export my grid view to excel, however, I do not wanna export it by using the traditional way. I wanna export it by using crystal report and the exported grid in excel should be the same as the original grid on the web.
The below you will find the procedure which I've created to export the grid view to excel in the traditional way. I want to make a procedure to export it by using crystal repoert.
Would you help me, please?

What I have tried:

C#
protected void ExportToExcel()
{
    DataTable dt = new DataTable();

    GridView grdvTemp = new GridView();

    dt.Columns.Add("emp_number");
    dt.Columns.Add("emp_name");
    dt.Columns.Add("ntl_description");
    dt.Columns.Add("emp_SSN");
    dt.Columns.Add("emp_salary");
    dt.Columns.Add("dpt_name");
    dt.Columns.Add("emp_phone");
    dt.Columns.Add("emp_jobTitle");

    foreach(GridViewRow row in grdvEmployees.Rows)
    {
        DataRow dr = dt.NewRow();
        dr[0] = row.Cells[0].Text;
        dr[1] = row.Cells[1].Text;
        dr[2] = row.Cells[2].Text;
        dr[3] = row.Cells[3].Text;
        dr[4] = row.Cells[4].Text;
        dr[5] = row.Cells[5].Text;
        dr[6] = row.Cells[6].Text;
        dr[7] = row.Cells[7].Text;
        dt.Rows.Add(dr);
    }

    grdvTemp = grdvEmployees;
    grdvTemp.DataSource = null;
    grdvTemp.DataBind();
    grdvTemp.Columns.RemoveAt(8);
    grdvTemp.DataSource = dt;
    grdvTemp.DataBind();

    Response.Clear();
    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Charset = "";
    string FileName = "Vithal" + DateTime.Now + ".xls";
    StringWriter strwritter = new StringWriter();
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);

    grdvTemp.HeaderRow.Style.Add("background-color", "#FFFFFF");
    foreach (TableCell tableCell in grdvTemp.HeaderRow.Cells)
        tableCell.Style["background-color"] = "#1C5E55";

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
    grdvTemp.GridLines = GridLines.Both;
    grdvTemp.HeaderStyle.Font.Bold = true;
    grdvTemp.RenderControl(htmltextwrtter);
    Response.Write(strwritter.ToString());
    Response.End();
}
Posted
Updated 29-Jun-21 1:23am
Comments
[no name] 25-Jun-21 14:25pm    
I think you should convert / export the grid to Excel; then do an Excel to Cr to Excel.

(csv may work too here and there)

1 solution

What you refer to as the "traditional" way is what everyone else calls the "wrong" way. :)

You are generating an HTML table, but lying to the browser and telling it you've sent an Excel file. The browser passes the response to Excel, which does its best to convert that HTML into a read Excel file, but it won't be perfect, and it will display a "wrong file type" warning.

Rather than trying to create and export a Crystal report to mimic your grid, use something like ClosedXML[^] to generate a real Excel file, and send that instead.
 
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