在页面显示正常 一导出 EXEL
从
tcHeader[14].Style.Add("text-align", "center");
tcHeader[14].Text = "签约日期"; 这里 就格式乱了 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//第1行表头--------------------------------------------
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Style.Add("text-align", "center");
tcHeader[0].Style.Add("font-size", "18px");
tcHeader[0].HorizontalAlign = HorizontalAlign.Center;
tcHeader[0].Attributes.Add("colspan", "15"); //跨Row
tcHeader[0].Text = "销售明细统计表</th></tr><tr>";
//第2行表头------------------------------------------
//前1列跨3行
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Style.Add("text-align", "center");
tcHeader[1].RowSpan = 3; //跨Row
tcHeader[1].ColumnSpan = 2;
tcHeader[1].Text = "销售动态统计";
//后几列
//第1行
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Style.Add("text-align", "center");
tcHeader[2].ColumnSpan = 2;
tcHeader[2].Text = "可售房统计";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Style.Add("text-align", "center");
tcHeader[3].ColumnSpan = 7;
tcHeader[3].Text = "本期销售情况";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Style.Add("text-align", "center");
tcHeader[4].ColumnSpan = 4;
tcHeader[4].Text = "累计销售房情况</th></tr><tr>";
//第2行
DataRow row = ds.Tables[1].Rows[0];//统计数据行
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Style.Add("text-align", "left");
tcHeader[5].ColumnSpan = 2;
tcHeader[5].Text = "可售套数:" + row[0].ToString() + "套";
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Style.Add("text-align", "left");
tcHeader[6].ColumnSpan = 7;
tcHeader[6].Text = "签约套数:" + row[2].ToString() + "套 比例:" + row[3].ToString() + "% 认购套数:" + row[6].ToString() + "套 比例:" + row[7].ToString() + "%"; tcHeader.Add(new TableHeaderCell());
tcHeader[7].Style.Add("text-align", "center");
tcHeader[7].ColumnSpan = 4;
tcHeader[7].Text = "签约套数: " + row[10].ToString() + "套 比例:" + row[11].ToString() + "%</th></tr><tr>";
//第3行
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Style.Add("text-align", "left");
tcHeader[8].ColumnSpan = 2;
tcHeader[8].Text = "可售面积: " + row[1].ToString() + "㎡";
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Style.Add("text-align", "left");
tcHeader[9].ColumnSpan = 7;
tcHeader[9].Text = "签约面积:" + row[4].ToString() + "㎡ 比例:" + row[5].ToString() + "% 认购面积" + row[8].ToString() + "㎡ 比例:" + row[9].ToString() + "%";
tcHeader.Add(new TableHeaderCell());
tcHeader[10].Style.Add("text-align", "center");
tcHeader[10].ColumnSpan = 4;
tcHeader[10].Text = "签约面积: " + row[12].ToString() + "㎡ 比例:" + row[13].ToString() + "%</th></tr><tr>"; //第3行表头--------------------------------------------------
//前9列跨2行
tcHeader.Add(new TableHeaderCell());
tcHeader[11].RowSpan = 2;
tcHeader[11].Style.Add("text-align", "center");
tcHeader[11].Text = "序号";
tcHeader.Add(new TableHeaderCell());
tcHeader[12].RowSpan = 2;
tcHeader[12].Style.Add("text-align", "center");
tcHeader[12].Text = "房间号";
tcHeader.Add(new TableHeaderCell());
tcHeader[13].RowSpan = 2;
tcHeader[13].Style.Add("text-align", "center");
tcHeader[13].Text = "客户姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[14].RowSpan = 2;
tcHeader[14].Style.Add("text-align", "center");
tcHeader[14].Text = "签约日期";
tcHeader.Add(new TableHeaderCell());
tcHeader[15].RowSpan = 2;
tcHeader[15].Style.Add("text-align", "center");
tcHeader[15].Text = "销售面积㎡";
tcHeader.Add(new TableHeaderCell());
tcHeader[16].RowSpan = 2;
tcHeader[16].Style.Add("text-align", "center");
tcHeader[16].Text = "销售单价¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[17].RowSpan = 2;
tcHeader[17].Style.Add("text-align", "center");
tcHeader[17].Text = "成交总价¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[18].RowSpan = 2;
tcHeader[18].Style.Add("text-align", "center");
tcHeader[18].Text = "付款方式";
tcHeader.Add(new TableHeaderCell());
tcHeader[19].RowSpan = 2;
tcHeader[19].Style.Add("text-align", "center");
tcHeader[19].Text = "银行按揭¥";
//后几列
//第1行
tcHeader.Add(new TableHeaderCell());
tcHeader[20].ColumnSpan = 2;
tcHeader[20].Style.Add("text-align", "center");
tcHeader[20].Text = "成交房款";
tcHeader.Add(new TableHeaderCell());
tcHeader[21].ColumnSpan = 4;
tcHeader[21].Style.Add("text-align", "center");
tcHeader[21].Text = "应收代税费</th></tr><tr>";
//第2行
tcHeader.Add(new TableHeaderCell());
tcHeader[22].Style.Add("text-align", "center");
tcHeader[22].Text = "定金¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[23].Style.Add("text-align", "center");
tcHeader[23].Text = "首付¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[24].Style.Add("text-align", "center");
tcHeader[24].Text = "五通费¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[25].Style.Add("text-align", "center");
tcHeader[25].Text = "大修基金¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[26].Style.Add("text-align", "center");
tcHeader[26].Text = "其它费用¥";
tcHeader.Add(new TableHeaderCell());
tcHeader[27].Style.Add("text-align", "center");
tcHeader[27].Text = "代收总税¥";
} if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
{
DataRowView drvBasicCountInfor = (DataRowView)e.Row.DataItem;
xsmj = Convert.ToDouble(drvBasicCountInfor["Innerarea"]);
zjxsmj += xsmj;
xsdj = Convert.ToDouble(drvBasicCountInfor[5]);
zjxsdj += xsdj;
cjdj = Convert.ToDouble(drvBasicCountInfor[6]);
zjcjdj += cjdj;
aj = Convert.ToDouble(drvBasicCountInfor["Bankmortgage"]);
zjaj += aj;
dj = Convert.ToDouble(drvBasicCountInfor["Deposit"]);
zjdj += dj;
sf = Convert.ToDouble(drvBasicCountInfor["Thefirstphase"]);
zjsf += sf; f1 = Convert.ToDouble(drvBasicCountInfor["Deed"]);
zjf1 += f1;
f2 = Convert.ToDouble(drvBasicCountInfor["Overhaul"]);
zjf2 += f2;
f3 = Convert.ToDouble(drvBasicCountInfor["Mortgage"]);
zjf3 += f3;
dssf = Convert.ToDouble(drvBasicCountInfor["Othertaxes"]);
zjdssf += dssf; } if (e.Row.RowType == DataControlRowType.Footer) //页脚
{
e.Row.Font.Bold = true;
e.Row.Cells[4].Text = string.Format("{0}", zjxsmj + " ㎡");
e.Row.Cells[5].Text = string.Format("{0:c}", zjxsdj);
e.Row.Cells[6].Text = string.Format("{0:c}", zjcjdj);
e.Row.Cells[8].Text = string.Format("{0:c}", zjaj);
e.Row.Cells[9].Text = string.Format("{0:c}", zjdj);
e.Row.Cells[10].Text = string.Format("{0:c}", zjsf); e.Row.Cells[11].Text = string.Format("{0:c}", zjf1);
e.Row.Cells[12].Text = string.Format("{0:c}", zjf2);
e.Row.Cells[13].Text = string.Format("{0:c}", zjf3); e.Row.Cells[14].Text = string.Format("{0:c}", zjdssf);
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnToExcel_Click(object sender, EventArgs e)
{
Emport("application/ms-excel", "Employee.xls");
} //导出方法
private void Emport(string fileType, string fileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = fileType;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.EnableViewState = false;
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
} /// <summary>
/// 必须重写次方法,否则会报错
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
/// 导出Excel
/// </summary>
/// <param name="ctl">数据控件</param>
/// <param name="FileName">导出的文件名.xls</param>
public static void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
////HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName); //
//使用UTF-8对文件名进行编码
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + "\"");
HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=\"UTF-8\">"); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
//
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
数据源是Gridview 还是table。
如果是其他的,或者直接根据数据源DATATABLE拼凑表格再输出