我想让vb直接调用excel生成的文件并生成他的预览。请问能不能实现。

解决方案 »

  1.   

    我的意思是用shell或ShellExecute类的函数调用。
      

  2.   

    Set EXCELApp = CreateObject("Excel.application")
    Set EXCELWorkBook = EXCELApp.Workbooks.Open("要调用的文件路径及名称")
      

  3.   

    '存字段长度值
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet    Set xlApp = CreateObject("Excel.Application")
        Set xlBook = xlApp.Workbooks.Add
        Set xlSheet = xlBook.Worksheets(1)    With Rs_Dzgl_Receipt
            If .RecordCount < 1 Then
                MsgBox ("没有记录!")
                Exit Sub
            End If
            xlSheet.Cells(1, 4).Value = .Fields("bt")
            xlSheet.Cells(2, 1).Value = .Fields("invoice")
            xlSheet.Cells(2, 9).Value = .Fields("packdate")
            xlSheet.Cells(3, 1).Value = .Fields("")
                            
            '合并单元格
            Dim nIcol As Integer
            
            xlSheet.Range(xlSheet.Cells(3, 1), xlSheet.Cells(5, 9)).Select
                With xlApp.Selection
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .WrapText = False
                    .Orientation = 0
                    .AddIndent = False
                    .ShrinkToFit = False
                   .MergeCells = True
                End With
                
            xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 9)).Select
                With xlApp.Selection
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .WrapText = False
                    .Orientation = 0
                    .AddIndent = False
                    .ShrinkToFit = False
                   .MergeCells = True
                End With
            '网格线
            With xlSheet
                .Range(.Cells(1, 1), .Cells(1, 9)).Font.Name = "黑体"
                '设标题为黑体字
                .Range(.Cells(1, 1), .Cells(1, 9)).Font.Bold = True
                '标题字体加粗
                .Range(.Cells(1, 1), .Cells(1, 9)).Borders.LineStyle = xlContinuous
                '设表格边框样式
            End With
            
            '显示表格
            Dim ExclFileName As String
            ExclFileName = App.Path & "\箱单" & Text1(1).Text & ".xls"
            If Dir(ExclFileName) <> "" Then
                Kill ExclFileName
            End If
            xlSheet.SaveAs (ExclFileName)
            xlApp.Application.Visible = True
            '交还控制给Excel
            xlSheet.PrintPreview
           ' xlApp.Application.Quit
           ' xlApp.Quit
        End With
      

  4.   

    我的意思是说:我已经把查询出来的数据转换为e;\xx.xls现在我想把xx.xls调用直接生成xx.xls的excel预览,能实现吗?
      

  5.   

    Option ExplicitPrivate Sub Command1_Click()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Set xlApp = New Excel.Application
        Set xlBook = GetObject("C:\1.xls")
        xlApp.Visible = True
        xlBook.Windows(1).Visible = True
            
      
        Set xlApp = Nothing
        Set xlBook = Nothing
        End Sub
      

  6.   

    参照设定--选中microsoft excel  9.0 object library
      

  7.   

    先参照设定
    再按上面哪位的打代码,再加一句xlBook.PrintPreviewPrivate Sub Command1_Click()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Set xlApp = New Excel.Application
        Set xlBook = GetObject("C:\1.xls")
        xlApp.Visible = True
        xlBook.Windows(1).Visible = True
        
        xlBook.PrintPreview    ' add
      
        Set xlApp = Nothing
        Set xlBook = Nothing
    End Sub
      

  8.   

    参照设定  再加一句      xlBook.PrintPreviewPrivate Sub Command1_Click()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Set xlApp = New Excel.Application
        Set xlBook = GetObject("C:\1.xls")
        xlApp.Visible = True
        xlBook.Windows(1).Visible = True
            
        xlBook.PrintPreview    'add    Set xlApp = Nothing
        Set xlBook = Nothing
        End Sub
      

  9.   


    工程--->引用--->microsoft excel  9.0 object library