VB中导出数据到EXCEL问题,求高手解答.
IfDir("C:\DocumentsandSettings\Administrator\MyDocuments\RESUME.XLW")<>""ThenKill"C:\...
If Dir("C:\Documents and Settings\Administrator\My Documents\RESUME.XLW") <> "" Then Kill "C:\Documents and Settings\Administrator\My Documents\RESUME.XLW" End If '下面代码创建Excel实体并打开 text1.text 中指定的Excel文件 Dim i, j As Long Set MyApp = CreateObject("Excel.Application") MyApp.Visible = False Set MyBook = MyApp.workbooks.open(text1.text) Set Mysheet = MyBook.worksheets(1) '下面Do循环导出在MSFlexGrid表格中的记录到Excel表 j = 1 Do While j < MSFlexGrid1.Rows For i = 0 To MSFlexGrid1.Cols - 1 Mysheet.Cells(j + 1, i + 1) = MSFlexGrid1.TextMatrix(j, i) Next j = j + 1 Loop '下面代码保存导出的数据到 text1.text 中 MyBook.Saveas text1.text MyApp.Save MyApp.Quit Set Mysheet = Nothing Set MyBook = Nothing Set MyApp = Nothing MsgBox "数据导出成功!", vbInformation, "导出"
展开
1个回答
展开全部
给你个函数吧,原创第一次发布
Public
Sub
MSFlexGridToOldExcel(MMSFlexGrid
As
MSFlexGrid,
ByVal
ExcelFileName
As
String,
ByVal
StartRowNumber
As
Integer,
ByVal
StartColNumber
As
Integer)
Dim
i
As
Integer
Dim
j
As
Integer
ReDim
aa(MMSFlexGrid.Rows,
MMSFlexGrid.Cols)
As
Single
For
i
=
1
To
MMSFlexGrid.Rows
For
j
=
1
To
MMSFlexGrid.Cols
aa(i,
j)
=
MMSFlexGrid.TextMatrix(i
-
1,
j
-
1)
Next
j
Next
i
Dim
XlApp
As
New
Excel.Application
Dim
XLWorkBook
As
New
Excel.Workbook
Dim
XLWorkSheet
As
New
Excel.Worksheet
Set
XLWorkBook
=
XlApp.Workbooks.Open(ExcelFileName)
Set
XLWorkSheet
=
XLWorkBook.Sheets(1)
XLWorkSheet.Range(XLWorkSheet.Cells(StartRowNumber,
StartColNumber),
XLWorkSheet.Cells(MMSFlexGrid.Rows
+
1,
MMSFlexGrid.Cols
+
1)).Value
=
aa
'
XlApp.Visible
=
True'如果要显示出来的话使用这行
XlApp.Visible
=
False
'如果不显示出来的话使用这行
XlApp.DisplayAlerts
=
False
'如果不显示出来的话使用这行
XLWorkBook.Save
'如果不显示出来的话使用这行
XlApp.Quit
'如果不显示出来的话使用这行
Erase
aa
End
Sub
使用:
Private
Sub
Command1_Click()
FName
=
App.Path
&
"\99.xls"
MSFlexGridToOldExcel
MSFlexGrid1,
FName,
1,
1
End
Sub
Public
Sub
MSFlexGridToOldExcel(MMSFlexGrid
As
MSFlexGrid,
ByVal
ExcelFileName
As
String,
ByVal
StartRowNumber
As
Integer,
ByVal
StartColNumber
As
Integer)
Dim
i
As
Integer
Dim
j
As
Integer
ReDim
aa(MMSFlexGrid.Rows,
MMSFlexGrid.Cols)
As
Single
For
i
=
1
To
MMSFlexGrid.Rows
For
j
=
1
To
MMSFlexGrid.Cols
aa(i,
j)
=
MMSFlexGrid.TextMatrix(i
-
1,
j
-
1)
Next
j
Next
i
Dim
XlApp
As
New
Excel.Application
Dim
XLWorkBook
As
New
Excel.Workbook
Dim
XLWorkSheet
As
New
Excel.Worksheet
Set
XLWorkBook
=
XlApp.Workbooks.Open(ExcelFileName)
Set
XLWorkSheet
=
XLWorkBook.Sheets(1)
XLWorkSheet.Range(XLWorkSheet.Cells(StartRowNumber,
StartColNumber),
XLWorkSheet.Cells(MMSFlexGrid.Rows
+
1,
MMSFlexGrid.Cols
+
1)).Value
=
aa
'
XlApp.Visible
=
True'如果要显示出来的话使用这行
XlApp.Visible
=
False
'如果不显示出来的话使用这行
XlApp.DisplayAlerts
=
False
'如果不显示出来的话使用这行
XLWorkBook.Save
'如果不显示出来的话使用这行
XlApp.Quit
'如果不显示出来的话使用这行
Erase
aa
End
Sub
使用:
Private
Sub
Command1_Click()
FName
=
App.Path
&
"\99.xls"
MSFlexGridToOldExcel
MSFlexGrid1,
FName,
1,
1
End
Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询