我正面临一个我根本无法理解的问题。创建excel文件时,我有多列,并且在每列上我想格式化其单元格。到目前为止一切都很好,几乎所有这些都在工作,但是当我尝试格式化Date列时它根本不起作用。单元格格式完全忽略了我传递给.NumberFormat
的阵型。这是我用于格式化的代码:
'Set textAlignment on Double
For c As Integer = 1 To dt.Columns.Count
If dt.Columns(c - 1).ColumnName = "Kasnr" Or dt.Columns(c - 1).ColumnName = "Nr" Then
For r As Integer = 1 To dt.Rows.Count
wBook.Worksheets(1).Cells(r, c).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
Next
ElseIf dt.Columns(c - 1).ColumnName = "BeginSaldo" Or dt.Columns(c - 1).ColumnName = "EindSaldo" Or dt.Columns(c - 1).ColumnName = "bedragExBTW" Or dt.Columns(c - 1).ColumnName = "Transactiedatum" Or dt.Columns(c - 1).ColumnName = "Grootboeknummer" Then
For r As Integer = 1 To dt.Rows.Count
wBook.Worksheets(1).Cells(r, c).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight
Next
ElseIf dt.Columns(c - 1).ColumnName = "Datum" Then
For r As Integer = 1 To dt.Rows.Count
wBook.Worksheets(1).Cells(r, c).NumberFormat = "dd/MM/yyyy;@"
Next
Else
Continue For
End If
Next
现在wBook.Worksheets(1).Cells(r, c).NumberFormat = "dd/MM/yyyy;@"
它显示为'11 -18-2013',我真的想要一个欧洲日期格式为'18 -11-2013'。现在,如果我wBook.Worksheets(1).Cells(r, c).NumberFormat = "MM/dd/yyyy;@"
,那么excel列将显示'18 -11-2013'..这很奇怪,因为我将它设置为相反的方式..有没有人遇到过这样的事情?
编辑: 我为每个单元格设置值的代码:
Dim dr1 As System.Data.DataRow
For i As Integer = 0 To datagridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To datagridView1.Columns.Count - 1
If TypeOf (datagridView1.Rows(i).Cells(j).Value) Is DateTime Then
'Format Date to display on Excel file
If datagridView1.Columns(j).Name = "Datum" Or datagridView1.Columns(j).Name = "Transactiedatum" Then
Dim datumFormat As String = datagridView1.Rows(i).Cells(j).Value.Date.Day & "/" & datagridView1.Rows(i).Cells(j).Value.Date.Month & "/" & datagridView1.Rows(i).Cells(j).Value.Date.Year
dr1(j) = datumFormat
ElseIf datagridView1.Columns(j).Name = "Periode" Then
Dim PeriodeFormat As String = datagridView1.Rows(i).Cells(j).Value.Date.Year & "/" & datagridView1.Rows(i).Cells(j).Value.Date.Month
dr1(j) = PeriodeFormat
Else
dr1(j) = datagridView1.Rows(i).Cells(j).Value
End If
ElseIf TypeOf (datagridView1.Rows(i).Cells(j).Value) Is Double Or TypeOf (datagridView1.Rows(i).Cells(j).Value) Is Integer Then
'Format Double/force 2 decimals
If datagridView1.Columns(j).Name = "BeginSaldo" Or datagridView1.Columns(j).Name = "EindSaldo" Or datagridView1.Columns(j).Name = "bedragExBTW" Then
dr1(j) = DMDbl(datagridView1.Rows(i).Cells(j).Value).ToString("N2")
Else
dr1(j) = datagridView1.Rows(i).Cells(j).Value
End If
Else
dr1(j) = datagridView1.Rows(i).Cells(j).Value
End If
Next
dset.Tables(0).Rows.Add(dr1)
Next
正如你可以看到你在两者上传递一个字符串(Datum和TransactieDatum)。但是Datum专栏总是得到一种格式(不正确)和Transactiedatum,也是一个日期,根本没有任何格式。
EDIT2: 我在excel上设置单元格值的代码:
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next