且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将datagridview转换为excel文件?

更新时间:2023-01-18 15:10:11

您是否检查了对Excel的引用才能使导入正常工作? /p>

它必须在项目参考中,并在项目中添加/添加参考

如果这不能解决您的红色花粉",请在此处发布更多代码...



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.

Have you checked you have a reference to Excel for the Imports to work?

It needs to be in the project References, add with Project/Add reference

If that doesn't resolve your "red squiggles", then post some more of your code here...