posts - 56, comments - 54, trackbacks - 0, articles - 4
   ::  ::  :: 联系 :: 聚合  :: 管理

VB中动态生成Excel文件

Posted on 2006-05-24 11:28 Terry的Blog 阅读(2604) 评论(1)  编辑  收藏 所属分类: 其他(工具软件...)

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    
    Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
    Set xlBook = xlApp.Workbooks.Add    'xlApp.Workbooks.Open("文件名") '打开已经存在的EXCEL工件簿文件
    xlApp.Visible = False '设置EXCEL对象可见(或不可见)
    Set xlSheet = xlBook.Worksheets(1) '设置活动工作表
    xlSheet.Activate
    xlSheet.Range("C3").Value = "1"
    xlSheet.Cells(1, 1) = "test中文" '给单元格(row,col)赋值
   
    ' 画边框线
    xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(2, 2)).Borders.LineStyle = xlContinuous
   
    xlSheet.Rows(1).HorizontalAlignment = xlVAlignCenter '左右居中
    xlSheet.Rows(1).VerticalAlignment = xlVAlignCenter '上下居中

    xlSheet.Cells(iStartRow + idx, 2).HorizontalAlignment = xlVAlignCenter '左右居中
    
    '设置指定列的宽度(单位:字符个数)
    xlApp.ActiveSheet.Columns(1).ColumnWidth = 15

    '设置指定行的高度(单位:磅)
    xlApp.ActiveSheet.Rows(1).RowHeight = 1 / 0.035
   
    '设置字体
    'xlApp.ActiveSheet.Cells(1, 1).Font.Name = "黑体"

    '设置字体大小
    xlApp.ActiveSheet.Cells(1, 1).Font.Size = 25
   
    '设置整列字体为粗体
    xlApp.ActiveSheet.Columns(1).Font.Bold = True
   
    'xlBook.SaveAs ("C:\Case1.xls")
   
    'xlSheet.PrintPreview (True)
   
    xlApp.Visible = True '显示文件
   
    'xlSheet.PrintOut '打印工作表

    If Not (xlApp Is Nothing) Then
        
        xlBook.Close (True) '关闭工作簿
       
        xlApp.Quit '必须结束EXCEL对象
        Set xlApp = Nothing '释放xlApp对象
    End If


评论

# re: VB中动态生成Excel文件  回复  更多评论   

2014-09-15 10:06 by rwer
wrwer

只有注册用户登录后才能发表评论。


网站导航: