【问题标题】:convert gridview to excel with original format使用原始格式将gridview转换为excel
【发布时间】:2014-05-13 16:21:06
【问题描述】:

我有下面的代码,单击时会生成一个 excel 文件。代码运行良好,颜色的格式很好。网格视图的数据带有前导零,这些数据未在 Excel 中显示,以及长数字“106638660952840428”,显示为 1.06639E+17,值为 106638660952840000,因此它也丢失了最后四位数字。我尝试了几种方法来导出所有结果都相同。我这样做是为了有机会单独格式化单元格。所以问题是有谁知道如何动态更改格式以便不会发生这些问题?要补充的另一件事是,Web 浏览器中的网格视图确实可以正确显示数据。

    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        Iframe1.Attributes.Add("src", "blank.aspx");

        if (CheckBox5.Checked || CheckBox6.Checked)
        {
            dt = (DataTable)Cache["dtable"];
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=BemisInventory.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                //GridView1.PageIndex = 0;
                GridView1.DataBind();

                GridView1.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in GridView1.HeaderRow.Cells)
                {
                    cell.BackColor = Color.White;//GridView1.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in GridView1.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = Color.White;
                        }
                        else
                        {
                            cell.BackColor = Color.White; //GridView1.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                GridView1.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

        }            
    }

【问题讨论】:

  • 通过添加修复:string style = @"";出现警告,但所有数据都在那里。

标签: c# asp.net excel gridview


【解决方案1】:

通过添加修复:string style = @" .textmode { mso-number-format:\@; } ";提出警告,但所有数据都在那里。

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2014-10-01
    • 2018-07-09
    • 2019-08-14
    相关资源
    最近更新 更多