Iam在gridview中与Export to excel相混淆 [英] Iam confused with Export to excel in gridview
问题描述
我有问题。
我很惊讶在gridview中导出到excel。我的所有网格视图设计
结构都在客户端。所以使用javascript我得到一个下载的文件
格式作为导出到excel没有.xls扩展名的结果。当我试图在控制器中编写代码时,我得到一个带有空数据的.xls文件。
因为我不知道如何获得clint side webgrid值到服务器端
控制器,所以我只是重新使用该方法来获取
服务器端的网格数据,我试图返回
文件( stream,application / vnd.openxmlformats-officedocument.spreadsheetml.sheet)。
但是没有得到最好的结果。我很期待你的帮助。
谢谢和问候
我知道有两个人愿意出口到xls
首先:循环到DataTable并创建manauly然后将其写入响应。
< pre lang = cs> Response.ContentType = application / vnd.ms-excel;
Response.AddHeader( content-disposition, attachment; filename = MyFiles.xls);
秒:使用出口商链接Devexpress ASPXGridExporter。
< pre lang =c#>
protected void imbPersonsXLS_Click( object sender,ImageClickEventArgs e)
{
GridExporterPersons.WriteXlsToResponse();
}
第二种方式非常简单,并且devexpress aspxgridview过滤器也支持。
< blockquote>尝试 CodeProject常见问题系列1: ASP.NET GridView [ ^ ]。
嗨。 Abhinav S在这里非常轻松和清晰的链接你需要的东西非常简单和容易......
< pre lang = cs> protected void Button1_Click( object sender,EventArgs e)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader( content-disposition,
string .Format( attachment; filename = {0} .xls, 已导出));
HttpContext.Current.Response.Charset = ;
HttpContext.Current.Response.ContentType = application / ms-excel;
使用(StringWriter sw = new StringWriter())
{
使用(HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// 创建一个包含网格的表
表table = new Table();
foreach (GridViewRow row in GridView1.Rows)
{
table.Rows.Add(row);
}
// 将表格渲染为htmlwriter
table.RenderControl(htw);
// 将htmlwriter渲染到响应中
HttpContext。 Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
使用此简单功能并避免执行复杂编码美容是在Simplicity。
I have Question for you.
Iam confused with Export to excel in gridview. my all grid view design
structures are in client side. so using javascript I got a downloaded File
format as the result of export to excel without .xls extension. When i
trying to write code in controller, i got a .xls file with empty data.
since i don't know how to get clint side webgrid value to the server side
controller, so thet i just reuse the method again for getting grid data in
server side where i tryed to return
file(stream,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").
But did't get a best result. I kindly expect your help.
Thanks and Regards
Hi i know two waies to export to xls
first : loop to DataTable and create manauly then write it to response.
<pre lang="cs">Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
second : use exporters link Devexpress ASPXGridExporter.
<pre lang="c#">protected void imbPersonsXLS_Click(object sender, ImageClickEventArgs e) { GridExporterPersons.WriteXlsToResponse(); }
the second way is very easy and devexpress aspxgridview filter also suported.
Try CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^].
Hi. the link that Abhinav S was very easey and clear here the thing which you need is very simple and easy...
<pre lang="cs">protected void Button1_Click(object sender, EventArgs e) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "Exported")); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/ms-excel"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // Create a table to contain the grid Table table = new Table(); foreach (GridViewRow row in GridView1.Rows) { table.Rows.Add(row); } // render the table into the htmlwriter table.RenderControl(htw); // render the htmlwriter into the response HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } }
Use this Simple Functionality and Avoid doing Complex Coding the beauty is in Simplicity.
这篇关于Iam在gridview中与Export to excel相混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!