Include ZERO before the numbers in CSV file?

Asked

Viewed 1,957 times

3

I need to include ZERO to complete the digits of the CPF in the csv file, however, if the CPF starts with ZERO is taken out and only the numbers, that is, has Cpf that saves only 9 digits, because the first two do not enter the account...

public string CreateCSVFile(List<ExcelColumsData> registers)
{
    List<string> linesList = new List<string>();
    foreach (var item in registers)
    {
        linesList.Add(item.Licence + ";" + item.CPF.ToString() + ";" + item.CodeANAC + ";" + item.Plate + ";" + item.FullName + ";" + item.BaseContract + ";" + item.IATACODE);
    }

    string fileName = "G3_" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
    FreePassDataContextDataContext dataContext = new FreePassDataContextDataContext(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
    var freePassConfigurationsEntity = dataContext.FreePassConfigurations.FirstOrDefault();

    string downloadFolderSFTP = "C:\\ftp\\"; 

    System.IO.File.WriteAllLines(downloadFolderSFTP + fileName, linesList, System.Text.Encoding.GetEncoding("iso-8859-1"));

    return fileName;
}

The code appears as string normally, but the CSV file does not. You can help me?

  • Remembering that no dot or dash is used in the CPF, only the same numbers.

  • What commands are used to create your CSV file?

  • I changed the post by putting the whole method.

  • Without analyzing your code too much, I found strange the following literal: ";'" on line 6 (linesList.Add) because there is no final quote that serves as a delimiter

  • This was a test that I made, disregard, in this case it includes the apostrophe in the beginning and prints the 0 however, should not have the apostrophe, sorry my carelessness.

1 answer

2


You can use it as follows:

Convert.ToUInt64(item.CPF.ToString()).ToString(@"000\.000\.000\-00")

This forces the CPF to pass through a mask specified in the second ToString().


EDIT

For numbers only, use:

String.Format("{0:00000000000}", item.CPF);

I’m guessing CPF is numerical.

  • It would be useful, but in my case, I can not wear mask, only numbers, because the system that reads this field, q is third party receives the CPF only by number.

  • @Ewertonmelo I edited the answer.

  • So, still in the CSV he doesn’t put the zero numbers in front...

  • 1

    @Ewertonmelo Edited again.

  • Thank you very much friend. Your reply was of great help and solved the problem. Thank you even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.