导出Excel:避免剥离前导零 [英] Export Excel : Avoid stripping the leading zeros

查看:191
本文介绍了导出Excel:避免剥离前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据导出到C#.Net中的Excel工作表。在那里我有列有数据像00123450098。导出的数据没有第一个零。




这是我的导出excel代码。

string style = @< style> .text {mso-number-format:\ @;}< / style>;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
content-disposition,string.Format(attachment; filename = {0},fileName));
HttpContext.Current.Response.ContentType =application / ms-excel;
HtmlForm frm = new HtmlForm();
...................
...................
table.RenderControl(htw);
HttpContext.Current.Response.Write(style);
//将htmlwriter转换成响应
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();


解决方案

,您可以通过跟随单个报价来设置带有前导零的数字。即


00123450098至'00123450098


但是,然后,该单元格的格式将更改为文本。
如果你生成的Excel文件,有任何公式,其中包括该单元格引用作为数字,那么它将无法正常工作。


I am Export a data to Excel Sheet in C#.Net. There i am having column which has the data like "00123450098". The data is exported without the first zero's. I want to show the data as it is.

Here is my export excel code.

 string style = @"<style> .text { mso-number-format:\@; } </style> ";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", fileName));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    HtmlForm frm = new HtmlForm();
    ...................
    ...................
     table.RenderControl(htw);
            HttpContext.Current.Response.Write(style);
            //render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();

解决方案

In Excel file, Numbers cell always strips the leading zeros, you can set numbers with leading zeros by following a single quote. i.e.

00123450098 to '00123450098

but then, the format for that cell will changes to text. If your generated excel file, have any formula, which is include that cell reference as number then it will not work as expected.

这篇关于导出Excel:避免剥离前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆