本文介绍了如何将datagridview转换为excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告知我的错误.

在下面写下程序源

导入Excel = Microsoft.Office.Interop.Excel
      昏暗的APP作为新的 Excel.Application
      将工作表变暗为 Excel.Worksheet
      昏暗的工作簿为 Excel.Workbook
      工作簿= APP.Workbooks.Open( excelLocation )
       worksheet = workbook.Worksheets("sheet1")
      昏暗的columnCount作为整数= DataGridView1.Columns.Count
      对于DataGridView1.Columns中的每一列
           worksheet.Cells(1,column.Index +1).Value = column.Name
      下一个
      对于i As Integer = 0到DataGridView1.Rows.Count-1
           Dim columnIndex As Integer = 0
          直到columnIndex = columnsCount
                             worksheet.Cells(i + 2,columnIndex +1).Value = DataGridView1.Item(columnIndex,i).Value.ToString
                             columnIndex + = 1
          循环
      下一个

在VB.net中用红色线条写下了我用黑体加粗的字符

这意味着VB.net未知此命令

请给我一个更正的代码.

解决方案


Please inform what my mistake is.

By write down the program source, below

Imports Excel = Microsoft.Office.Interop.Excel
        Dim APP As New Excel.Application
        Dim worksheet As Excel.Worksheet
        Dim workbook As Excel.Workbook
        workbook = APP.Workbooks.Open(excelLocation)
        worksheet = workbook.Worksheets("sheet1")
        Dim columnsCount As Integer = DataGridView1.Columns.Count
        For Each column In DataGridView1.Columns
            worksheet.Cells(1, column.Index + 1).Value = column.Name
        Next
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            Dim columnIndex As Integer = 0
            Do Until columnIndex = columnsCount
                worksheet.Cells(i + 2, columnIndex + 1).Value = DataGridView1.Item(columnIndex, i).Value.ToString
                columnIndex += 1
            Loop
        Next

The character, that I bold in black color is written down with red colored line in VB.net

It means that this command has not known by VB.net

Please give me a corrective code.

解决方案


这篇关于如何将datagridview转换为excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:00