vb.net 保存DataGridView为EXCEL文件,想存在D盘 文件名为ABC,怎么弄啊,我现有的保存时要打开EXCEL很烦
DimMyExcelAsNewMicrosoft.Office.Interop.Excel.Application()MyExcel.Application.Workbo...
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 - 2
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
次程序可用,就是导入EXECL时,会自动打开EXECL 然后通过EXECL才能保存,我想直接保存在D盘,文件名为ABC 怎么弄啊 望大神帮忙改改 谢谢 展开
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 - 2
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
次程序可用,就是导入EXECL时,会自动打开EXECL 然后通过EXECL才能保存,我想直接保存在D盘,文件名为ABC 怎么弄啊 望大神帮忙改改 谢谢 展开
1个回答
2016-12-07
展开全部
Dim MyExcel As New Microsoft.Office.Interop.Excel.Application()
MyExcel.Application.Workbooks.Add()
MyExcel.Visible = false
'获取标题
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 - 2
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
MyExcel.saveas "d:\abc.xls"
MyExcel.quit
追问
可是还是会打开EXECL啊 而且D盘也没有abc的xls文件啊
追答
MyExcel.ActiveWorkbook.SaveAs("z:\abc.xls")
MyExcel.Quit()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询