问题描述
我有code出口到Excel。在我的GridView我设置分页显示的记录PAGECOUNT的数量。
I have code for exporting to Excel. In my gridview I set paging to display the number of records in pagecount.
但在我导出到Excel它不给我整记录,取而代之的则是显示我在同一寻呼与六个记录。
But in my export to Excel it is not giving me entire records, instead it is showing me the same paging with six records.
我的code:
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
GrdDynamicControls.AllowPaging = false;
GrdDynamicControls.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GrdDynamicControls);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
我如何更改或禁用分页从我的GridView得到所有的记录?
How do I change or disable the paging to get all the records from my gridview?
我在GridView中为+9199等数字,但它显示了我它的9.99的格式导出后,等等。
I have numbers in gridview as +9199, etc., but after exporting it it is showing me it in the format of 9.99, etc.
我如何通过在号码formatcells从这里?
How do I pass the formatcells in numbers from here?
推荐答案
您可以导出GridView的数据源(例如,数据集)到Excel中。
You can export a gridview's datasource (for example, dataset) to Excel.
下面是示例code这样做。你可以参考的 的了解更多信息。
Here is sample code for doing this. You can refer to Howto: Export a dataset to Excel (C# / ASP.NET) for more information.
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Whatever
{
///
/// This class provides a method to write a dataset to the HttpResponse as
/// an Excel file.
///
public class ExcelExport
{
public static void ExportDataSetToExcel(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// First let's clean up the response.object.
response.Clear();
response.Charset = "";
// Set the response mime type for Excel.
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// Create a string writer.
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
}
}
这篇关于导出到Excel从GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!