今天做机房收费系统做到了报表打印部分了。
在将dataGridView中的数据导出到excel时,到网上找了一些相关的内容,个人感觉不是太好,有的在复杂了。
下面是简单的实现dataGridView数据到excel:
当然首先要添加引用:Microsoft.Office.Interop.Excel
Dim MyExcel As New Microsoft.Office.Interop.Excel.Application() MyExcel.Application.Workbooks.Add() MyExcel.Visible = True '获取标题 Dim Cols As Integer For Cols = 1 To DataGridView1.Columns.Count MyExcel.Cells(1,Cols) = DataGridView1.Columns(Cols - 1).HeaderText Next '往excel表里添加数据() Dim i As Integer For i = 0 To DataGridView1.RowCount - 1 Dim j As Integer For j = 0 To DataGridView1.ColumnCount - 1 If Me.DataGridView1(j,i).Value Is System.DBNull.Value Then MyExcel.Cells(i + 2,j + 1) = "" Else MyExcel.Cells(i + 2,j + 1) = DataGridView1(j,i).Value.ToString End If Next j Next i