如何使用MVC 5在excel中显示图像 [英] How to show image in excel using MVC 5

查看:66
本文介绍了如何使用MVC 5在excel中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好iam试图解决这个问题3天...如何使用mvc 5在excel中导出webgrid

i已尝试但只显示图像路径..任何图像都没有显示....只是我想在excel中显示图像。



我尝试过:

< br $>
控制器

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();



}


}

推荐答案

这是导出图片的片段。请使用Microsoft Excel Liberary:



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


这篇关于如何使用MVC 5在excel中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆