VB 在不安装 excel 的环境下 生成 xls 文件 ??

解决方案 »

  1.   

     你只要加入EXCEL控件就可以操作EXCEL了。
      

  2.   

    应该是没办法,除非你自已写个Excel 文件解码器
      

  3.   

    当然可以了,有BIFF8格式的(office2000-office2003),完全通过VB代码来创建Excel文件.
    组件是老外写的,但老外的不支持Unicode,我重新根据BIFF8格式,重写了代码
    偶的联系:hongzhen#mail.nbptt.zj.cn
    示例:Private Sub sfWriteXls_Table(ByVal poListView As Object, ByVal pvTxt As Variant, ByVal psFile As String, ByVal pvData As Variant)
    Dim Wbook As ExcelWorkbook
    Dim Wsheet As ExcelWorksheet
    'Dim row           As ExcelRow
    Dim cell          As ExcelCell
    Dim style As ExcelCellStyleDim lRow As Long
    Dim j As Long, i As LongDim sDataType As String
      
      Set Wbook = Excel8.CreateExcelWorkbook
      Wbook.SetDefaultFont "宋体", 10
      
      Call Wbook.CreateSheet("Sheet1")
      Set Wsheet = Wbook.GetSheet("Sheet1")
      Wbook.SetActiveSheet "Sheet1"
      
      With Wsheet.PrintSetup
        .CenterHorizontally = True '水平居中
        .HeaderMargin = 0.5        ' inches
        .TopMargin = 1.5
        .FitPagesWide = 1
        .FitPagesHigh = 1
        .BottomMargin = 1.5
        .FooterMargin = 0.5
        .Orientation = oPortrait
        .ScaleFactor = 100         '打印缩放比例
      End With
    ''  '[--------------------
    ''  With Wsheet.Header
    ''    .StartLeftSection
    ''    .SetFont "宋体", fsItalic
    ''    .SetFontSize 10
    ''    .AddText "页眉 左边"
    ''    .StartCenterSection
    ''    .SetFont "宋体", fsBold
    ''    .SetFontSize 20
    ''    .AddText "页眉 中间"
    ''    .StartRightSection
    ''    .SetFont "宋体", fsBoldItalic
    ''    .SetFontSize 10
    ''    .AddText "页眉 右边"
    ''  End With
    ''  With Wsheet.Footer
    ''    .StartLeftSection
    ''    .SetFont "宋体", fsItalic
    ''    .SetFontSize 10
    ''    .AddText "页脚 左边"
    ''    .StartCenterSection
    ''    .SetFont "宋体", fsBold
    ''    .SetFontSize 20
    ''    .AddText "页脚 中间"
    ''    .StartRightSection
    ''    .SetFont "宋体", fsBoldItalic
    ''    .SetFontSize 10
    ''    .AddText "页脚 右边"
    ''  End With
    ''  '--------------------]
        
        Set style = Excel8.CreateExcelCellStyle
        '列
        For j = 1 To poListView.ColumnHeaders.Count
          'Set style = sfWriteXls_Style(poListView, j, , , sDataType)
          Wsheet.SetColumnStyle j, j, poListView.ColumnHeaders(j).Width * mdXs ', style
        Next
        
        '标题
        lRow = 0
        For i = 0 To UBound(pvTxt)
            Set style = sfWriteXls_Style(poListView, 0, 1, pvTxt(i))
            lRow = lRow + 1
            Wsheet.AddCell i, lRow, Split(pvTxt(i), ",")(0), style
            
            If UBound(Split(pvTxt(i), ",")) >= 4 Then
               If Val(Split(pvTxt(i), ",")(4)) = 0 Then
                  Wsheet.GetRow(lRow).Height = 25
               Else
                  Wsheet.GetRow(lRow).Height = Val(Split(pvTxt(i), ",")(4))
               End If
            Else
               Wsheet.GetRow(lRow).Height = 25
            End If
            
            Wsheet.MergeCells 1, lRow, poListView.ColumnHeaders.Count, i  '行合并
        Next
        
        '表头
        lRow = lRow + 1
        Set style = sfWriteXls_Style(poListView, j, 2)
        For j = 1 To poListView.ColumnHeaders.Count
            Wsheet.AddCell j, lRow, poListView.ColumnHeaders(j).Text, style
        Next
    '    If poListView.listitems.Count > 0 Then
    '       Wsheet.GetRow(lRow).Height = poListView.listitems(1).Height * mdXs
    '    End If
        
        '行
        If Not (IsNull(pvData) Or IsEmpty(pvData)) Then
           Set style = Excel8.CreateExcelCellStyle
           With style
             .LeftLineStyle = xfbtThin
             .LeftLineColour = cBlack
             
             .TopLineStyle = xfbtThin
             .TopLineColour = cBlack
             
             .RightLineStyle = xfbtThin
             .RightLineColour = cBlack
             
             .BottomLineStyle = xfbtThin
             .BottomLineColour = cBlack
          End With
          Wsheet.AddTable 1, lRow + 1, style, pvData
        End If
        
        Wbook.Save psFile
    End Sub
      

  4.   

    使用CSV格式输出文件即可:1:CSV文件就是文本文件。
    2:在安装有OFFICE的系统上,CSV格式被默认使用EXCEL打开
    3:CSV只能保存内容,无法保存格式
    4:文件格式:逗号分割字段,回车分割记录举个例子:
    将一下内容粘贴到一个新建文本文件内姓名,性别,年龄
    老大,男,30
    老二,男,29保存文本文件,并改名为:TEXT.CSV
    最后,你双击该文件试试。
      

  5.   

    还有种方法用vsflexgrid控件,它有个.save方法,可以生成xls