导出为ex​​cel时,前导零不会显示

时间:2015-05-12 12:32:54

标签: asp.net

将数据导出到excel前导零时,导出到excel时没有显示。解决方法是什么。

 foreach (GridViewRow row in grd.Rows)
    {
        GridView gvOrderscell = (row.FindControl("gv_Child") as GridView);
        foreach (GridViewRow crow in gvOrderscell.Rows)
        {
            GridView gvOrderQty = (crow.FindControl("gv_ChildQty") as GridView);
            foreach (GridViewRow qtyrow in gvOrderQty.Rows)
            {
                Response.Write(row.Cells[1].Text + "\t" + row.Cells[2].Text + "\t" + row.Cells[3].Text);
                Response.Write("\t" + crow.Cells[1].Text + "\t" + crow.Cells[2].Text + "\t" + crow.Cells[3].Text + "\t" + crow.Cells[4].Text + "\t" + crow.Cells[5].Text);
                Response.Write("\t" + qtyrow.Cells[0].Text + "\t" + qtyrow.Cells[1].Text);
                Response.Write("\n");
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

您可以在字符串之前使用字符`。 Excel将其视为数字并删除前导零。

Response.Write("`"+row.Cells[1].Text + "\t" + "`"+row.Cells[2].Text + "\t" +"`"+ row.Cells[3].Text);

答案 1 :(得分:0)

这是Excel所做的事情。它将值解释为数字,并自动为您删除前导零。如果您在值前面放置一个引号,Excel会将其解释为字符串并保留前导零。

答案 2 :(得分:0)

请勿将数据与格式混淆。

Excel将禁止前导零,因为它们不需要表示数字。

您可以通过为单个引号字符添加前缀来保留字符串类型,但是在数值计算中使用该单元格会更难(并且更慢)。

如果右键单击Excel中的单元格,选择“格式化”,选择“自定义”,然后输入“00000”作为格式,则Excel将格式化数字值始终为5位数,任何必要的提供前导零。调整该格式以满足您的特定需求。