私はExcelファイルにデータを書き込むvb.netプログラムを作っています。それはこれまでのところ問題ではありません! しかし、ファイルにTimeSpansを書き込む際に問題があります。例えばExcelファイルにTimeSpanを書く
、コード上:
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
行にエラーが発生します。
On Error Resume Next
では、このコードは、他のすべてのTimeSpansを書き込みます。
おそらくこの問題には単純な解決策がありますが、実際にはわかりません。 ご協力いただければ幸いです。そして、いつものように、あなたにすべて前もって感謝します。
'.cells(3,7)= test07'はどうですか? – Vityata
いいえ!それはどちらも動作しません... – Pspl
'Dim test07 As TimeSpan = DateTime.Parse(" 1.12:03:55 ")'? – Vityata