微软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列的二维码就会自动刷新。

相关推荐
观远数据9 小时前
Excel继续用、自研BI、替换BI:产品VP拆解三条路线的隐性成本与能力边界
大数据·人工智能·excel
用户298698530141 天前
Excel 转 PDF 免费在线工具,格式完美保留
人工智能·c#·excel
开开心心就好1 天前
文件查重软件批量删除重复文件释放空间
java·开发语言·随机森林·ocr·excel·音视频·最小二乘法
开开心心就好1 天前
文件批量重命名工具简单好用支持规则改名
java·开发语言·b树·ocr·excel·音视频·kmeans
破z晓2 天前
javascript 导出excel表
开发语言·javascript·excel
Am-Chestnuts2 天前
豆包多轮回答中的表格导出 Excel:编号、日期与长数字检查
excel
hPw0eKIqD3 天前
使用二次封装的Excel COM 组件操作Excel\WPS ET中的区域、行和列
excel·wps
冰心孤城3 天前
Excel: xls与xlsx格式转换排坑指南
java·前端·excel
金豆呀3 天前
WPS批量提取Word文档内容生成固定格式Excel表格
word·excel·wps
Access开发易登软件3 天前
Access 怎么做前后端分离?用 Web API 读写 SQL Server
前端·数据库·人工智能·microsoft·excel·access