Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel文件上编写时间跨度_Excel_Vb.net_Time - Fatal编程技术网

在Excel文件上编写时间跨度

在Excel文件上编写时间跨度,excel,vb.net,time,Excel,Vb.net,Time,我正在做一个vb.net程序,我在Excel文件上写一些数据。到目前为止这还不是问题! 但是,我在文件上写时间跨度时遇到了问题 例如,关于代码: Sub DataExport() 'Create a bridge between Console and Excel: Dim ExcelBridge As Excel.Application ExcelBridge = New Excel.Application Dim NewWorkbook As Excel.Wo

我正在做一个vb.net程序,我在Excel文件上写一些数据。到目前为止这还不是问题! 但是,我在文件上写时间跨度时遇到了问题

例如,关于代码:

Sub DataExport()
    'Create a bridge between Console and Excel:
    Dim ExcelBridge As Excel.Application
    ExcelBridge = New Excel.Application
    Dim NewWorkbook As Excel.Workbook = ExcelBridge.Workbooks.Open("P:\HelpDesk\Definitions\Models\RPMAN.xlsm")
    NewWorkbook.Application.DisplayAlerts = False
    'Create Desktop folders if they don't exist:
    If Dir(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Reports", vbDirectory) = "" Then
        MkDir(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Reports")
    End If
    If Dir(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Reports\HelpDesk", vbDirectory) = "" Then
        MkDir(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Reports\HelpDesk")
    End If
    'Test variables:
    Dim test01 As Integer = 2
    Dim test02 As String = "FieldA"
    Dim test03 As String = "FieldB"
    Dim test04 As String = "FieldC"
    Dim test05 As Date = DateSerial(2017, 1, 1)
    Dim test06 As Date = Now()
    Dim test07 As TimeSpan = TimeSpan.Parse("1.12:03:55")
    Dim test08 As String = "FieldD"
    Dim test09 As TimeSpan = TimeSpan.Parse("3.20:43:07")
    Dim test10 As String = "FieldE"
    'Write Excel file:
    With NewWorkbook.Sheets("RESUMO")
        .cells(3, 1).formular1c1 = test01
        .cells(3, 2).formular1c1 = test02
        .cells(3, 3).formular1c1 = test03
        .cells(3, 4).formular1c1 = test04
        .cells(3, 5).formular1c1 = test05
        .cells(3, 6).formular1c1 = test06
        .cells(3, 7).formular1c1 = test07
        .cells(3, 8).formular1c1 = test08
        .cells(3, 9).formular1c1 = test09
        .cells(3, 10).formular1c1 = test10
    End With
    'Save Excel file:
    NewWorkbook.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Relatórios\HelpDesk\ITreport" & Format(Now(), "yyyyMMddHHmmss") & ".xlsm")
    'Close Excel file
    NewWorkbook.Close()
End Sub
我在电话里听到一个错误

.cells(3, 7).formular1c1 = test07
它根本不让我写

如果在错误恢复下一步时出现
,代码将写入除两个时间跨度之外的所有内容

也许这个问题有一个简单的解决方案,但我真的看不出来。
任何帮助都将不胜感激。和往常一样,提前感谢大家。

系统.TimeSpan
没有直接对应的Excel类型。您需要将其值转换为Excel能够理解的值。Excel将日期-时间值存储为与基准日期的偏移量,并将其存储为双精度,表示与所述偏移量之间的总天数和小数天数

但是,TimeSpan结构具有将用作Excel日期时间值的
TotalDays
属性。Excel单元格应设置NumberFormat以正确显示值(“d:HH:mm:ss”)


那么
.cells(3,7)=test07呢?不!这也不行…
Dim test07作为TimeSpan=DateTime.Parse(“1.12:03:55”)
?对不起!你最后的建议的任何组合都是有效的…问题显然在时间跨度内。尝试将其设置为字符串,并简单地将值放在那里。类似于
.cells(3,7).formular1c1=test07.ToString(@“dd\.hh \:mm \:ss”)
它工作了!谢谢。然而,我使用了代码
NewWorkbook.Sheets(“RESUMO”).Cells(3,7).FormulaR1C1=格式(test07.TotalDays,“d:HH:mm:ss”)
。。。
NewWorkbook.Sheets("RESUMO").Cells(3, 7).Value2 = test07.TotalDays
NewWorkbook.Sheets("RESUMO").Cells(3, 7).NumberFormat = "d:HH:mm:ss"