将页面导出到带有徽标图像的 Excel

发布于 2024-10-17 20:46:49 字数 1571 浏览 4 评论 0原文

我必须将一些数据从 asp.net 页面导出到 Excel,因此我基本上使用一个表来创建我需要的自定义标头,然后创建一个 GridView。导出到 Excel 时,所有数据都正确显示,但是当我将徽标图像添加到 html 时,导出时它不会显示在 Excel 文件上。

由于我需要将其导出到 Excel 2007 或更高版本,我知道我可以使用 Microsoft 的 Open XML 内容来导出数据,问题是我已经完成了代码上的所有操作,我想知道是否还有其他方法来做到这一点,而不是使用 Open XML 重新做一遍。

如果不使用 Open XML 就无法做到这一点,谁能告诉我如何将数据导出到其中?我尝试过一次,但没有取得多大成功。 =/

顺便说一句,我正在使用 C#。

提前致谢!

我已经更新了代码,现在看起来像这样,但我仍然看不到图像...=/

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    frmPlanoAcao.RenderControl(htmlWrite);

    StringWriter w = new StringWriter();
    HtmlTextWriter t = new HtmlTextWriter(w);
    imgLogo.RenderControl(t);
    var byte_array = System.Text.Encoding.ASCII.GetBytes(w.ToString());
    Response.Write(stringWrite.ToString());

    this.EnableViewState = false;

    Response.Clear();
    Response.Buffer = false;
    Response.Charset = "ISO-8859-1";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "plano_acao.xls"));
    Response.ContentType = "application/vnd.ms-excel";
    Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    Response.Write(getExcelStyling());
    Response.OutputStream.Write(byte_array, 0, byte_array.Length);
    Response.Write(stringWrite.ToString());
    Response.Write("</body>");
    Response.Write("</html>");
    Response.End();

I have to export some data from an asp.net page to excel, so I use basically a table to create the custom headers I need then a GridView. All the data displays correctly when exported to excel, but when I added an Logo image to the html, it doesn't show up on the Excel file when exported.

Since I need to export it to Excel 2007 or later, I know I can use the Open XML stuff from Microsoft to export the data, the problem is that I already have everything done on the code and I wanted to know if there is another way to do that instead of doing all over again using Open XML.

If there isn't a way to do that without using Open XML, can anyone show me how I could export the data to it? I tried once but I didn't have much success. =/

BTW, I'm using C#.

Thanks in advance!

I've updated the code and now it looks like this, but I still can't see the image... =/

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    frmPlanoAcao.RenderControl(htmlWrite);

    StringWriter w = new StringWriter();
    HtmlTextWriter t = new HtmlTextWriter(w);
    imgLogo.RenderControl(t);
    var byte_array = System.Text.Encoding.ASCII.GetBytes(w.ToString());
    Response.Write(stringWrite.ToString());

    this.EnableViewState = false;

    Response.Clear();
    Response.Buffer = false;
    Response.Charset = "ISO-8859-1";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "plano_acao.xls"));
    Response.ContentType = "application/vnd.ms-excel";
    Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    Response.Write(getExcelStyling());
    Response.OutputStream.Write(byte_array, 0, byte_array.Length);
    Response.Write(stringWrite.ToString());
    Response.Write("</body>");
    Response.Write("</html>");
    Response.End();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑叹一世浮沉 2024-10-24 20:46:49

尝试以下代码。我已经在本地 IIS 上进行了测试,它工作正常。
在网格数据顶部包含诸如 Header Image/Logo 之类的图像。

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

您可以根据您的要求自定义图像的高度和宽度。 标记需要相同的高度和宽度设置。

Try the following code. I have tested at local IIS and it is working properly.
Including the image like Header Image/Logo on top of the grid data.

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

you can customize your image's height and width as per your requirement. Same height and width setting will be required for the <TD> tag.

天赋异禀 2024-10-24 20:46:49

您必须在 StringWriter 中获取图像并在字节数组中使用 System.Text.Encoding.ASCII.GetBytes(stringwriter string) 。
然后将它们写为outputStream.Write(byte_array, 0, byte_array.Length);
其中输出流是 HttpContext 响应输出流。

如果你说,将网格、图像等所有内容一次性转换为 Excel,我还没有尝试过。但是,我可以说,您可以通过它们的方式将它们分别放入 Excel 中。

You have to get images in StringWriter and use System.Text.Encoding.ASCII.GetBytes(stringwriter string) in array of bytes.
then write these as outputStream.Write(byte_array, 0, byte_array.Length);
where outputstream is HttpContext Response outputstream.

If you say, to convert Grid, Images, everything in one go to Excel, i have not tried. But, i can say, you can individually have both of them into Excel by their way.

许仙没带伞 2024-10-24 20:46:49

如果您使用 C# 并想导出到 Excel,我会推荐 EPPLUS

http://epplus.codeplex.com/

将为您现在和将来省去很多麻烦。

If you are using C# and want to export to Excel I would recommend EPPLUS

http://epplus.codeplex.com/

Will save you a whole lot of troubles now and in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文