资源:excelService 处事
摆列问题
导出时,,数字和字符的摆列格式默认不一样,数字靠右,字符靠左,想让摆列一致,有两个步伐:
1 在绑按时,就做固定格式
2 导出时,将数字那一列转换为文本格式,例如以下
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //让数字以文本形式暗示 e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } }转换函数
/// <summary> /// 导出button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExport_Click(object sender, System.Web.UI.ImageClickEventArgs e) { //导出文件名称 string strFileName = "报表.xls"; //取原始数据。并绑定 this.dagInfo.AllowPaging = false; this.dagInfo.AllowSorting = false; DataTable dt = BingData(); this.dagInfo.DataSource = dt; this.dagInfo.DataBind(); //使用本页面直接输出 WHPT_WebApp.Statistics.GridStyleReport.ToExcel(this.dagInfo , strFileName, delegate(System.Web.UI.HtmlTextWriter writer) { writer.Write(string.Format("<table width=100%><tr><td align=center colspan=5>{0}</td></tr></table>", "报表")); } , null); //恢复分页数据 this.dagInfo.AllowPaging = true; this.dagInfo.AllowSorting = true; BindInfo(1, ""); return; } #endregion
PS:此文仅做记录交流所用。不做他用