0

I'd like to know how to "apply" cell formatting to a cell when we don't write manually.

I have a vb.net application which enter the date and time to a cell in excel. The cells are formatted to display the date only as 16/04/2013.

When the application enter the date, excel displays it as a string so with the time. But if I double click to edit the cell then confirm without changing anything it formats the cell right.

Here are the screens

The last 5 are entered by the application.

I edited the n°66 then confirmed by enter and it is how I want it

enter image description here

Thank you

EDIT: Here is the code I use

Dim day As Date
oExcel.ScreenUpdating = False
oExcel.cells(6, 3).Select()
While oExcel.ActiveCell.Value <> Nothing
    oExcel.ActiveCell.OffSet(1, 0).Select()
End While
oExcel.ScreenUpdating = True

day = DateValue(Now)
oExcel.ActiveCell.Value = day & " " & TimeOfDay
oExcel.ActiveCell.Offset(0, 1).Value = timeOutput

EDIT2: Found a workaround

I found a way to convert the string to a number in excel here is the code:

Dim writeThis As String = day & " " & TimeOfDay
With oExcel.ActiveCell
  .OffSet(0, 10).Value = writeThis
  .Formula = "=" & oExcel.ActiveCell.OffSet(0, 10).Address & "+0"
  .Formula = oExcel.ActiveCell.Value
  .OffSet(0, 10).Value = Nothing
  .Offset(0, 1).Value = timeOutput
End With

I'm not sure it's the best way to do it but it works so i'm happy with it. Thank you Siddharth for all your help :)

4

1 回答 1

0

就像您正在写入现有文件一样。如果您一直在写入一个新文件,那么上面的代码就会按照您的意愿工作。

在现有文件中,列/单元格被格式化为文本,因此您的日期被存储为文本。

如果整个 Column 都在存储日期,请查看此示例(根据需要更改)

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    '~~> Open Existing Workbook
    xlWorkBook = xlApp.Workbooks.Open("C:\Sample.xlsx")

    '~~> Display Excel
    xlApp.Visible = True

    '~~> Set the relebant sheet that we want to work with
    xlWorkSheet = xlWorkBook.Sheets("Sheet1")

    Dim Day As Date = DateValue(Now)
    Dim TimeOfDay As Date = TimeOfDay

    With xlWorkSheet
        '~~> Directly type the values that we want
        .Columns(1).numberformat = "dd/mm/yyyy"
        .Range("A1").Value = Day & " " & TimeOfDay
    End With

    '
    '~~> Rest of the code
    '
End Sub

否则,如果您只想格式化单元格,请查看此代码

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    '~~> Open Existing Workbook
    xlWorkBook = xlApp.Workbooks.Open("C:\Sample.xlsx")

    '~~> Display Excel
    xlApp.Visible = True

    '~~> Set the relebant sheet that we want to work with
    xlWorkSheet = xlWorkBook.Sheets("Sheet1")

    Dim Day As Date = DateValue(Now)
    Dim TimeOfDay As Date = TimeOfDay

    With xlWorkSheet
        '~~> Directly type the values that we want
        With .Range("A1")
            .NumberFormat = "dd/mm/yyyy"
            .Value = Day & " " & TimeOfDay
        End With
    End With

    '
    '~~> Rest of the code
    '
End Sub
于 2013-04-16T10:26:23.280 回答