Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello iam trying to fix this issues from 3 days...how to export webgrid in excel using mvc 5.
i have tried but only image path is showing in .. any image is not showing....just i want to show image in excel.

What I have tried:

Controller
C#
public void ExportClientsListToExcel(string Mode)
        {

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
             "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            var grid = new System.Web.UI.WebControls.GridView();
            grid.DataSource = _StaffRepository.GetAllStaffListForExport(Mode);
            grid.DataBind();


           
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                GridViewRow row = grid.Rows[i];
                //Apply text style to each Row
                row.Attributes.Add("class", "textmode");
            }
            grid.RenderControl(hw);

            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

}
Posted
Updated 17-Nov-16 20:00pm
Comments
Dave Kreskowiak 16-Nov-16 8:46am    
Ummm...I hate to tell you this but you're not even close to creating an Excel file. You're setting a content type of Excel, but the content you're writing is HTML, NOT and Excel file.

With the code you have, you can't show an image. I suggest you Google for "OpenXML SDK" and use that for creating an Excel file. Warning! The learning curve for the SDK is kind of steep.

1 solution

Here is a snippet to export image. Please use Microsoft Excel Liberary:

C#
protected void btnExportExcelWithImage_Click(object sender, EventArgs e)
{
    var xlApp = new Excel.Application();
    Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
    Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];

    xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
    xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";

    xlWorkSheet.Shapes.AddPicture(@"C:\Users\PGoel\Desktop\pp.jpg", MsoTriState.msoFalse, MsoTriState.msoCTrue, 50, 50, 300, 45);

    xlWorkBook.SaveAs(@"C:\Users\PGoel\Desktop\csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal);
    xlWorkBook.Close(true);
    xlApp.Quit();

    Marshal.ReleaseComObject(xlApp);

    //MessageBox.Show("File created !");
}
 
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