将选定的gridview行导出到Excel,并在ASP.Net中附加文档和图像 [英] Export selected rows of gridview to Excel with attached documents and images in ASP.Net

查看:43
本文介绍了将选定的gridview行导出到Excel,并在ASP.Net中附加文档和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i希望根据选定的行从网格视图生成或导出到excel我引用了很多链接但我的问题是在我的网格视图中包含附件的附件列,如word,excel,pdf等附件和其他包含图像的列,所以我想要当我导出到excel包含所有附加的文件和导出的Excel工作表中的图像这可能吗?如果可能的话,请指导我并给出任何解决方案,如果你有任何解决方案。



i参考许多链接,其中能够将所选行导出为ex​​cel但不包含和附加文件和图片。



谢谢。



i want to generate or export to excel from grid view based on selected row i refer many links for that but my problem is that in my grid view column for attachments which contain attachments like word,excel,pdf etc attachments and other column which contain images so i want that when i do export to excel contain all attached documents and images in exported excel sheet is this possible?? and if possible guide me for that and give any solution if u have any.

i refer many link in which able to export selected row to excel but that not contain and attached documents and images in it.

Thank You.

推荐答案

看看这段代码。它可能对你有帮助。



私有DataTable SourceForGrid

{

get {return(DataTable)Session [ SourceForGrid]; }

set {Session [SourceForGrid] = value; }



}



protected void btnExport_Click(object sender,EventArgs e)

{

foreach(gvSource.Rows中的GridViewRow gvr)

{

CheckBox chk =(CheckBox)gvr.FindControl(chkBxExport );

if(chk.Checked)

{

id + = gvSource.DataKeys [gvr.RowIndex] .Value.ToString() +,;

}

id = id.TrimEnd(',');

}

if(id.Length!= 0)

{

var query = from result in((DataTable)Session [SourceForGrid])。AsEnumerable()

其中id.Contains(Convert.ToString(result.Field< int64>(ads_src_id)))

选择结果;



GenerateExcel(query.CopyToDataTable());

}

else

{

Response.Write(alert('请选择导出来源'););

}

}





public static void GenerateExcel(DataTable dtSource)

{

StringBuilder sbDocBody = new StringBuilder (); ;

试试

{

//宣布样式

sbDocBody.Append();

sbDocBody.Append(。Header {background-color:Navy; color:#ffffff; font-weight:bold; font-family:Verdana; font-size:12px;});

sbDocBody.Append(。SectionHeader {background-color:#8080aa; color:#ffffff; font-family:Verdana; font-size:12px; font-weight:bold;});

sbDocBody.Append(。内容{background-color:#ccccff;颜色:#000000; font-family:Verdana; font-size:12px; text-align:left});

sbDocBody.Append(。Label {background-color:#ccccee; color:#000000; font-family:Verdana; font-size:12px; text-align:right;});

sbDocBody.Append();

//

StringBuilder sbContent = new StringBuilder(); ;

sbDocBody.Append(
);

sbDocBody.Append(
);

sbDocBody.Append();

//

if(dtSource.Rows.Count> 0)

{

sbDocBody.Append(
);

sbDocBody.Append();

sbDocBody.Append();

sbDocBody.Append();

for(int i = 0; i< dtSource.Columns.Count; i ++)

{

sbDocBody.Append();

}

sbDocBody.Append();

//

//添加数据行

for(int i = 0; i< dtSource.Rows.Count; i ++)

{

sbDocBody.Append();

sbDocBody.Append();

for(int j = 0; j< dtSource.Columns.Count; j ++)

{

sbDocBody.Append();

}

sbDocBody.Append( );

}

sbDocBody.Append(
);

//

//添加列标题

sbDocBody.Append(
+ dtSource。 Columns [i] .ToString()。Replace(。,
)+
+ dtSource.Rows [i] [j] .ToString()+
);

sbDocBody.Append (
);

sbDocBody.Append(
);

}

//

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Buffer = true;

//

HttpContext.Current.Response.AppendHeader(Content-Type,application / ms-excel) ;

HttpContext.Current.Response.AppendHeader(Content-disposition,附件; filename = Ads_Src.xls);

HttpContext.Current.Response.Write(sbDocBody.ToString());

HttpContext.Current.Response.End(); < br $>
}

catch(exception ex)

{

//忽略此错误,因为这是由于终止造成的响应流。

}

}
Look at this code. It might be helpful for you.

private DataTable SourceForGrid
{
get { return (DataTable)Session["SourceForGrid"]; }
set { Session["SourceForGrid"] = value; }

}

protected void btnExport_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvSource.Rows)
{
CheckBox chk = (CheckBox)gvr.FindControl("chkBxExport");
if (chk.Checked)
{
id += gvSource.DataKeys[gvr.RowIndex].Value.ToString() + ",";
}
id = id.TrimEnd(',');
}
if (id.Length != 0)
{
var query = from result in ((DataTable)Session["SourceForGrid"]).AsEnumerable()
where id.Contains(Convert.ToString(result.Field<int64>("ads_src_id")))
select result;

GenerateExcel(query.CopyToDataTable());
}
else
{
Response.Write("alert('Please select source for export');");
}
}


public static void GenerateExcel(DataTable dtSource)
{
StringBuilder sbDocBody = new StringBuilder(); ;
try
{
// Declare Styles
sbDocBody.Append("");
sbDocBody.Append(".Header { background-color:Navy; color:#ffffff; font-weight:bold;font-family:Verdana; font-size:12px;}");
sbDocBody.Append(".SectionHeader { background-color:#8080aa; color:#ffffff; font-family:Verdana; font-size:12px;font-weight:bold;}");
sbDocBody.Append(".Content { background-color:#ccccff; color:#000000; font-family:Verdana; font-size:12px;text-align:left}");
sbDocBody.Append(".Label { background-color:#ccccee; color:#000000; font-family:Verdana; font-size:12px; text-align:right;}");
sbDocBody.Append("");
//
StringBuilder sbContent = new StringBuilder(); ;
sbDocBody.Append("
");
sbDocBody.Append("
");
sbDocBody.Append("");
//
if (dtSource.Rows.Count > 0)
{
sbDocBody.Append("
");
sbDocBody.Append("");
sbDocBody.Append("");
sbDocBody.Append("");
for (int i = 0; i < dtSource.Columns.Count; i++)
{
sbDocBody.Append("");
}
sbDocBody.Append("");
//
// Add Data Rows
for (int i = 0; i < dtSource.Rows.Count; i++)
{
sbDocBody.Append("");
sbDocBody.Append("");
for (int j = 0; j < dtSource.Columns.Count; j++)
{
sbDocBody.Append("");
}
sbDocBody.Append("");
}
sbDocBody.Append("
");
//
// Add Column Headers
sbDocBody.Append("
" + dtSource.Columns[i].ToString().Replace(".", "
") + "
" + dtSource.Rows[i][j].ToString() + "
");
sbDocBody.Append("
");
sbDocBody.Append("
");
}
//
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
//
HttpContext.Current.Response.AppendHeader("Content-Type", "application/ms-excel");
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=Ads_Src.xls");
HttpContext.Current.Response.Write(sbDocBody.ToString());
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
// Ignore this error as this is caused due to termination of the Response Stream.
}
}


这篇关于将选定的gridview行导出到Excel,并在ASP.Net中附加文档和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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