当前位置:网站首页>Excel exports data to SQL and pictures to folder through macro | VBA
Excel exports data to SQL and pictures to folder through macro | VBA
2022-06-25 04:39:00 【zepcjsj0801】
A project at the end of these days , At the end of the project, the customer needs to import data , One 70 many M Of excel, More than 5000 pieces of data , The main capacity is due to excel Each data in the corresponds to a picture , Lead to excel Too big , And the import system does not display . Grope for a few days , Think of a way like this .
use excel Macro splice export for sql sentence . Auto export excel Pictures to an external folder , Then upload it to the server .
Function clearChar(no As String)
no = Replace(no, Chr(10), "")
no = Replace(no, Chr(13), "")
no = Replace(no, " ", "")
no = Replace(no, "'", "\'")
clearChar = no
End Function
Sub saveSql()
Dim objSht As Object
Dim i, strTemp
Dim today As String
today = Year(Date) & Month(Date) & Day(Date)
Open ThisWorkbook.Path & "\sql.txt" For Output As #1 ' Export data storage location ,excel Under the directory
Print #1, "insert into sb_shangbiao(catid,status,no,time1,sbstate,title,sbcat,thumb,description,sbshiyong,price,sbcat2)values"
Dim sheet As Integer
For sheet = 1 To Worksheets.Count
Set objSht = Sheets(sheet)
i = 3
Do
strTemp = clearChar(objSht.Cells(i, 2).Text)
If (strTemp = "") Then Exit Do ' If there is no field , Just skip.
strTemp = "(14,99,'" & strTemp & "','" & clearChar(objSht.Cells(i, 3)) & "','" & clearChar(objSht.Cells(i, 4)) & "','" & clearChar(objSht.Cells(i, 5)) & "','" & clearChar(objSht.Cells(i, 6) ) & "','/uploadfile/logo" & today & "/" & clearChar(objSht.Cells(i, 2)) & ".jpg','" & clearChar(objSht.Cells(i, 8)) & "','" & clearChar(objSht.Cells(i, 9)) & "','" & clearChar(objSht.Cells(i, 10)) & "','" & clearChar(objSht.Cells(i, 11)) & "'),"
Print #1, strTemp
i = i + 1
Loop
Set objSht = Nothing
Next
Close #1
MsgBox " Data export completed !", vbInformation
End Sub
Sub saveImg()
Dim objSht As Object
Dim i As Integer, minHeight As Integer, sh As Shape
Dim FileName As String
Dim today As String
today = Year(Date) & Month(Date) & Day(Date)
Dim folder As String
Open ThisWorkbook.Path & "\log.txt" For Output As #1
minHeight = ActiveSheet.Cells(1, 1).Height + ActiveSheet.Cells(2, 1).Height
folder = ThisWorkbook.Path & "\sblogo" & today
On Error Resume Next
MkDir folder
Dim sheet As Integer
For sheet = 38 To Worksheets.Count
Set objSht = Sheets(sheet)
For i = 1 To objSht.Shapes.Count
Set shp = objSht.Shapes(i)
strTemp = sheet & "-" & i & "-" & clearChar(objSht.Cells(shp.TopLeftCell.Row, 2)) & "-" & shp.Height & "-" & objSht.Name
Print #1, strTemp
If shp.Top > minHeight And shp.Height > 0 And shp.Width > 0 Then
FileName = folder & "\" & clearChar(objSht.Cells(shp.TopLeftCell.Row, 2)) & ".jpg"
Print #1, FileName
shp.Copy
With objSht.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart ' If the error is reported because the picture is illegal , The exact reason is unknown
.Paste
.Export FileName, "jpg"
.Parent.Delete
End With
End If
Next
' Sleep 500
Next
Close #1
MsgBox " Picture export completed !", vbInformation
End Sub```
边栏推荐
- 马斯克发布人形机器人,AI对马斯克为什么意义重大?
- Part I Verilog quick start
- 什么是持久化?redis 持久化中的RDB和AOF是什么?
- Bingbing's learning notes: implementation of circular queue
- 坐标系左乘右乘
- Thorough understanding of database transactions
- Simple text analysis of malicious samples - Introduction
- 30岁了开始自学编程,家里比较困难还来得及吗?
- What is persistence? What are RDB and AOF in redis persistence?
- Laravel document sorting 11. System architecture
猜你喜欢

JS arguments

简单的恶意样本行文分析-入门篇

php开发支付宝支付功能之扫码支付流程图

cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)

A detailed summary of TCP connection triple handshake

CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)

关于TCP连接四次握手(或者叫四次挥手)的详细总结

Vscode 设置clang-format

CTF_ Web: how to recognize and evaluate a regular expression
随机推荐
Mongodb cluster
2021.8.29 notes: register, bit operation, pointer, structure
Record small knowledge points
记录小知识点
How to screen out words related to products and eliminate invalid words accurately
Use of deferred environment variable in gbase 8s
GBASE 8s中DELIMIDENT环境变量的使用
Immutable學習之路----告別傳統拷貝
《牛客刷verilog》Part I Verilog快速入门
GBASE 8s活锁、死锁问题的解决
Unit test coverage
CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
UCLA | generative pre training for black box optimization
GBASE 8s的多线程结构
ROS2/DDS/QoS/主题的记录
JS arguments
写shell脚本报错总结
什么是数据持久化?
CMD operation MySQL in Windows
Blob page in gbase 8s