使用asp日期问题导出到Excel

时间:2014-01-16 03:15:10

标签: c# asp.net excel gridview

您好我有这个代码将我的数据gridview导出到excel 我的问题是,gridview显示12小时格式但是当我将它导出到excel时它显示24小时格式,我怎么把它变成12小时格式?谢谢。

            DataSet ds = (DataSet)ViewState["audit"];
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            Table table = new Table();
            AuditTrailGV.AllowPaging = false;
            AuditTrailGV.DataSource = (DataSet)ViewState["audit"];
            AuditTrailGV.DataBind();

            for (int i = 0; i < AuditTrailGV.HeaderRow.Cells.Count; i++)
            {
                AuditTrailGV.HeaderRow.Cells[i].Style.Add("background-color", "#bfc2c7");

            }
            AuditTrailGV.HeaderRow.Style.Add("border-color", "#FFFFFF");
            int j = 1;
            foreach (GridViewRow gvrow in AuditTrailGV.Rows)
            {
                //gvrow.BackColor = color.White;
                gvrow.Style.Add("background-color", "#FFFFFF");
                gvrow.Style.Add("border-color", "#bfc2c7");
                if (j <= AuditTrailGV.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                            gvrow.Cells[k].Style.Add("border-color", "#bfc2c7");
                        }

                    }
                    else
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("border-color", "#bfc2c7");
                        }
                    }
                }
                j++;
            }


            foreach (GridViewRow row in AuditTrailGV.Rows)
            {
                table.Rows.Add(row);
            }



            TableHeaderCell header = new TableHeaderCell();
            header.RowSpan = 1;
            header.ColumnSpan = 6;
            header.Text = "Inventory Accounting and Control System";
            header.Font.Bold = true;
            header.HorizontalAlign = HorizontalAlign.Center;
            header.VerticalAlign = VerticalAlign.Middle;
            header.Font.Size = 20;
            TableRow headerrow = new TableRow();
            headerrow.Cells.Add(header);



            TableHeaderCell header2 = new TableHeaderCell();
            header2.RowSpan = 1;
            header2.ColumnSpan = 6;
            header2.Text = "Audit Trail";
            header2.Font.Bold = true;
            header2.HorizontalAlign = HorizontalAlign.Center;
            header2.VerticalAlign = VerticalAlign.Middle;
            header2.Font.Size = 16;
            TableRow headerrow2 = new TableRow();
            headerrow2.Cells.Add(header2);

            TableHeaderCell header3 = new TableHeaderCell();
            header3.RowSpan = 1;
            header3.ColumnSpan = 6;
            header3.Text = DatefromTxtBox.Text + " to " + DatetoTxtBox.Text;
            header3.Font.Bold = true;
            header3.HorizontalAlign = HorizontalAlign.Center;
            header3.VerticalAlign = VerticalAlign.Middle;
            header3.Font.Size = 16;
            TableRow headerrow3 = new TableRow();
            headerrow3.Cells.Add(header3);

            table.Rows.AddAt(0, headerrow);
            table.Rows.AddAt(1, headerrow2);
            table.Rows.AddAt(2, headerrow3);
            table.Rows.AddAt(3, AuditTrailGV.HeaderRow);


            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "IACSAuditTrail" + "[" + DatefromTxtBox.Text.Replace("/", "") + "_" + DatetoTxtBox.Text.Replace("/", "") + "]" + ".xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
  table.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End()

它的外观enter image description here
但这是我将它导出到excel时的样子:(

enter image description here

1 个答案:

答案 0 :(得分:0)

保存日期格式的唯一方法是将日期字段预格式化为文本,然后导出到excel。为此,

string style = @"<style> TD { mso-number-format:\@; } </style> ";
Response.write(style);
//Code to Export Control
Panel1.RenderControl(htmlWrite);

您可以参考以下链接获取更多信息

  

http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx