none
想请教有关代码的问题 RRS feed

  • 问题

  • 想问看有方法将datagridview 的titile 也convert进excel file吗

            MessageBox.Show("Pls wait a while", "Information")

            Dim xlapp As Microsoft.Office.Interop.Excel.Application
            Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
            Dim i As Integer
            Dim j As Integer

            xlapp = New Microsoft.Office.Interop.Excel.Application
            xlWorkBook = xlapp.Workbooks.Add(misValue)
            xlWorkSheet = CType(xlWorkBook.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)

            For i = 0 To dgv1.RowCount - 1
                For j = 0 To dgv1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) = _
                        dgv1(j, i).Value.ToString()
                Next
            Next

            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "Execl files (*.xls)|*.xls"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                xlWorkSheet.SaveAs(saveFileDialog1.FileName)
                MsgBox("Save file success")
            Else
                Return
            End If
            xlWorkBook.Close()
            xlapp.Quit()

            releaseObject(xlapp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)

            RefreshData()


    我 excel convert 的document 没有上图的title只有内容

    请指教

    2020年11月4日 2:58

答案

  • Hi christing,

    经过我的测试,你需要修改以下代码:

            For i = 0 To dgv1.RowCount - 1
                For j = 0 To dgv1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) =
                        dgv1(j, i).Value.ToString()
                Next
            Next

    修改后的代码为:

            For k = 0 To dgv1.ColumnCount - 1
                xlWorkSheet.Cells(1, k + 1).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                xlWorkSheet.Cells(1, k + 1) = dgv1.Columns(k).Name
            Next
            For i = 0 To dgv1.RowCount - 1
                For j = 0 To dgv1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 2, j + 1).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                    xlWorkSheet.Cells(i + 2, j + 1) =
                        dgv1(j, i).Value.ToString()
                Next
            Next

    注意:我已将所有 DataGridView中的元素设置为居中对齐。

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年11月6日 8:46
    2020年11月6日 2:05
    版主
  • Hi christing,

    一个快速解决该问题的方式是将 ‘Option Strict On’ 改成 ‘Option Strict Off’.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年11月6日 8:46
    2020年11月6日 7:10
    版主

全部回复

  • Hi christing,

    经过我的测试,你需要修改以下代码:

            For i = 0 To dgv1.RowCount - 1
                For j = 0 To dgv1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) =
                        dgv1(j, i).Value.ToString()
                Next
            Next

    修改后的代码为:

            For k = 0 To dgv1.ColumnCount - 1
                xlWorkSheet.Cells(1, k + 1).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                xlWorkSheet.Cells(1, k + 1) = dgv1.Columns(k).Name
            Next
            For i = 0 To dgv1.RowCount - 1
                For j = 0 To dgv1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 2, j + 1).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                    xlWorkSheet.Cells(i + 2, j + 1) =
                        dgv1(j, i).Value.ToString()
                Next
            Next

    注意:我已将所有 DataGridView中的元素设置为居中对齐。

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年11月6日 8:46
    2020年11月6日 2:05
    版主
  • @xing yu zhao

    感谢你的回复

    我出现error在  xlWorkSheet.Cells(1, k + 1).HorizontalAlignment 。

    2020年11月6日 4:15
  • Hi christing,

    你可以提供对应的错误信息吗?

    另外,如果你不需要对齐方式,可以考虑将这两行代码删除。

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年11月6日 5:32
    版主
  • hi, xing yu zhao

    我遇到下图error

    2020年11月6日 6:44
  • Hi christing,

    一个快速解决该问题的方式是将 ‘Option Strict On’ 改成 ‘Option Strict Off’.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年11月6日 8:46
    2020年11月6日 7:10
    版主