|
本帖最后由 wcj109 于 2023-2-22 21:56 编辑
Dim cnn, rs, rs1, tab1, mycmd, nam '模块级变量
Sub 连接数据库()‘利用表格作为数据库
Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set
rs1 = CreateObject("ADODB.Recordset")
Set mycmd = CreateObject("ADODB.command")
' If Dir(ThisWorkbook.Path & "\" & "*.mdb") = "答题查询.mdb" Then
' my = ThisWorkbook.Path & "\答题查询.mdb " '连接数据库 ace.OLEDB.12.0 jet.OLEDB.4.0";Extended Properties=Excel 8.0;
' Else
' my = "d:/数据库/答题查询.mdb"
' End If
my = ThisWorkbook.FullName '完整路径
tab1 = "[" & Sheet3.Range("F2") & "$]" '表格名称
If Application.Version < 12 Or InStr(Replace(UCase(Application.Caption), UCase(Application.ActiveWorkbook.Name), ""), "WPS") > 0 Then 'Application.Caption标题栏文本
cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & my ' & ";hdr=yes;imex=1" '表格连接
' cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" & my '连接数据库 ace.OLEDB.12.0 jet.OLEDB.4.0’access作为数据库
Else
cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & my '& ";hdr=yes;imex=1" '表格连接
' cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & my
End If
End Sub
‘写入工作表
Sub 写入表格1()
b = Range("b60000").End(xlUp).Row
h = Range("iv3").End(xlToLeft).Column
' On Error Resume Next
If rs.RecordCount > 0 Then
rs.MoveFirst
Range(Range("a3"), Cells(b, h)).Clear 'Contents
End If
If Not rs.EOF Then
' Sheet1.[b2].CopyFromRecordset rs
[a3] = "序号"
For i = 0 To rs.Fields.Count - 1
Cells(3, i + 2) = rs.Fields(i).Name '表头名称
Next
For i = 1 To rs.RecordCount
For j = 0 To rs.Fields.Count - 1
Cells(i + 3, j + 2) = rs.Fields(j) '添加行
Next j
rs.MoveNext
Next i
Columns("b:b").NumberFormatLocal = "yyyy/m/d" '设置b列为日期格式
' Sheet3.Range("e:e").NumberFormatLocal = "@"
'1\添加序号
i = 1
Do
Range("a" & i + 3) = i
i = i + 1
Loop While Range("b" & i + 3) <> ""
'2\设置表格格式
' Sheet1.Activate
Call 设置表格格式
Else
Range("a4:ag65355").Clear
MsgBox "没有查到"
End If
End Sub
Sub 设置表格格式()
row1 = Range("a63535").End(xlUp).Row '表格总的行数
col = Range("a3").End(xlToRight).Column '表格总的列数
bi = 3 '标题行行号,颜色,蓝色
qi = 4 '单元格区域起始行号
With Range(Cells(qi, 1), Cells(row1, col)) '数据区域大概设置
'1、单元格边框、颜色
' .Borders.LineStyle = 0 '去边框3行1列起
.Borders.LineStyle = 1
' .Borders.ColorIndex = 5 '边框颜色
'2、调整单元格的字体及颜色大小
.Font.Name = "宋体" '"仿宋体"
' Range("a3:q" & i).Font.FontStyle = "Bold"
.Font.Bold = False '是否粗体
.Font.ColorIndex = 1
.Font.Size = 10
'3调整行列的宽高度及换行
.WrapText = True '自动换行
.Rows.AutoFit '自动调整行高
' .Columns.AutoFit '自动调整列宽
End With
With Range(Cells(bi, 1), Cells(bi, col))
'4、调整标题行的宽度和字体加深
.Interior.ColorIndex = 37 '调整单元格颜色,第二行蓝色
.Font.Bold = True '第二行字体加深
.Font.Size = 10
.Borders.LineStyle = 1
.RowHeight = 26 '调整第二行的行高,columnwidth=3列宽
' .Columns.AutoFit '自动调整列宽
End With
'5、单独调整列宽度和字体加深
Range(Cells(bi, 1), Cells(row1, col)).Columns.AutoFit '自动调整列宽
' Range("d:d").ColumnWidth = 25 'b列列宽设置
' Range("b:b").ColumnWidth = 9.13
' Range("c:c").ColumnWidth = 9
' Range("e:e").ColumnWidth = 13
' Range("ah:ah").Font.Size = 9
End Sub
sub 查询()'利用sql语句查谒
Call 连接数据库
sql="select * from " & tab1
rs.Open Sql, cnn, 1, 3
Sheet3.Activate'写入sheet3表格
Call 写入表格1
rs.Close
cnn.Close
Set cnn = Nothing
end sub
|
|