首页 > 解决方案 > c#格式化数据写入csv文件

问题描述

我的 C# 非常有限,我的英语也很有限。我需要你的帮助。我需要从条形码阅读器编辑数据。像这样的传入数据。

1-) 12345678(条码)
2-) 310319(日期 dmy 格式)
3-) 174252(时分秒)
4-) 123 (条形码)
5-) 010419(日期 dmy 格式)
6-) 153020 (时分秒)
7-) 3873 (代码)

好的,这是我来自条形码阅读器的数据。我想按如下方式格式化这些数据。

最终输出是这样的样本。对不起,我的英语不好,你能帮帮我吗

1-) 12345678(条码不变)
2-)2和3行时间戳(310319 + 174252)?时间戳
3-) 123(条码不变)
4-) 5 和 6 行时间戳 (010419 + 153020) ?到时间戳
5-) 3873 (不变)
string name = lvw.SubItems[1].Text = Splt[0];
string filePath = @"C:\Temp\data.csv";
var one   = lvw.SubItems[1].Text = Splt[0];
var two   = lvw.SubItems[1].Text = Splt[1];
var three = lvw.SubItems[1].Text = Splt[2];
var four  = lvw.SubItems[1].Text = Splt[3];
var five  = lvw.SubItems[1].Text = Splt[4];
var six   = lvw.SubItems[1].Text = Splt[5];
var seven = lvw.SubItems[1].Text = Splt[6];
var data = one +";" + two + ";" + three + ";" 
        + four + ";" + five + ";" + six + ";" + seven + Environment.NewLine;                

File.AppendAllText(filePath, data);

变量二 + 三结合时间戳这是我的问题吗?

标签: c#

解决方案


string date = "310319";
        string time = "174252";
        DateTime dt;

        bool correct = DateTime.TryParseExact(date + time, 
                                      "ddMMyyHHmmss", 
                                      CultureInfo.InvariantCulture, 
                                      DateTimeStyles.AdjustToUniversal,
                                      out dt); 

        Console.WriteLine(dt);


        DateTimeOffset dto = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

        dto = new DateTimeOffset(dt, TimeSpan.Zero);
        long y =  dto.ToUnixTimeSeconds();

        Console.WriteLine(y); // output 1554054172

谢谢大家。这就是我解决问题的方法。


推荐阅读