C# writeline properly show leading zeros save as csv
<p>Got a problem in properly handling leading zeros in csv, already tried adding <code>'</code> before the string value but in csv file it shows <code>'StringValue</code></p>

<p><a href="https://i.stack.imgur.com/SZTmI.jpg" rel="nofollow noreferrer">Sample Output</a></p>

<p>Already tried <code>\t</code> it works but when i try to open it with notepad it shows a tab.</p>

<p>Is there other way I can solve this issue? My main purpose is that when it is open through csv file the <code>'</code> must not show and when I open it with text editor it will not tab.</p>

<p>This is my current code</p>

<pre><code>foreach (var i in TransmittedDataBLL.Instance.GetDepEdTextFile(data))
        {
            writer.WriteLine(
                "'" + i.Region.ToString() + ",'" + i.Division.ToString() + ",'" + i.Station.ToString() + ",'" + i.EmployeeNumber.ToString()
                + "," + i.FirstName.ToString() + "," + i.MiddleInitial.ToString() + "," + i.LastName.ToString() + "," + i.Appelation.ToString()
                + ",'" + i.DednCode.ToString() + "," + i.DednSubCode.ToString() + ",'" + i.EffectDate.ToString() + ",'" + i.TermDate.ToString()
                + ",'" + i.AmountStr.ToString() + ",'" + i.LoanAmountStr.ToString() + ",'" + i.InterestAmountStr.ToString() + ",'" + i.OtherChargesStr.ToString()
                + "," + i.BillingType.ToString() + "," + i.UpdateCode.ToString()
                );
        }
</code></pre>

<p>Updated Code:</p>

<pre><code>writer.WriteLine("'{0}','{1}','{2}','{3}'", i.Region.ToString(), i.Division.ToString(), i.Station.ToString(), i.EmployeeNumber.ToString());
</code></pre>

<p>Updated Code: Same issue, leading zeros not showing</p>

<pre><code>writer.WriteLine("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", i.Region.ToString(), i.Division.ToString(), i.Station.ToString(), i.EmployeeNumber.ToString());
</code></pre>

<p>This the data from my query</p>

<p><a href="https://i.stack.imgur.com/NeHyZ.jpg" rel="nofollow noreferrer">SAMPLE RAW DATA</a></p>

<p>Full code:</p>

<pre><code>private void CreateTextFile(string regioncode, string branchcode)
{
    string sFileName = Path.GetRandomFileName();
    string fileName = DateTime.Now.ToString("mmddyyyy") + ".csv";
    string Serverpath=ConfigurationManager.AppSettings["UploadPath"].ToString() + "\\DepEdReports\\";

    TransmittedData data = new TransmittedData();
    data.Region = regioncode.Trim();
    data.BranchCode = branchcode.Trim();

    if (!Directory.Exists(Serverpath))
        Directory.CreateDirectory(Serverpath);

    using (StreamWriter writer = new StreamWriter(Serverpath + sFileName + ".csv"))//Server.MapPath("TextFiles/" + sFileName + ".txt")))
    {
        foreach (var i in TransmittedDataBLL.Instance.GetDepEdTextFile(data))
        {
            //writer.WriteLine(
            //    i.Region + "," + i.Division + "," + i.Station + "," + i.EmployeeNumber
            //    + "," + i.FirstName + "," + i.MiddleInitial + "," + i.LastName + "," + i.Appelation
            //    + "," + i.DednCode + "," + i.DednSubCode + "," + i.EffectDate + "," + i.TermDate
            //    + "," + i.AmountStr + "," + i.LoanAmountStr + "," + i.InterestAmountStr + "," + i.OtherChargesStr
            //    + "," + i.BillingType + "," + i.UpdateCode
            //    );
            //writer.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", i.Region, i.Division, i.Station, i.EmployeeNumber));

            writer.WriteLine(GetCSV(i));

        }
    }

    FileStream fs = null;
    //fs = File.Open(Server.MapPath("TextFiles/" + sFileName + ".txt"), FileMode.Open);
    fs = File.Open(Serverpath + sFileName + ".csv", FileMode.Open);
    byte[] fbyte = new byte[fs.Length];
    fs.Read(fbyte, 0, Convert.ToInt32(fs.Length));
    fs.Close();

    Response.AddHeader("Content-disposition", "attachment;filename=" + fileName);
    Response.ContentType = "application/octet-stream";
    Response.BinaryWrite(fbyte);
    Response.End();

}

private string GetCSV(TransmittedData i)
{
    string[] list = new string[]
    {
        i.Region.ToString(),
        i.Division.ToString(),
        i.Station.ToString(),
        i.EmployeeNumber.ToString(),
        i.FirstName,
        i.MiddleInitial,
        i.LastName,
        i.Appelation.ToString(),
        i.DednCode.ToString(),
        i.DednSubCode.ToString(),
        i.EffectDate.ToString(),
        i.TermDate.ToString(),
        i.AmountStr,
        i.LoanAmountStr,
        i.InterestAmountStr,
        i.OtherChargesStr,
        i.BillingType.ToString(),
        i.UpdateCode.ToString()
    };

    string csvLine = "\"" + string.Join(@"", "", list) + "\"";
    return csvLine;
}
</code></pre>