如何将datagridview转换为excel文件? [英] How to convert the datagridview into excel file ?

查看:71
本文介绍了如何将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未知此命令

请给我一个更正的代码.

解决方案

您是否检查了对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...



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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆