简体   繁体   中英

Remove mask from columns in export CSV File in C#

在此处输入图片说明

I am exporting a csv file and getting the date field and phone number field masked as shown in the image below. The problem occurs only when I open the exported file in Microsoft Excel not on other platforms

What I want to do is remove the mask and show the birth date and number properly as: birth date: "12/11/2016" and number as "123456789".

Note: birth date is datetime AND cell_phone or mobile number is string

The code I used to create this is as follows:

    foreach (EmployeeDataExportObject item1 in _obj_distinct_company)
                {

                    sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21}"
                    , item1.client_id
                    , item1.employee_number
                    , item1.employee_status_type
                    , item1.first_name.Replace(",", "")
                    , item1.middle_name.Replace(",", "")
                    , item1.last_name.Replace(",", "")
                    , '"' + item1.ssn.ToString() + '"'
                    , '"' + birth_date.ToString("MM-dd-yyyy") + '"' //problem is on this part
                    , Address.Replace(",", "")
                    , item1.address2.Replace(",", "")
                    , item1.city
                    , item1.state
                    , item1.zip_code
                    , item1.country
                    , '"' + item1.cell_phone.ToString().Replace("-", "").Replace("(", "").Replace(")", "").Replace(".", "").Replace(" ", "") + '"' //problem is on this part
                    , item1.pay_rate1
                    , item1.pay_rate_amount1.ToString("0.00")
                    , item1.pay_rate2
                    , item1.pay_rate_amount2.ToString("0.00")
                    , item1.employee_status_type
                    , item1.termination_reason.Replace(",", " ")
                    , item1.termination_date.ToString("MM-dd-yyyy"))
                    + Environment.NewLine);
                }

                _fileName = item.company_name;
                if (!Directory.Exists(paychex_folder))
                    Directory.CreateDirectory(paychex_folder);

                filepath = paychex_folder + _fileName + ".csv";

                File.WriteAllText(filepath, sb.ToString());
            }

What do you mean by masked, can you attach a screenshot of the excel file?

If a column with a date in is not wide enough, the value is shown as ###### - is this the problem you are having?

It's actually easier to create an XLSX file using a library like EPPLus . XSLX files are zipped XML files which can be generated without having Excel installed. With EPPlus you can fill a sheet with data from a DataTable or collection with a simple call to LoadFromDatatable or LoadFromCollection , eg:

FileInfo targetFile = new FileInfo(targetFile);

using (var excelFile = new ExcelPackage(targetFile))
{
    var sheet = excelFile.Workbook.Worksheets.Add("Sheet1");
    sheet.LoadFromCollection(_obj_distinct_company, PrintHeaders: true);
    excelFile.Save();
}

EPPlus takes care to serialize dates in the decimal format (OA Date) expected by Excel.

LoadFromCollection returns an ExcelRange object which you can use to further format rows and columns, or create a named table, eg:

var range1=sheet.LoadFromCollection(_obj_distinct_company, PrintHeaders: true);
var table = sheet.Tables.Add(range, "Companies");
table.TableStyle = TableStyles.Light2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM