|
楼主 |
发表于 2022-3-27 09:47
|
显示全部楼层
ACCESS保存文件.rar
(144.55 KB, 下载次数: 96)
ACCESS中保存图片 或者文件
- Sub A_保存()
- Dim CN As ADODB.Connection
- Dim RS As ADODB.Recordset
- Set CN = New ADODB.Connection
- Str_coon = "Provider=Microsoft.ACE.OLEDB.12.0;;data source=" & ThisWorkbook.Path & "\Database.mdb"
- CN.ConnectionString = Str_coon
- CN.Open
- Set RS = New ADODB.Recordset
- RS.Open "select 学生编号,相片 from [table1]", CN, adOpenStatic, adLockOptimistic
- Set FSO = CreateObject("Scripting.FileSystemObject")
- Dim mst As ADODB.Stream
- Set SHX = Worksheets("登记表")
- For I = 2 To SHX.Range("A65536").End(3).Row
- RS.AddNew
- RS.Fields("学生编号").Value = SHX.Cells(I, 1).Value
- Rem 判断文件是否存在
- Path = ThisWorkbook.Path & "\原始图片" & SHX.Cells(I, 1).Value & ".png"
- If FSO.FileExists(Path) = True Then
- Set mst = New ADODB.Stream
- mst.Type = adTypeBinary
- mst.Open
- mst.LoadFromFile Path
- RS.Fields("相片").Value = mst.Read
- Else
- RS.Fields("相片").Value = Null
- End If
- RS.Update
- Next
- RS.Close
- CN.Close
- MsgBox "已添加", , "北极狐QQ:14885553"
- End Sub
复制代码- Sub B_下载()
- Path = ThisWorkbook.Path & "\下载"
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(Path) = False Then
- MkDir Path '//创建文件夹
- End If
- Dim CN, RS
- Set CN = New ADODB.Connection
- Str_coon = "Provider=Microsoft.ACE.OLEDB.12.0;;data source=" & ThisWorkbook.Path & "\Database.mdb"
- CN.ConnectionString = Str_coon
- CN.Open
- Set RS = New ADODB.Recordset
- StrSQL = "select 学生编号,相片 from [table1]"
- RS.Open StrSQL, CN, adOpenKeyset, adLockBatchOptimistic
- Set SHX = Worksheets("登记表")
- SHX.Range("A2:B65536").ClearContents
- I = 1
- Do While Not RS.EOF
- I = I + 1
- SHX.Cells(I, 1).Value = RS.Fields("学生编号").Value '显示第一标题下每一条的内容
- If IsNull(RS.Fields("相片").Value) = True Then
- SHX.Cells(I, 2).Value = "无照片"
- Else
- SHX.Cells(I, 2).Value = RS.Fields("学生编号").Value
- Rem 写入文件
- Set mst = New ADODB.Stream
- mst.Type = adTypeBinary
- mst.Open
- mst.Write RS.Fields("相片").Value
- mst.SaveToFile ThisWorkbook.Path & "\下载" & SHX.Cells(I, 1).Value & ".PNG", adSaveCreateOverWrite '保存文件,adSaveCreateOverWrite为覆盖以存在文件
- End If
- RS.MoveNext
- Loop
- RS.Close
- CN.Close
- MsgBox "已显示,并下载", , "北极狐QQ:14885553"
- End Sub
复制代码
|
|