微软365Excel配合本地艺术二维码API在指定单元格动态生成二维码

第一步:模块里放这个宏代码

Alt+F11 → 插入模块 → 粘贴:

复制代码
Option Explicit

' 二维码固定尺寸(像素)
Const QR_SIZE As Integer = 40

Sub 生成二维码()
    Dim ws As Worksheet
    Dim i As Long, lastRow As Long
    Dim url As String
    Dim qrCell As Range
    Dim tempPath As String
    Dim leftPos As Single, topPos As Single
    
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    tempPath = Environ("TEMP") & "\temp_qr_" & Environ("USERNAME") & ".png"
    ws.Columns("B").ColumnWidth = 10    ' 固定足够宽度,不挤压
    ws.Rows(1 & ":" & lastRow).RowHeight = 60 ' 足够高度,居中不贴边
    
    ' 清除B列所有旧二维码(防止重叠)
    DeleteAllQrShapes ws
    
    For i = 1 To lastRow
        If Trim(ws.Cells(i, "A").Value) <> "" Then
            url = "http://127.0.0.1:3000/api/qrcode?content=" & WorksheetFunction.EncodeURL(ws.Cells(i, "A").Value)
            
            ' 下载二维码
            DownloadFile url, tempPath
            
            Set qrCell = ws.Cells(i, "B")
            leftPos = qrCell.Left + (qrCell.Width - QR_SIZE) / 2
            topPos = qrCell.Top + (qrCell.Height - QR_SIZE) / 2
            
            ' 插入二维码(固定尺寸、正正方形、不拉伸)
            With ws.Shapes.AddPicture( _
                Filename:=tempPath, _
                LinkToFile:=msoFalse, _
                SaveWithDocument:=msoTrue, _
                Left:=leftPos, _
                Top:=topPos, _
                Width:=QR_SIZE, _
                Height:=QR_SIZE)
                
                .Name = "QR_" & i
                .LockAspectRatio = msoTrue ' 强制正方形
            End With
        End If
    Next i
    
    If Dir(tempPath) <> "" Then Kill tempPath
End Sub

' 辅助:删除B列所有旧二维码
Private Sub DeleteAllQrShapes(ws As Worksheet)
    On Error Resume Next
    Dim shp As Shape
    For Each shp In ws.Shapes
        If shp.Name Like "QR_*" Then shp.Delete
    Next
    On Error GoTo 0
End Sub

' 辅助:下载图片
Private Sub DownloadFile(url As String, savePath As String)
    Dim http As Object, stream As Object
    Set http = CreateObject("MSXML2.XMLHTTP.6.0")
    Set stream = CreateObject("ADODB.Stream")
    
    With http
        .Open "GET", url, False
        .send
        If .Status <> 200 Then
            MsgBox "? 接口请求失败,状态码:" & .Status, vbCritical
            Exit Sub
        End If
    End With
    
    With stream
        .Mode = 3
        .Type = 1
        .Open
        .Write http.responseBody
        .SaveToFile savePath, 2
        .Close
    End With
    
    Set stream = Nothing
    Set http = Nothing
End Sub

第二步:工作表代码实现「自动生成」

在VBA编辑器里,双击左侧你的工作表(比如Sheet1),粘贴下面这段代码:

复制代码
Private Sub Worksheet_Change(ByVal Target As Range)
    ' 当A列内容变化时,自动重新生成二维码
    If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
        Application.ScreenUpdating = False
        Call 生成二维码
        Application.ScreenUpdating = True
    End If
End Sub

第三步:启动艺术二维码API服务


第四步:启动艺术二维码API服务

  1. 保存文件为「启用宏的工作簿(.xlsm)」格式。
  2. 运行一次宏,让B列自动调整单元格大小。
  3. 之后只要你修改A列的内容,B列的二维码就会自动刷新。

相关推荐
Non-existent9876 天前
WPS批量清理单元格空白字符的4种方法-异常数字格式处理-实战
excel·wps
Channing Lewis7 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
jarreyer7 天前
【数据分析绘图】excel绘图和bi工具区别
数据挖掘·数据分析·excel
chatexcel7 天前
ChatExcel Max使用教程:图片、PDF、网页与复杂Excel的一站式数据分析
数据分析·pdf·excel
cngkqy7 天前
excel从某一列中用match筛选匹配的数据
excel
qq_546937277 天前
Excel批量转PDF_Word_图片,支持自动合并报表,效率翻倍。
pdf·word·excel
ai_coder_ai7 天前
在自动化脚本中操作excel文件
运维·自动化·excel
三千花灯7 天前
【Playwright】 自动化测试之参数化登录(Excel/CSV 数据源)
人工智能·机器学习·excel
罗政7 天前
AI工作流实现Excel全自动化(支持SQL)-案例:医院门诊排班表
人工智能·自动化·excel
小妖6667 天前
excel 怎么在单元格内容自动加上一段文字不能用公式
excel·vba