[英]Remove mask from columns in export CSV File in C#
我正在導出一個csv文件,並屏蔽了日期字段和電話號碼字段,如下圖所示。 僅當我在其他平台上不在Microsoft Excel中打開導出的文件時,才會出現此問題
我想要做的是取下口罩,正確顯示出生日期和數字為:出生日期:“ 12/11/2016”,數字為“ 123456789”。
注意:生日是日期時間,並且手機或手機號碼是字符串
我用來創建此代碼的代碼如下:
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());
}
遮罩是什么意思,您可以附加excel文件的屏幕截圖嗎?
如果帶有日期的列不夠寬,則該值將顯示為######-這是您遇到的問題嗎?
使用EPPLus之類的庫創建XLSX文件實際上更容易。 XSLX文件是壓縮的XML文件,無需安裝Excel即可生成。 使用EPPlus,您可以通過簡單地調用LoadFromDatatable
或LoadFromCollection
來填充數據表或集合中的數據,例如:
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會注意以Excel期望的十進制格式(OA日期)序列化日期。
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.