如何将sql结果填充到excel中?

zcg 2009-09-15 01:51:17
想将以下sql语句查询结果填充到一个已经画好的excel报表"aa.xls""sheet1"中的指定单元格内如"c1"中
Select sum(金额) as 总额 from kk where 日期<2009.9.1
请指教
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zcg 2009-09-15
  • 打赏
  • 举报
回复
感谢,给分
xunis 2009-09-15
  • 打赏
  • 举报
回复
Imports System.Data
Imports System.Data.Common
Imports System.Windows.Forms.DateTimePicker
Imports System.Configuration
Imports System.Reflection.MethodBase
Imports System.Text
Imports System.Data.SqlClient '引用數據庫服務
Imports System.Reflection
Imports Microsoft.Office.Interop


Public Class frmPrint

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strSql As String = ""
Dim ds As New DataSet
Dim strMsg As String = ""
'从第五行开始打印正文
Dim i As Integer = 4
Dim j As Integer

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try

'清空dataset
ds.Clear()
'创建Excel 对象
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

'打印报表头
xlSheet.Range("A1:G1").Select()
With xlSheet.Range("A1:G1").Select
xlSheet.Range("A1:G1").MergeCells = True
'xlSheet.Range("A1:G1").HorizontalAlignment = xlCenter
'xlSheet.Range("A1:G1").VerticalAlignment = .xlCenter
End With
xlSheet.Range("A1:G1").Value = "員工信息报表"

'打印页眉
xlSheet.Cells(3, 1) = "员工编号"
xlSheet.Cells(3, 2) = "员工名称"
xlSheet.Cells(3, 3) = "性别"
xlSheet.Cells(3, 4) = "身份证号码"
xlSheet.Cells(3, 5) = "出生日期"
xlSheet.Cells(3, 6) = "年龄"
xlSheet.Cells(3, 7) = "部门编号"
xlSheet.Cells(3, 8) = "部门名称"
xlSheet.Cells(3, 9) = "上级部门"


If chkChooes.Checked = True And Training.strSql <> "" Then
strSql = Training.strSql
Else
strSql = " select top 10 * from VWEmployees where EmpBirthday between '" & cmbBirS.Value.Date & "' And '" & cmbBirE.Value.Date & "' "
End If
ds = GetDataSet(strSql)

If chkChooes.Checked = False Then
xlSheet.Cells(2, 2) = "打印 " & cmbBirS.Value.ToString("yyyy-mm-dd") & " 到 " & cmbBirE.Value.ToString("yyyy-mm-dd")
End If


'循环打印报表内容
For Me.j = 0 To ds.Tables(0).Rows.Count - 1
With xlSheet
.Cells(i, 1).NumberFormatLocal = "@"
.Cells(i, 4).NumberFormatLocal = "@"
.Cells(i, 5).NumberFormatLocal = "@"
.Cells(i, 1) = ds.Tables(0).Rows(j)("EmpID").ToString()
.Cells(i, 2) = ds.Tables(0).Rows(j)("EmpName").ToString()
If ds.Tables(0).Rows(j)("EmpSex").ToString().Equals("1") Then
.Cells(i, 3) = "男"
Else
.Cells(i, 3) = "女"
End If
.Cells(i, 4) = ds.Tables(0).Rows(j)("EmpPID").ToString()
.Cells(i, 5) = ds.Tables(0).Rows(j)("EmpBirthday").ToString()
.Cells(i, 6) = ds.Tables(0).Rows(j)("EmpAge").ToString()
.Cells(i, 7) = ds.Tables(0).Rows(j)("DepID").ToString()
.Cells(i, 8) = ds.Tables(0).Rows(j)("DepName").ToString()
.Cells(i, 9) = ds.Tables(0).Rows(j)("PDepID").ToString()
End With
i = i + 1
Next

'打印报表尾
i = i + 3
xlSheet.Cells(i, 6) = "制表人"
xlSheet.Cells(i, 7) = "XXXXXX"

'对象释放
xlSheet.Application.Visible = True
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing

Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub


Private Sub btnGoBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoBack.Click
Training.Visible = True
Me.Close()
End Sub

Private Sub frmPrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
chkChooes.Checked = False
cmbBirS.Value = "1980-01-01"
cmbBirE.Value = "1990-01-01"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim y7 As String = "1.00 "
Dim y8 As String = "1.6 "
MsgBox((Convert.ToInt16(y7) * Convert.ToInt16(y8)).ToString())
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

End Class

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧