在VB.NET中使用xml和datagridview导出excel时如何设置自定义日期格式?

时间:2016-09-14 23:46:47

标签: vb.net excel date datagridview

我很难设置导出excel的自定义格式。 想要excel的自定义日期格式是yyyy-mm-dd。 我已经在datagridview中设置了日期格式,但它不会影响或在excel中工作。

 Private Sub ExportToExcel(ByVal DGV As DataGridView)

        Dim fs As New StreamWriter(filePathAndNameHome, False)

        With fs
            .WriteLine("<?xml version=""1.0""?>")
            .WriteLine("<?mso-application progid=""Excel.Sheet""?>")
            .WriteLine("<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet"">")
            '...
            .WriteLine("    </Styles>")

            If DGV.Name = "Report" Then
                .WriteLine("    <Worksheet ss:Name=""Report"">") 'SET NAMA SHEET
                .WriteLine("        <Table>")
                'Columns
                '...
            End If

            'AUTO SET HEADER
            .WriteLine("            <Row ss:StyleID=""ksg"">")
            For i As Integer = 0 To DGV.Columns.Count - 1 'SET HEADER
                Application.DoEvents()
                .WriteLine("            <Cell ss:StyleID=""hdr"">")
                .WriteLine("                <Data ss:Type=""String"">{0}</Data>", DGV.Columns.Item(i).HeaderText)
                .WriteLine("            </Cell>")
            Next
            'The row
            .WriteLine("            </Row>")
            For intRow As Integer = 0 To DGV.RowCount - 1
                Application.DoEvents()
                .WriteLine("        <Row ss:StyleID=""ksg"" ss:utoFitHeight =""0"">")

                For intCol As Integer = 0 To DGV.Columns.Count - 1
                    Application.DoEvents()
                    .WriteLine("        <Cell ss:StyleID=""isi"">")
                    .WriteLine("            <Data ss:Type=""String"">{0}</Data>", DGV.Item(intCol, intRow).Value) 'How to change its date format
                    .WriteLine("        </Cell>")
                Next
                .WriteLine("        </Row>")
            Next
            .WriteLine("        </Table>")
            .WriteLine("    </Worksheet>")
            .WriteLine("</Workbook>")
            .Close()
        End With
    End Sub

2 个答案:

答案 0 :(得分:0)

在Excel中,如果您将单元格A1的自定义格式更改为yyyy-mm-dd,然后在VBA立即窗口中键入?[a1].value(11),则会得到以下结果:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62">
   <NumberFormat ss:Format="yyyy\-mm\-dd"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1">
   <Column ss:Width="53.25"/>
   <Row>
    <Cell ss:StyleID="s62">
     <Data ss:Type="DateTime">1899-12-31T00:00:00.000</Data>
    </Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>

所以将ss:Type更改为DateTime并将数字格式添加到isi样式定义

  <Style ss:ID="isi">
   <NumberFormat ss:Format="yyyy\-mm\-dd"/>
  </Style>

答案 1 :(得分:0)

为了在Excel中设置自定义格式,首先,我们需要确定日期所在或插入的列号。

确定列号后,您只需添加如下条件:

 For intCol As Integer = 0 To DGV.Columns.Count - 1
         Application.DoEvents()
         .WriteLine("        <Cell ss:StyleID=""isi"">")
            If intCol = 7 Or intCol = 12 Or intCol = 14 Or intCol = 15 Then 'if column 7 or ... has date set custom format
               .WriteLine("            <Data ss:Type=""String"">{0}</Data>", Format(DGV.Item(intCol, intRow).Value, "yyyy-MM-dd").ToString())
            Else
               .WriteLine("            <Data ss:Type=""String"">{0}</Data>", DGV.Item(intCol, intRow).Value)
            End If
         .WriteLine("        </Cell>")
     Next