Click here to Skip to main content
14,212,087 members
Rate this:
Please Sign up or sign in to vote.
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
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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 !");
}
   

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 18 Nov 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100