| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 629 人关注过本帖
标题:请教VB msflexgrid控件导出到excel表格问题
只看楼主 加入收藏
芝诺
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-12-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
请教VB msflexgrid控件导出到excel表格问题
界面有两个msflexgrid表格,点击command控件(只有一个)输出到excel表格。问题是:1.excel中两个表格是相互独立的,我想做到一个excel里面。2.excel表格无法调节适合大小。刚输来的有点凌乱。请教大神!!!
搜索更多相关主题的帖子: excel表格 command 
2014-12-19 11:22
snrtjat
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:山那边 海尽头
等 级:贵宾
威 望:22
帖 子:1115
专家分:7025
注 册:2013-1-21
收藏
得分:20 
你的问题描述,我不是很明白。
只能给你一个实例,你自己试着去调试吧!
程序代码:
Private Sub Command3_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim j As Integer

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open(App.Path & "\text.xlsx")
Set xlSheet = xlBook.Worksheets(1)
Sheets("sheet1").Select
Sheets("sheet1").Name = "New Trial"
For i = 0 To DataGrid1.Columns.Count - 1
    xlSheet.Cells(1, i + 1) = DataGrid1.Columns.Item(i).Caption
Next i
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
    For j = 0 To Adodc1.Recordset.Fields.Count - 1
        xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j)
    Next j
    Adodc1.Recordset.MoveNext
Next i

Set xlSheet = xlBook.Worksheets(2)
Sheets("sheet2").Select
Sheets("sheet2").Name = "Underway Trial"
For i = 0 To DataGrid2.Columns.Count - 1
    xlSheet.Cells(1, i + 1) = DataGrid2.Columns.Item(i).Caption
Next i
Adodc2.Recordset.MoveFirst
For i = 0 To Adodc2.Recordset.RecordCount - 1
    For j = 0 To Adodc2.Recordset.Fields.Count - 1
        xlSheet.Cells(i + 2, j + 1) = Adodc2.Recordset(j)
    Next j
    Adodc2.Recordset.MoveNext
Next i
End Sub

不怕错误,只怕知错不改.
2014-12-20 15:55
snrtjat
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:山那边 海尽头
等 级:贵宾
威 望:22
帖 子:1115
专家分:7025
注 册:2013-1-21
收藏
得分:0 
你的问题描述,我不是很明白。
只能给你一个实例,你自己试着去调试吧!
程序代码:
Private Sub Command3_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim j As Integer

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Open(App.Path & "\text.xlsx")
Set xlSheet = xlBook.Worksheets(1)
Sheets("sheet1").Select
Sheets("sheet1").Name = "New Trial"
For i = 0 To DataGrid1.Columns.Count - 1
    xlSheet.Cells(1, i + 1) = DataGrid1.Columns.Item(i).Caption
Next i
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
    For j = 0 To Adodc1.Recordset.Fields.Count - 1
        xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j)
    Next j
    Adodc1.Recordset.MoveNext
Next i

Set xlSheet = xlBook.Worksheets(2)
Sheets("sheet2").Select
Sheets("sheet2").Name = "Underway Trial"
For i = 0 To DataGrid2.Columns.Count - 1
    xlSheet.Cells(1, i + 1) = DataGrid2.Columns.Item(i).Caption
Next i
Adodc2.Recordset.MoveFirst
For i = 0 To Adodc2.Recordset.RecordCount - 1
    For j = 0 To Adodc2.Recordset.Fields.Count - 1
        xlSheet.Cells(i + 2, j + 1) = Adodc2.Recordset(j)
    Next j
    Adodc2.Recordset.MoveNext
Next i
End Sub

不怕错误,只怕知错不改.
2014-12-20 15:55
芝诺
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-12-19
收藏
得分:0 
Dim a   As Integer
       Dim b   As Integer
       Dim d   As Integer
      On Error GoTo Ert
       Me.MousePointer = 11
       Dim Excelapp   As Excel.Application
       Set Excelapp = New Excel.Application
       On Error Resume Next
       DoEvents
       Excelapp.SheetsInNewWorkbook = 1
       Excelapp.Workbooks.Add
       Excelapp.ActiveSheet.Cells(1, 3) = s
       Excelapp.Range("C1").Select
       Excelapp.Selection.Font.FontStyle = "Bold"
       Excelapp.Selection.Font.Size = 16
       Excelapp.Caption = "输出表格"
With MSFlexGrid1
               d = MSFlexGrid1.Rows
               For a = 0 To d - 1
                       For b = 0 To 5
                             DoEvents
                             Excelapp.ActiveSheet.Cells(3 + a, b + 1) = "'" & .TextMatrix(a, b)
                       Next b
               Next a
       End With
       Me.MousePointer = 0
       Excelapp.Visible = True
      ' Excelapp.Sheets.PrintPreview
      Excelapp.Cells.EntireColumn.AutoFit
Ert:
       If Not (Excelapp Is Nothing) Then
              Excelapp.Quit
      End If
 Dim e   As String
       Dim f   As Integer
       Dim m   As Integer
       Dim n   As Integer
       On Error GoTo Ert2
       Me.MousePointer = 11
       Dim Excelapp2   As Excel.Application
       Set Excelapp2 = New Excel.Application
       On Error Resume Next
       DoEvents
       Excelapp2.SheetsInNewWorkbook = 1
       Excelapp2.Workbooks.Add
       Excelapp2.ActiveSheet.Cells(1, 3) = e
       Excelapp2.Range("C1").Select
       Excelapp2.Selection.Font.FontStyle = "Bold"
       Excelapp2.Selection.Font.Size = 16
       Excelapp2.Caption = "合计表格"
With MSFlexGrid2
               n = MSFlexGrid2.Rows
               For f = 0 To n - 1
                       For m = 0 To 5
                             DoEvents
                             Excelapp2.ActiveSheet.Cells(3 + f, m + 1) = "'" & .TextMatrix(f, m)
                              
                       Next m
               Next f
       End With
       Me.MousePointer = 0
       Excelapp2.Visible = True
      ' Excelapp2.Sheets.PrintPreview
       Excelapp2.Cells.EntireColumn.AutoFit
Ert2:
      If Not (Excelapp2 Is Nothing) Then
               Excelapp2.Quit
我写的是这样  出来的Excel表格是两个 我希望写到一个表格里面

谢谢你
2014-12-20 22:54
快速回复:请教VB msflexgrid控件导出到excel表格问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028632 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved