如何将字符串值从xlworksheet解析为time(hh:mm)值到新表中?

杰拉尔德

我无法将具有in0和out0属性的对象“ Time”从字符串转换为DateTime。

for (int row = 2; row <= rw; row++)
                    {
                        for (cCnt = 6; cCnt <= 6; cCnt++)
                        {
                            var in0 = (String)(xlworkSheet.Cells[row, 7] 
                             as Excel.Range).Value;
                            Time.In0 = in0;
                            Console.WriteLine(Time.In0.ToString());
                            DateTime dtnew = DateTime.Parse(in0);

                            Excel.Range formatRange;
                            formatRange = xlNewSheet.Cells[row, 7];
                            formatRange.NumberFormat = "hh:mm";
                            xlNewSheet.Cells[row, 7].Value = Time.In0;

                        }
                    }
 //this is my class
  public class DateandTime 
      {
       public string In0 { get; set; }
       public string Out0 { get; set; }
       public string In1 { get; set; }
       public string Out1 { get; set; }
       public string In2 { get; set; }
       public string Out2 { get; set; }
       public string In3 { get; set; }
       public string Out3 { get; set; }
       public string In4 { get; set; }
       public string Out4 { get; set; }
       public string break_time { get; set; }

         }

mscorlib.dll中发生了'System.FormatException'类型的未处理异常

附加信息:字符串未被识别为有效的DateTime。

约翰

出现此错误的原因是,当您使用时DateTime.Parse(string),线程的当前区域性用于解释该日期/时间。如果您不提供日期,.NET会假定是今天,但是时间格式未知:它不知道如何解释4位数字。我们必须提供有关如何解释此字符串的信息。

一种解决方法是使用DateTimeTryParseExact方法,然后格式化结果:

private static bool TryFormatTime(string time, out string formattedTime)
{
    formattedTime = null;
    DateTime parsedDate;
    if (!DateTime.TryParseExact(time, "HHmm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out parsedDate))
    {
        return false;
    }

    formattedTime = parsedDate.ToString("HH:mm", System.Globalization.CultureInfo.InvariantCulture);
    return true;
}

或者,您也可以将其作为字符串操作来处理。下面的代码验证所有字符都是数字的长度,然后添加一个:

private static bool TryFormatTime(string time, out string formattedTime)
{
    formattedTime = null;
    if (time.Length != 4 || !time.All(c => char.IsDigit(c)))
    {
        return false;
    }

    formattedTime = string.Format("{0}:{1}", time.Substring(0, 2), time.Substring(2, 2));
    return true;
}

这两个都可以如下使用:

if (TryFormatTime("0655", out time))
{
    Console.WriteLine(time);
}
else
{
    Console.WriteLine("bad time");
}

在线尝试

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将“ hh:mm:ss.SSS”的字符串解析为DateTime

如何将数组中的真假字符串解析为布尔值

Java 8 Time API:如何将“ mm:ss”格式的字符串解析为Duration?

如何将逗号分隔值的字符串解析为haskell中的字符串列表?

将字符串解析为time.Time值

Java 8 Time API:如何将格式为“ MM.yyyy”的字符串解析为LocalDate

解析时如何将字符串JSON值膨胀到对象?

如何将字符串解析为变量/函数/浮点数/整数赋值/值?

如何将枚举字符串和值从 XML 模型解析为对象

如何将sql中的值映射为字符串值

在JAXB中,如何将节点值设置为字符串字段的属性值?

如何将字符串字符的 ASCII 值解释为 C 中的字符?

如何将值从NSMutableArray存储到字符串?

如何将ListBox的值设置为字符串

如何将长值存储为字符串

如何将字符串值映射为从 1 到多个字符串的整数

如何将URL字符值复制到字符串中

如何将下拉列表中的 selectlistitem 值绑定到模型中的字符串属性

如何将日期选择器中的值存储到字符串变量中

如何将字符串值存储到php中的数组中?

如何将字符串中的值分隔为 postgresql 中的列?

如何将以毫秒(hh:mm:ss.xxx)为单位的字符串时间转换为time.Time?

如何将itemsource的值(字符串数组)绑定到ListView中的标签

如何将某些值插入到某个部分的字符串中

如何将变量插值到字符串中?

如何将字符串作为键和值传递到数组中

如何将复选框绑定到Angular7中的字符串值?

如何将独立值绑定到数组中的字符串(Swift)?

如何将给定的字符串格式解析为dd-MMM-yyyy hh:mm:ss:aa?