2

Iam Exporting my datas to .csv format from asp.net project. When iam exporting those numbers having zero at beginning was handled by adding

="0183"

Then it is visible as 0183 in excel. But the problem is when i am copying these things to other files it again copied as ="0183"

So , can anyone help me to handle zero started numbers in export to .csv from C# coding?

BOBIN JOSEPH
  • 1,012
  • 8
  • 25
  • could you rephrase the question again? whats the significance of `0183`? – naveen May 04 '15 at 04:58
  • would you please show us your code? – Marco May 04 '15 at 04:58
  • tried any of these? http://stackoverflow.com/q/12071963/17447 – naveen May 04 '15 at 05:03
  • @naveen : thats just a number starts with zero. this link also explains the same what i mentioned above. – BOBIN JOSEPH May 04 '15 at 05:07
  • @BOBINJOSEPH, so the problem is that numbers starting with leading zeros are treated as numbers. and you want them to be treated as string, right? read http://stackoverflow.com/q/7437662/17447 and http://stackoverflow.com/q/17016803/17447 and http://stackoverflow.com/q/6627301/17447 – naveen May 04 '15 at 05:14

1 Answers1

1

The problem is that its the excel thats auto converting the csv to string. If you create a simple CSV and open it in excel using the Open with Excel you can see the behavior. So first and easier option will be making the column type text and viewing it.

The other option will be to parse the column using regex before re-translating it to another format like this.

var inputString = "=\"0813\"";
var match = Regex.Match(inputString, "=\"([^\"]*)\"");
var output = match.Groups.Count > 1 ? match.Groups[1].Value : inputString;
naveen
  • 53,448
  • 46
  • 161
  • 251