返回首页

您好,
我想一个gridview数据与图像导出到一个Excel文件。

当我执行下面的代码,GridView数据导出到Excel
正确。在Excel中显示,但没有图像。只有一个空白图像图标
里面的Excel表演。
任何机构指出什么是错的,在我的代码或缺少什么可以

谢谢,

-------------------------代码开始----------------------- -----------------------


protected void Button1_Click(object sender, System.EventArgs e)

{

	ExportGridToExcel(GridView1, "mydata.xls");

}

public void ExportGridToExcel(GridView grdGridView, string fileName)

{

	Response.Clear();

	Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));

	Response.Charset = "";

	Response.ContentType = "application/vnd.xls";

 

	StringWriter stringWrite = new StringWriter();

	HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

	grdGridView.RenderControl(htmlWrite);

	Response.Write(stringWrite.ToString());

	Response.End();

}

 



 

public override void VerifyRenderingInServerForm(Control control)

{

}
-----------------------------------

我这样
网格中显示数据
-----------------------------------

{C} [编辑]代码被包裹在"前"的标签[/编辑]

回答

评论会员:游客 时间:2012/02/06
转换为bmp格式的图像数据
:您需要提供的绝对URL到Excel
有一个看] - 这应该给你一个想法,你需要做什么
评论会员:纳雷什bahekar 时间:2012/02/06
/ * --------------------- ------------------------- * /
/ /作者:Mudassar汗
/ /文章:Word,Excel和PDF格式的图像的GridView导出
/ /网址:http://www.aspsnippets.com
/ * ---------------------------------------------- * /
使用系统;
使用System.Data;
使用System.Data.SqlClient的
使用System.Configuration;
使用的System.Web;
使用System.Web.Security;
CSDN社区 -
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.IO;
使用iTextSharp.text
使用iTextSharp.text.pdf
使用iTextSharp.text.html
使用iTextSharp.text.html.simpleparser

公共部分类_Default:System.Web.UI.Page的
{
保护无效的Page_Load(对象发件人,EventArgs的五)
{
 0;的DataTable DT =新的DataTable();
字符串strConnString System.Configuration.ConfigurationManager.ConnectionStrings"conString"的ConnectionString
字符串strQuery ="SELECT * tblFiles为了通过ID";
SqlCommand的CMD =新的SqlCommand(strQuery)
SqlConnection的CON =新的SqlConnection(strConnString)
SqlDataAdapter的SDA =新的SqlDataAdapter()
 0;cmd.CommandType = CommandType.Text;
cmd.Connection = CON;
尝试
  ; {
con.Open()
sda.SelectCommand = CMD;
sda.Fill(DT)
 60; GridView1.DataSource = DT;
  ; GridView1.DataBind()
 0; }
赶上(例外EX)
{
 60; 回复于(ex.Message);
}
最后
{
con.Close()
sda.Dispose()
&# 160; con.Dispose()
}
}
保护的无效Export_Grid(对象发件人,EventArgs的五)
{
按钮BTN =(按钮)寄件人;
开关(btn.CommandArgument)
{
& #160; "字":
&# 160; Word_Export()
 0; 打破;
情况下的"Excel":
Excel_Export()
打破;
"PDF格式":
PDF_Export()
打破;
}
}
公众覆盖无效VerifyRenderingInServerForm(控制控制)
&# 160; {
/ *验证控件呈现* /
}
私人的无效Excel_Export()
{
Response.Clear();
&# 160; 将Response.Buffer = TRUE;
&# 160; Response.AddHeader("内容处理",
&# 160; "附件;名= GridViewExport.xls"的);
Response.Charset ="";
 60; ="应用程序/ vnd.ms-Excel的"
StringWriter将SW =新的StringWriter()
HtmlTextWriter的HW =新的HtmlTextWriter(SW);
GridView1.AllowPaging = FALSE;
GridView1.DataBind()
  ; 为(int i = 0;我LT; GridView1.Rows.Count;我)
{
 60;GridViewRow的行= GridView1.Rows [I]
 0; / /应用到每一行文字样式
row.Attributes.Add("类","文本模式");
}
GridView1.RenderControl(HW)

/ /样式格式化数字字符串
字符串风格= @"<风格>文本模式。{MSO数格式:\ @;} LT ;/风格>";
回复于(风格);
 0; Response.Output.Write(sw.ToString())
Response.Flush()
Response.End();
}
protected string GetUrl(string imagepath)

   {

       string[] splits = Request.Url.AbsoluteUri.Split('/');

       if (splits.Length >= 2)

       {

           string url = splits[0] + "//";

           for (int i = 2; i < splits.Length - 1; i++)

           {

               url += splits[i];

               url += "/";

           }

           return url + imagepath;

       }

       return imagepath;

   }
的。|纳雷什bahekar。:使用系统;
使用System.Data;
使用System.Data.SqlClient的
使用System.Configuration;
使用的System.Web;
使用System.Web.Security;
CSDN社区 -
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.IO;
使用iTextSharp.text
使用iTextSharp.text.pdf
使用iTextSharp.text.html
使用iTextSharp.text.html.simpleparser

公共部分类_Default:System.Web.UI.Page的
{
保护无效的Page_Load(对象发件人,EventArgs的五)
{
&# 160; 的DataTable DT =新的DataTable();
&# 160; 字符串strConnString System.Configuration.ConfigurationManager.ConnectionStrings"conString"的ConnectionString
字符串strQuery ="SELECT * tblFiles为了通过ID";
SqlCommand的CMD =新的SqlCommand(strQuery)
SqlConnection的CON =新的SqlConnection(strConnString)
&# 160; SqlDataAdapter的SDA =新的SqlDataAdapter()
&# 160; cmd.CommandType = CommandType.Text;
 0; cmd.Connection = CON;
 0; 尝试
{
&# 160; con.Open()
sda.SelectCommand = CMD;
sda.Fill(DT)
GridView1.DataSource = DT;
GridView1.DataBind()
}
赶上(例外EX)
{
& #160; 回复于(ex.Message);
& #160; }
最后
{
con.Close()
sda.Dispose()
con.Dispose()
&# 160; }
}
保护的无效Export_Grid(对象发件人,EventArgs的五)
{
  ; 按钮BTN =(按钮)寄件人;
开关(btn.CommandArgument)
{
  ; "字":
Word_Export()
&# 160; 打破;
 60; 情况下的"Excel":
 0; Excel_Export()
 0; 打破;
  ; "PDF格式":
&# 160; PDF_Export()
打破;
}
}
公众覆盖无效VerifyRenderingInServerForm(控制控制)
{
/ *验证控件呈现* /
}
私人的无效Excel_Export()
{
Response.Clear();
将Response.Buffer = TRUE;
Response.AddHeader("内容处理",
"附件;名= GridViewExport.xls"的);
Response.Charset ="";
="应用程序/ vnd.ms-Excel的"
StringWriter将SW =新的StringWriter()
HtmlTextWriter的HW =新的HtmlTextWriter(SW);
GridView1.AllowPaging = FALSE;
GridView1.DataBind()
为(int i = 0;我LT; GridView1.Rows.Count;我)
{
& #160; GridViewRow的行= GridView1.Rows [I]
&# 160; / /应用到每一行文字样式
 0; row.Attributes.Add("类","文本模式");
}
& #160; GridView1.RenderControl(HW)

/ /样式格式化数字字符串
 60; 字符串风格= @"<风格>文本模式。{MSO数格式:\ @;} LT ;/风格>";
回复于(风格);
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End();
}
& #160;
protected string GetUrl(string imagepath)

   {

       string[] splits = Request.Url.AbsoluteUri.Split('/');

       if (splits.Length >= 2)

       {

           string url = splits[0] + "//";

           for (int i = 2; i < splits.Length - 1; i++)

           {

               url += splits[i];

               url += "/";

           }

           return url + imagepath;

       }

       return imagepath;

   }