简体   繁体   English

从C#中的导出CSV文件中的列中删除掩码

[英]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. 我正在导出一个csv文件,并屏蔽了日期字段和电话号码字段,如下图所示。 The problem occurs only when I open the exported file in Microsoft Excel not on other platforms 仅当我在其他平台上不在Microsoft Excel中打开导出的文件时,才会出现此问题

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". 我想要做的是取下口罩,正确显示出生日期和数字为:出生日期:“ 12/11/2016”,数字为“ 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? 遮罩是什么意思,您可以附加excel文件的屏幕截图吗?

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 . 使用EPPLus之类的库创建XLSX文件实际上更容易。 XSLX files are zipped XML files which can be generated without having Excel installed. XSLX文件是压缩的XML文件,无需安装Excel即可生成。 With EPPlus you can fill a sheet with data from a DataTable or collection with a simple call to LoadFromDatatable or LoadFromCollection , eg: 使用EPPlus,您可以通过简单地调用LoadFromDatatableLoadFromCollection来填充数据表或集合中的数据,例如:

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. EPPlus会注意以Excel期望的十进制格式(OA日期)序列化日期。

LoadFromCollection returns an ExcelRange object which you can use to further format rows and columns, or create a named table, eg: LoadFromCollection返回一个ExcelRange对象,您可以使用该对象进一步格式化行和列,或创建命名表,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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