在HTML页面中就一个按钮
//////<asp:button id=Button runat=server
Text=导出>
///</asp:button>
和 一个DataGrid
///<asp:datagrid id=DataGrid runat=server>
///</asp:datagrid>
aspxcs 文件的代码是:
////public class WebForm : SystemWebUIPage
{
protected SystemWebUIWebControlsDataGrid DataGrid;
protected SystemWebUIWebControlsButton Button;
private void Page_Load(object sender SystemEventArgs e)
{
DataGridDataSource=CreateDataSource();
DataGridDataBind();
// 在此处放置用户代码以初始化页面
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dtColumnsAdd(new DataColumn(身份证号码 typeof(string)));
dtColumnsAdd(new DataColumn(图书单价typeof(decimal)));
dtColumnsAdd(new DataColumn(购买数量typeof(Int)));
dtColumnsAdd(new DataColumn(总价格typeof(decimal)));
for (int i = ; i < ; i++)
{
dr = dtNewRow();
dr[] = ;
dr[] = * i /;
dr[] = i + ;
dr[] = (decimal)dr[] * (Int)dr[];
dtRowsAdd(dr);
}
DataView dv = new DataView(dt);
return dv;
}
private void Button_Click(object sender SystemEventArgs e)
{
ResponseClear();
ResponseBuffer= true;
ResponseCharset=GB;
ResponseAppendHeader(ContentDispositionattachment;filename=FileNamexls);
ResponseContentEncoding=SystemTextEncodingGetEncoding(GB); //设置输出流为简体中文
ResponseContentType = application/msexcel; //设置输出文件类型为excel文件
thisEnableViewState = false;
SystemGlobalizationCultureInfo myCItrad = new SystemGlobalizationCultureInfo(ZHCNtrue);
SystemIOStringWriter oStringWriter = new SystemIOStringWriter(myCItrad);
SystemWebUIHtmlTextWriter oHtmlTextWriter = new SystemWebUIHtmlTextWriter(oStringWriter);
thisDataGridRenderControl(oHtmlTextWriter); // 在这里设置保存那个数据源的数据
ResponseWrite(oStringWriterToString());
ResponseEnd();
}
private void DataGrid_ItemDataBound(object sender SystemWebUIWebControlsDataGridItemEventArgs e) //这个设置显示的格式
{
if(eItemItemType == ListItemTypeItem || eItemItemType == ListItemTypeAlternatingItem)
{
eItemCells[]AttributesAdd(stylevndmsexcelnumberformat:@);
eItemCells[]AttributesAdd(stylevndmsexcelnumberformat:¥####);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASPNET Web 窗体设计器所必需的
//
InitializeComponent();
baseOnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 不要使用代码编辑器修改
/// 此方法的内容
/// </summary>
private void InitializeComponent()
{
thisButtonClick += new SystemEventHandler(thisButton_Click);
thisDataGridItemDataBound += new SystemWebUIWebControlsDataGridItemEventHandler(thisDataGrid_ItemDataBound);
thisLoad += new SystemEventHandler(thisPage_Load);
}
#endregion
}