Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

c# 4.0 - Export Excel : Avoid stripping the leading zeros

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While exporting to excel, adding before the value being inserted will solve the problem. Eg:

string test = "000456";
string insertValueAs = "" + test;

The string test would then be considered as a string value and not an integer value. Thus, it would retain the leading zeros.

I have faced the same issue, and above solution worked for me. Hope this post helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...