分享
 
 
 

VB中使用EXCEL输出

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Private Sub cmdSwatch_Click()

Dim xls As excel.Application

Dim xlbook As excel.Workbook

'On Error GoTo exlError

Dim i As Integer

If Dir(Text1.Text) <> "" Then '此目录下如有同名文件给出提示,并作相应处理

If MsgBox("文件已存在,是否覆盖!", vbYesNo + vbQuestion, "另存为工程造价文件") = vbNo Then

Exit Sub

Else

Kill (Text1.Text) '删除文件

End If

End If

'************打开工作表***************

Set xls = New excel.Application

xls.Visible = True

Set xlbook = xls.Workbooks.Add

'*********************************

For i = 0 To 14

If Check2(i).Value = vbChecked Then

Select Case i

Case 8

ToExcelJDanJiaSum.ToExcelJDanJiaSum xlbook, xls

Case 9

ToExcelADanJiaSum.ToExcelADanJiaSum xlbook, xls

Case 10

ToExcelCailiao.ToExcelCailiao xlbook, xls

Case 11

ToExcelTsf.ToExcelTsf xlbook, xls

Case 12

ToExcelZgcl.ToExcelZgcl xlbook, xls

End Select

End If

Next

For i = 0 To 6

If Check3(i).Value = vbChecked Then

Select Case i

Case 0

ToExcelMan.ToExcelMan xlbook, xls

Case 1

ToExcelFSD_CL.ToExcelFSD_CL xlbook, xls

Case 2

ToExcelHNT.ToExcelHNT xlbook, xls

Case 3

ToExcelZsf.ToExcelZsf xlbook, xls

Case 4

ToExcelJingChang.ToExcelJingChang xlbook, xls

Case 5

ToExcelJDanJia.ToExcelJDanJia xlbook, xls

Case 6

ToExcelADanJia.ToExcelADanJia xlbook, xls

End Select

End If

Next

xlbook.SaveAs Text1.Text '保存EXCEL文件

'***************************关闭EXCEL对象*******************

If Check1.Value = vbChecked Then

xlbook.Close

xls.Quit

End If

Set xlbook = Nothing

Set xls = Nothing

Exit Sub

'exlError:

' MsgBox Err.Description, vbOKOnly + vbCritical, "警告"

End Sub

Option Explicit

Public Sub ToExcelZgcl(ByRef xlbook, ByRef xls) '输出总工程量

Dim con As New ADODB.Connection

Dim rst_gcl As New ADODB.Recordset

Dim rst_qm As New ADODB.Recordset

'**************************连接数据库****************************************

con.CursorLocation = adUseClient

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strConnection & ";Persist Security Info=False"

con.Open

rst_gcl.Open "zonggcl", con, adOpenKeyset, adLockOptimistic, adCmdTable '打开工程量汇总表

If Not (rst_gcl.BOF And rst_gcl.EOF) Then

rst_gcl.MoveFirst

End If

rst_qm.Open "qianming", con, adOpenKeyset, adLockOptimistic, adCmdTable '打开签名表

rst_qm.MoveFirst

'****************************工作表初使化***********************************

Dim xlsheet As excel.Worksheet

Set xlsheet = xlbook.Sheets.Add '添加一张工作表

xlsheet.Name = "工程量汇总"

xls.ActiveSheet.PageSetup.Orientation = xlLandscape '纸张设置为横向

xlsheet.Columns("a:j").Font.Size = 10

xlsheet.Columns("a:j").VerticalAlignment = xlVAlignCenter '垂直居中

xlsheet.Columns(1).HorizontalAlignment = xlHAlignCenter '1列水平居中对齐

xlsheet.Columns(1).ColumnWidth = 8

xlsheet.Columns(2).HorizontalAlignment = xlHAlignLeft

xlsheet.Columns(2).ColumnWidth = 26

xlsheet.Columns("c:j").HorizontalAlignment = xlHAlignRight

xlsheet.Columns("c:j").ColumnWidth = 10

xlsheet.Columns("c:j").NumberFormatLocal = "0.00_ " '3到10列保留两位小数

'***************************写入标头*************************************

xlsheet.Rows(1).RowHeight = 40

xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, 10)).MergeCells = True

xlsheet.Cells(1, 1).Value = "工程量汇总"

xlsheet.Cells(1, 1).Font.Size = 14

xlsheet.Cells(1, 1).Font.Bold = True

xlsheet.Rows(2).RowHeight = 18

xlsheet.Rows(2).HorizontalAlignment = xlHAlignCenter

xlsheet.Cells(2, 1).Value = "序号"

xlsheet.Cells(2, 2).Value = "工程项目及名称"

xlsheet.Cells(2, 3).Value = "土方开挖(m3)"

xlsheet.Cells(2, 4).Value = "石方开挖(m3)"

xlsheet.Cells(2, 5).Value = "土方回填(m3)"

xlsheet.Cells(2, 6).Value = "洞挖石方(m3)"

xlsheet.Cells(2, 7).Value = "砼浇筑(m3)"

xlsheet.Cells(2, 8).Value = "钢筋制安(t)"

xlsheet.Cells(2, 9).Value = "砌石工程(m3)"

xlsheet.Cells(2, 10).Value = "灌浆工程(m)"

xls.ActiveSheet.PageSetup.PrintTitleRows = "$1:$2" '固定表头

'***************************写入内容*************************

Dim i As Integer

i = 3 'i控制行

Dim j As Integer 'j控制列

Dim countpage As Integer

countpage = 0 '控制页

Do While Not rst_gcl.EOF

xlsheet.Rows(i).RowHeight = 18 '控制行高

For j = 1 To 10

xlsheet.Cells(i, j) = rst_gcl.Fields(j) '将工程理库中的一条记录的第一个字段写入工作表中

Next

'每18行为一页,如果数据超出一页时进行特殊处理

If i > 18 Then

xls.ActiveWindow.SmallScroll Down:=1 '活动窗口内容向下滚动1行

End If

If i Mod 18 = 0 Then

If countpage = 0 Then

xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(i, 10)).Borders.LineStyle = xlContinuous '首页加边框

Else

xlsheet.Range(xlsheet.Cells(23 + (countpage - 1) * 18, 1), xlsheet.Cells(i, 10)).Borders.LineStyle = xlContinuous '中间页加边框

End If

i = i + 2 '加一条空行

'******************************在非尾页写入签名**************************************

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(64) & rst_qm.Fields(0)

xlsheet.Rows(i).RowHeight = 30

i = i + 1 '换行

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(50) & rst_qm.Fields(1)

xlsheet.Rows(i).RowHeight = 15

i = i + 1

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(55) & rst_qm.Fields(2)

xlsheet.Rows(i).RowHeight = 30

'****************************************************************************

xlsheet.HPageBreaks.Add (xlsheet.Rows(i + 1)) '添加分页符

countpage = countpage + 1 '换页

End If

i = i + 1

rst_gcl.MoveNext

Loop

xlsheet.Range(xlsheet.Cells(23 + (countpage - 1) * 18, 1), xlsheet.Cells(i - 1, 10)).Borders.LineStyle = xlContinuous '尾页加边框

i = i + 1 '加入一空行

'*********************************在尾页加签名***************************************

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(64) & rst_qm.Fields(0)

xlsheet.Rows(i).RowHeight = 30

i = i + 1 '换行

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(50) & rst_qm.Fields(1)

xlsheet.Rows(i).RowHeight = 15

i = i + 1

xlsheet.Range(xlsheet.Cells(i, 1), xlsheet.Cells(i, 10)).MergeCells = True

xlsheet.Cells(i, 1).Value = Space(55) & rst_qm.Fields(2)

xlsheet.Rows(i).RowHeight = 30

'***********************************************************************************

xls.ActiveWindow.View = xlPageBreakPreview '分页预览

xls.ActiveWindow.Zoom = 100

If con.State = adStateOpen Then

rst_gcl.Close

rst_qm.Close

Set rst_gcl = Nothing

Set rst_qm = Nothing

con.Close

Set con = Nothing

End If

Set xlsheet = Nothing

End Sub

Option Explicit

Public Sub ToExcelTsf(ByRef xlbook, ByRef xls)

Dim con As New ADODB.Connection

Dim rst_tsf As New ADODB.Recordset

Dim rst_qm As New ADODB.Recordset

'**********************************连接数据库************************

con.CursorLocation = adUseClient

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strConnection & ";Persist Security Info=False"

con.Open

rst_tsf.Open "tdefeiyong", con, adOpenKeyset, adLockOptimistic, adCmdTable

If Not (rst_tsf.BOF And rst_tsf.EOF) Then

rst_tsf.MoveFirst

End If

rst_qm.Open "qianming", con, adOpenKeyset, adLockOptimistic, adCmdTable

rst_qm.MoveFirst

'*********************************工作表初使化**********************************

Dim xlsheet As excel.Worksheet

Set xlsheet = xlbook.Sheets.Add

xlsheet.Name = "机械台时、组时费汇总表"

xlsheet.Columns(1).ColumnWidth = 5

xlsheet.Columns(2).ColumnWidth = 20

xlsheet.Columns(3).ColumnWidth = 7

xlsheet.Columns(4).ColumnWidth = 7

xlsheet.Columns(5).ColumnWidth = 7

xlsheet.Columns(6).ColumnWidth = 7

xlsheet.Columns(7).ColumnWidth = 7

xlsheet.Columns(8).ColumnWidth = 7

xlsheet.Columns(9).ColumnWidth = 7

xlsheet.Columns("A:I").Font.Size = 9

xlsheet.Columns("A:I").VerticalAlignment = xlVAlignCenter '垂直居中

xlsheet.Columns(1).HorizontalAlignment = xlHAlignCenter '1列水平居中对齐

xlsheet.Columns(2).HorizontalAlignment = xlHAlignLeft '2列水平左对齐

'******************************写入标头************************************

xlsheet.Rows(1).RowHeight = 35

xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, 9)).MergeCells = True

xlsheet.Cells(1, 1).Font.Size = 14

xlsheet.Cells(1, 1).Font.Bold = True

xlsheet.Cells(1, 1).Value = "机械台时、组时费汇总表"

xlsheet.Cells(2, 9).Value = "单位:元"

xlsheet.Range(xlsheet.Cells(3, 1), xlsheet.Cells(5, 1)).MergeCells = True

xlsheet.Cells(3, 1).Value = "编号"

xlsheet.Range(xlsheet.Cells(3, 2), xlsheet.Cells(5, 2)).MergeCells = True

xlsheet.Cells(3, 2).Value = "机械名称"

xlsheet.Range(xlsheet.Cells(3, 3), xlsheet.Cells(5, 3)).MergeCells = True

xlsheet.Cells(3, 3).Value = "台时费"

xlsheet.Range(xlsheet.Cells(3, 4), xlsheet.Cells(3, 9)).MergeCells = True

xlsheet.Cells(3, 4).Value = "其 中"

xlsheet.Range(xlsheet.Cells(3, 3), xlsheet.Cells(5, 3)).MergeCells = True

xlsheet.Cells(3, 3).Value = "台时费"

xlsheet.Range(xlsheet.Cells(4, 4), xlsheet.Cells(5, 4)).MergeCells = True

xlsheet.Cells(4, 4).Value = "折旧费"

xlsheet.Range(xlsheet.Cells(4, 5), xlsheet.Cells(5, 5)).MergeCells = True

xlsheet.Cells(4, 5).Value = "修理替换费"

xlsheet.Range(xlsheet.Cells(4, 6), xlsheet.Cells(5, 6)).MergeCells = True

xlsheet.Cells(4, 6).Value = "安拆费"

xlsheet.Range(xlsheet.Cells(4, 7), xlsheet.Cells(5, 7)).MergeCells = True

xlsheet.Cells(4, 7).Value = "人工费"

xlsheet.Range(xlsheet.Cells(4, 8), xlsheet.Cells(5, 8)).MergeCells = True

xlsheet.Cells(4, 8).Value = "燃料费"

xlsheet.Range(xlsheet.Cells(4, 9), xlsheet.Cells(5, 9)).MergeCells = True

xlsheet.Cells(4, 9).Value = "其他费"

xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(5, 9)).HorizontalAlignment = xlHAlignCenter

xls.ActiveSheet.PageSetup.PrintTitleRows = "$1:$5" '固定表头

'****************************************写入内容*************************************

Dim i As Integer

i = 6

Do While Not rst_tsf.EOF

xlsheet.Cells(i, 1).Value = rst_tsf.Fields("nn")

xlsheet.Cells(i, 2).Value = rst_tsf.Fields("name")

xlsheet.Cells(i, 3).Value = rst_tsf.Fields("price")

xlsheet.Cells(i, 4).Value = rst_tsf.Fields("zhejiu")

xlsheet.Cells(i, 5).Value = rst_tsf.Fields("xiuli")

xlsheet.Cells(i, 6).Value = rst_tsf.Fields("anchai")

xlsheet.Cells(i, 7).Value = rst_tsf.Fields("rengong")

xlsheet.Cells(i, 8).Value = rst_tsf.Fields("dongli")

xlsheet.Cells(i, 9).Value = rst_tsf.Fields("qita")

If i > 22 Then

xls.ActiveWindow.SmallScroll Down:=1 '活动窗口内容向下滚动1行

End If

i = i + 1

rst_tsf.MoveNext

Loop

xlsheet.Range(xlsheet.Cells(6, 3), xlsheet.Cells(i - 1, 9)).NumberFormatLocal = "0.00_ " '保留两位小数

'*********************************添加边框**********************************

xlsheet.Range(xlsheet.Cells(3, 1), xlsheet.Cells(i - 1, 9)).Borders.LineStyle = xlContinuous

'******************************************************************************

xls.ActiveSheet.PageSetup.BottomMargin = Application.InchesToPoints(2.2) '设置下侧面边距

xls.ActiveSheet.PageSetup.FooterMargin = Application.InchesToPoints(1) '设置页脚高

xls.ActiveSheet.PageSetup.CenterFooter = "&10" & rst_qm.Fields(0) & Chr(10) & Chr(10) & rst_qm.Fields(1) & Chr(10) & Chr(10) & rst_qm.Fields(2) '加页脚

xls.ActiveWindow.View = xlPageBreakPreview '分页预览

xls.ActiveWindow.Zoom = 100

'***************************关闭记录集*******************

If con.State = adStateOpen Then

rst_tsf.Close

rst_qm.Close

Set rst_tsf = Nothing

Set rst_qm = Nothing

con.Close

Set con = Nothing

End If

Set xlsheet = Nothing

End Sub

精彩的后续

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有