生成Excel报告时需要毫秒的日期和时间

时间:2019-03-26 13:56:22

标签: sql-server excel sql-server-2008-r2 ado.net

我要在SQL中记录我的过程参数并生成Excel报告。 编程是在VB.Net中完成的

我们要在'YYYY-MM-DD HH:mm:ss.ms'中记录Date_Time例如:'2018-11-27 21:41:24.170'(这里ms是毫秒) 但是在使用excel导出时,它出现在'27 / 11/18 21:41:24.000'中,无法显示毫秒数据(此处为.170)。 因此,您能告诉我如何在Excel中显示日期和时间(包括毫秒)吗?

日期列的Excel设置:dd / mm / yy hh:mm:ss.000

请找到数据库快照,生成的报告快照和Vb.net程序以供参考。

请让我知道您的建议,这样可以解决我的问题。

01 .DatabseImage DatabseImage

02。生成的报告 Generated Report

    Imports System.Data
Imports System.Data.SqlClient
' For th is need to add Microsoft Excel componant from project > Add Ref > Com
Imports Excelr = Microsoft.Office.Interop.Excel

Module Module1
    Dim conn As SqlConnection
    Dim path As String = "C:\mysettxtup\Connectionstring.txt"
    Dim objectreader As New System.IO.StreamReader(path)
    Dim connetionString As String

    Dim adpt As New SqlDataAdapter
    Dim ds As New DataSet

    ' used for excel report
    Dim r, c As Integer
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
    Sub Main()
        '----- Read SQl Connection from Text File------
        connetionString = objectreader.ReadLine
        conn = New SqlConnection(connetionString)
        'Dim trn_Log1, trn_Log2 As ADODB.Recordset
        '************************************************************************************************************
        ' Set the Format type and the CustomFormat string.
        'Start date 
        ' DateTimePickerST.Format = DateTimePickerFormat.Custom
        ' DateTimePickerST.CustomFormat = "MM/dd/yyyy H:mm"
        'Clear connection and dataset before open
        conn.Close()
        ds.Clear()

        'To handle exception use here Try catach
        Try
            'Open connection 
            conn.Open()
            'Dim query As String

            '  Dim query As String = "Select * from ProDataTbl1Sec where Date_Time between '" & RStartDate.Text & "'and'" & REndDate.Text & "'order by date_time asc"

            Dim query As String = "SELECT * FROM ProDataTbl1Sec WHERE Date_Time >= dateadd(hour,-24,getdate())"
            'Dim query As String = "SELECT * FROM ProDataTbl1Sec"

            adpt.SelectCommand = New SqlCommand(query, conn)

            ds = New DataSet("wincc")
            adpt.Fill(ds)


            Dim i, j As Integer

            ' Standard for Excel application
            Dim xlApp As Excelr.Application
            Dim xlWorkBook As Excelr.Workbook
            Dim xlWorkSheet As Excelr.Worksheet
            'Dim shXL As Excel.Worksheet
            'xlWorkSheet.Visible = True

            Dim misValue As Object = System.Reflection.Missing.Value


            ' Dim xlSourceRange As Excel.Range
            xlApp = New Excelr.Application

            ' ------- Read Sample Report Location from Text file ------

            'Dim path As String = "C:\Pall\mysetup\connectionstring.txt"
            'Dim objectreader As New System.IO.StreamReader(path)
            'connetionString = objectreader.ReadLine
            'conn = New SqlConnection(connetionString)

            Dim Srpath As String = "C:\mysettxtup\samplereport.txt"
            Dim Srobjectreader As New System.IO.StreamReader(Srpath)
            Dim Srpathstring As String = Srobjectreader.ReadLine


            ' ------- Read Sample Report Location from Text file  ------
            xlWorkBook = xlApp.Workbooks.Add(Srpathstring & "\SampleReport")

            ' ------- Save as Report Location from Text file  ------
            Dim Slrpath As String = "C:\mysettxtup\Savereport.txt"
            Dim Slrobjectreader As New System.IO.StreamReader(Slrpath)
            Dim Slrpathstring As String = Slrobjectreader.ReadLine

            'xlWorkSheet = xlWorkBook.Sheets("D:\Pall\Report\SampleReport")
            xlWorkSheet = xlWorkBook.Sheets("Report")

            r = ds.Tables(0).Rows.Count + 7 ' 7 add here because we start 1 record from 8th row
            c = ds.Tables(0).Columns.Count

            Console.WriteLine(r)
            Console.WriteLine(c)

            Console.WriteLine("Work in Process")

            ' Print Table Row with column of worksheet
            For i = 0 To ds.Tables(0).Rows.Count - 1
                'For j = 0 To ds.Tables(0).Columns.Count - 1
                For j = 0 To ds.Tables(0).Columns.Count - 1


                    xlWorkSheet.Cells(i + 8, j + 1) = ds.Tables(0).Rows(i).Item(j)
                    xlWorkSheet.Columns("A:I").EntireColumn.AutoFit()
                    'xlWorkSheet.Columns("A").EntireColumn.farmat()
                Next

                Console.WriteLine(i)
            Next
            ' Save Excel Sheet
            '~~> Save Worksheet file to the following location
            'xlWorkSheet.SaveAs("E:\VB\Report\vbexcel_" + currentdate.ToString() + "xlsx")
            Dim currentdate As String = String.Format("{0:ddMMyyyy-Hmm }", DateTime.Now)
            ' Excel Animation
            'xlWorkSheet.Columns("A:I").EntireColumn.AutoFit()
            With xlWorkSheet
                .Range("A2").Value() = "Date : " & currentdate
            End With

            ' ------- Save as Report Location from Text file  ------
            'RLocation.Text = " Report save in " & Slrpathstring
            xlWorkSheet.SaveAs(Slrpathstring & "\ParagDailyReport_" & currentdate & ".xlsx")

            xlWorkBook.Close()
            xlApp.Quit()

            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)

            'close connection , dadaptor and clear dataset
            conn.Close()
            adpt.Dispose()
            ds.Dispose()


        Catch ex As Exception

            MsgBox(ex.ToString())

        End Try
    End Sub

End Module

0 个答案:

没有答案