【发布时间】:2017-04-30 21:59:27
【问题描述】:
我正在使用以下代码将带有图像的数据导出到Excel。
代码
protected void ExportToExcel(对象发送者,EventArgs e) { //从数据库中获取数据到数据表中 string strQuery = "select CustomerID, ContactName, City, PostalCode, display_picture" + “来自客户”; SqlCommand cmd = 新的 SqlCommand(strQuery); 数据表 dt = GetData(cmd); //创建一个虚拟的GridView GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); 响应。清除(); 响应缓冲区=真; Response.AddHeader("内容配置", “附件;文件名=DataTable.xls”); Response.Charset = ""; Response.ContentType = "应用程序/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); for (int i = 0; i .textmode { mso-number-format:\@; } "; Response.Write(风格); Response.Output.Write(sw.ToString()); Response.Flush(); 响应。结束(); }Excel 正在正确下载。但问题是当我过滤Excel 中的数据时。 Excel 中的图像位于 Move but don't size with cells 属性中。如何用Move and size with cells属性制作图片?
【问题讨论】:
-
这不是 Excel,它只是一个 HTML 表格,其中包含 Excel 可能 能够使用默认设置导入的虚假内容类型。使用像 EPPlus 这样的库来创建真正的 Excel 文件。这要容易得多
-
@PanagiotisKanavos:谢谢,我会检查的。是否可以使用EPPlus 将图像添加为文本
-
EPplus 生成真实的 Excel 文件,所以是的,可以添加图片、数据透视表、连接等。SO 中有很多问题说明了如何执行此操作,包括有关定位、样式等的问题