gpt4 book ai didi

c# - 导出 Gridview 以处理图像

转载 作者:行者123 更新时间:2023-11-30 21:50:53 25 4
gpt4 key购买 nike

在我的网页中,我有一个带有数据和一些图像的 gridview,在这里我想将 gridview 导出到带有图像的 excel,我正在尝试使用下面的代码,它仅导出数据,在这里我如何导出到 excel图片 ?

 Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.HeaderRow.Style.Add("background-color", "Red");
for (int i = 0; i < GridView1.Columns.Count - 2; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "Red");
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

这里我使用Openoffice calc读取excel文件

最佳答案

用这个替换函数代码

private void Excel_Export()
{
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);
GridView1.AllowPaging = false;
GridView1.DataBind();

for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];

//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}

GridView1.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();
}

引用这个link了解更多详情

关于c# - 导出 Gridview 以处理图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com