1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

帖子
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 阿哩木

[求助] 表格内容保存为图片,在此跪求大神、巨佬来帮忙解决下这个问题!

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-3-31 19:16 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
313667354 发表于 2025-3-31 17:03
要把数据一段段做成图片,这样做的目的是做什么用途呢?

自然是工作上需要了

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-3-31 19:25 | 显示全部楼层
lss001 发表于 2025-3-31 15:56
请参考一下附件代码!

大佬大佬 !!就是你做的这个结果,但还是有点点地方需要修改一下 可以留下QQ吗 我在上面再请教您下 这个忙必须给您点辛苦费,一点小心意!!

TA的精华主题

TA的得分主题

发表于 2025-3-31 19:52 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
阿哩木 发表于 2025-3-31 19:25
大佬大佬 !!就是你做的这个结果,但还是有点点地方需要修改一下 可以留下QQ吗 我在上面再请教您下 这个 ...

有什么问题在论坛也可以发私信

TA的精华主题

TA的得分主题

发表于 2025-3-31 21:47 | 显示全部楼层
我也用AI写了一个,改了改,可以用

  1. Sub ExportDataToImages()
  2.     Dim ws As Worksheet
  3.     Dim lastRow As Long, currentRow As Long
  4.     Dim batchStart As Long, batchEnd As Long
  5.    
  6.     Set ws = ThisWorkbook.ActiveSheet
  7.     lastRow = ws.UsedRange.Rows.Count
  8.     currentRow = 3 ' 数据起始行
  9.    
  10.     Application.ScreenUpdating = False
  11.     Application.DisplayAlerts = False
  12.     Do While currentRow <= lastRow
  13.         batchStart = currentRow
  14.         batchEnd = FindBatchEnd(ws, batchStart, lastRow)
  15.         ProcessABatch ws, batchStart, batchEnd
  16.         currentRow = batchEnd + 1
  17.     Loop
  18.    
  19.     ws.Rows.EntireRow.Hidden = False
  20.     Application.ScreenUpdating = True
  21.     Application.DisplayAlerts = True

  22.     MsgBox "处理完成!"
  23. End Sub

  24. Function FindBatchEnd(ws As Worksheet, startRow As Long, lastRow As Long) As Long
  25.     Dim currentValue As String
  26.     currentValue = ws.Cells(startRow, "B").Value
  27.     FindBatchEnd = startRow
  28.    
  29.     Do While FindBatchEnd < lastRow
  30.         Dim nextRow As Long
  31.         nextRow = FindBatchEnd + 1
  32.         
  33.         If ws.Cells(nextRow, "B").Value = currentValue Or ws.Cells(nextRow, "B").Value = "HJ" Then
  34.             FindBatchEnd = nextRow
  35.         Else
  36.             Exit Do
  37.         End If
  38.     Loop
  39. End Function

  40. Sub ProcessABatch(ws As Worksheet, batchStart As Long, batchEnd As Long)
  41.     Dim subStart As Long, subEnd As Long
  42.     Dim outputRange As Range
  43.    
  44.     subStart = batchStart
  45.    
  46.     Do While subStart <= batchEnd
  47.         subEnd = FindSubEnd(ws, subStart, batchEnd)
  48.         subEnd = CheckHJSpecial(ws, subEnd, batchEnd)
  49.         
  50.         On Error Resume Next
  51.         Set outputRange = ws.Range(ws.Cells(1, "A"), ws.Cells(subEnd, "N"))
  52.         
  53.         If Not outputRange Is Nothing Then
  54.             ExportAsImage ws, outputRange, "Export_" & subStart & "_" & subEnd
  55.         Else
  56.             MsgBox "无法合并范围:行" & subStart & "-" & subEnd
  57.         End If
  58.         ws.Rows(CLng(subStart) & ":" & CLng(subEnd)).Select
  59.         Selection.EntireRow.Hidden = True
  60.         subStart = subEnd + 1
  61.     Loop
  62. End Sub

  63. Function FindSubEnd(ws As Worksheet, startRow As Long, batchEnd As Long) As Long
  64.     FindSubEnd = startRow
  65.     If startRow >= batchEnd Then Exit Function
  66.    
  67.     Do While FindSubEnd < batchEnd
  68.         Dim nextRow As Long
  69.         nextRow = FindSubEnd + 1
  70.         
  71.         If ws.Cells(nextRow, "A").Value = 1 Then
  72.             Exit Do
  73.         Else
  74.             FindSubEnd = nextRow
  75.         End If
  76.     Loop
  77. End Function

  78. Function CheckHJSpecial(ws As Worksheet, currentEnd As Long, batchEnd As Long) As Long
  79.     CheckHJSpecial = currentEnd
  80.     If currentEnd >= batchEnd Then Exit Function
  81.    
  82.     Dim i As Long
  83.     For i = currentEnd + 1 To batchEnd
  84.         If IsEmpty(ws.Cells(i, "A")) And ws.Cells(i, "B").Value = "HJ" Then
  85.             CheckHJSpecial = i
  86.         Else
  87.             Exit For
  88.         End If
  89.     Next i
  90. End Function

  91. Sub ExportAsImage(ws As Worksheet, rng As Range, filename As String)
  92.     If ws Is Nothing Then
  93.         MsgBox "工作表对象无效!"
  94.         Exit Sub
  95.     End If
  96.    
  97.     If rng Is Nothing Then
  98.         MsgBox "输出范围无效!"
  99.         Exit Sub
  100.     End If
  101.    
  102.     Dim chartObj As ChartObject
  103.     Dim tempChart As Chart
  104.    
  105.     ' 复制为图片
  106.     rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
  107.    
  108.     ' 创建临时图表对象
  109.     Workbooks.Add
  110.     ActiveSheet.Paste
  111.     Set shp = ActiveSheet.Shapes(1)
  112.         With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart
  113.                 .Parent.Select
  114.                 .Paste
  115.                 .Export ws.Parent.Path & "" & filename & ".png"
  116.         End With
  117.     ' 清理对象
  118.    ActiveWorkbook.Close False
  119. End Sub
复制代码




表格内容保存为图片.zip (273.98 KB, 下载次数: 8)

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2025-3-31 22:15 | 显示全部楼层
Sub test()
Dim i, m, k, n, irow As Integer
Dim ar As Variant
Dim ws As Worksheet
Dim rng As Range
Dim zoom#
zoom = 2
Dim Chartobj As ChartObject
Dim p As String
p = ThisWorkbook.Path & "\"
ThisWorkbook.Sheets("a").Activate
Set ws = ThisWorkbook.Sheets.Add(after:=Sheets(Sheets.Count))
ws.Name = "过渡"
ar = ThisWorkbook.Sheets("A").Range("a1:o" & Sheets("A").[b65536].End(xlUp).Row + 1)
ar(UBound(ar), 2) = "HJ"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 3 To UBound(ar)
  With ThisWorkbook.Sheets("过渡")
  If ar(i, 2) <> "HJ" Then
    If InStr(ar(i, 1), 1) > 0 Then
      m = m + 1
      ThisWorkbook.Sheets("a").Cells(i, UBound(ar, 2)) = m
        If .[a1] = "" Then
            ThisWorkbook.Sheets("A").Range("a1:n2").Copy
            .[a1].PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            .[a1].PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Else
            irow = .[a65536].End(xlUp).Row
            ThisWorkbook.Sheets("A").Range("a1:n2").Copy
            .Cells(irow + 2, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            .Cells(irow + 2, 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         End If
      irow = .[a65536].End(xlUp).Row
      ThisWorkbook.Sheets("a").Cells(i, 1).Resize(1, UBound(ar, 2)).Copy
      .Cells(irow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      .Cells(irow + 1, 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
      Application.CutCopyMode = False
     End If
  End If
  End With
  If ar(i, 2) = "HJ" Then
    ThisWorkbook.Sheets("过渡").Columns.AutoFit
    n = n + 1
    Set rng = ThisWorkbook.Sheets("过渡").UsedRange
    rng.CopyPicture xlPrinter, xlPicture
    With ThisWorkbook.Sheets("过渡").ChartObjects.Add(0, 0, rng.Width * zoom, rng.Height * zoom).Chart
         .Parent.Select
         .Paste
         .Export p & "图片" & n & ".png", "png"
         .Parent.Delete
    End With
    ThisWorkbook.Sheets("过渡").Activate
    ThisWorkbook.Sheets("过渡").[a1].Resize(100, UBound(ar, 2)).Clear
  End If
Next
Application.CutCopyMode = False
ThisWorkbook.Sheets("过渡").Delete
Application.ScreenUpdating = False
Application.DisplayAlerts = False
MsgBox "ok"
End Sub

TA的精华主题

TA的得分主题

发表于 2025-3-31 22:16 | 显示全部楼层
供参考,欢迎批评指正。存在的缺点:运行速度有点慢

工作表保存为png图片.rar

470.73 KB, 下载次数: 13

样稿

TA的精华主题

TA的得分主题

发表于 2025-3-31 22:48 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2025-4-1 01:55 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
稳定度不佳。
有时后,输出的图片,最前面几张,会只有 1 KB ,而图片是空白的。
有时后,paste 会产生错误,而需要 resume 。
为了仅量不出现上面状况,在效率上,也不好。
还是,就不要为难Excel了。

  1. Sub Main()
  2.     Dim PNG_Path As String
  3.     PNG_Path = ThisWorkbook.Path
  4.    
  5.     Main_sb "A", PNG_Path
  6.    
  7.     Main_sb "B", PNG_Path
  8. End Sub

  9. Sub Main_sb(wsName As String, PNG_Path As String)
  10.     Application.ScreenUpdating = False
  11.    
  12.     sbSheet_tmp wsName
  13.    
  14.     sbCopyToPic wsName
  15.    
  16.     sbPNG_Fill_ForeColor
  17.    
  18.     sbExport_PNG wsName, PNG_Path
  19. End Sub

  20. Sub sbSheet_tmp(wsName As String)
  21.     Dim wsTemp As Worksheet
  22.     Set wsTemp = Sheets.Add
  23.     wsTemp.Name = "Temp"
  24.    
  25.     Dim ws As Worksheet
  26.     Set ws = Worksheets(wsName)
  27.     nCols = ws.Range("A1").CurrentRegion.Columns.Count
  28.    
  29.     ws.Columns("A").Resize(, nCols).Copy
  30.    
  31.     wsTemp.Range("A1").PasteSpecial
  32.     wsTemp.Range("A1").CurrentRegion.Offset(2).Clear
  33. End Sub

  34. Sub sbCopyToPic(wsName As String)
  35.     Dim ws As Worksheet
  36.     Set ws = Worksheets(wsName)
  37.     ws.Activate
  38.     nRows = ws.Range("A1").CurrentRegion.Rows.Count
  39.     nCols = ws.Range("A1").CurrentRegion.Columns.Count
  40.    
  41.     Dim wsTemp As Worksheet
  42.     Set wsTemp = Worksheets("Temp")
  43.    
  44.     Dim Row_Start
  45.     Dim Row_End
  46.     For i = 1 To nRows - 1
  47.         If Range("A1").Offset(i) = 1 Then
  48.             If Row_Start = "" Then
  49.                 Row_Start = i
  50.             Else
  51.                 Row_End = i - 1
  52.             End If
  53.         End If
  54.         
  55.         If i = nRows - 1 Then
  56.             Row_End = i
  57.         End If
  58.         
  59.         If Range("A1").Offset(i) = "" And Range("B1").Offset(i) = "" Then
  60.             Row_End = i - 1
  61.             myExit_for = True
  62.         End If
  63.         
  64.         If Row_Start <> "" And Row_End <> "" Then
  65.             wsTemp.Range("A1").CurrentRegion.Offset(2).Clear
  66.             Range(Range("A1").Offset(Row_Start), Cells(Row_End + 1, nCols)).Copy
  67.             wsTemp.Range("A3").PasteSpecial
  68.             wsTemp.Range("A1").CurrentRegion.CopyPicture xlScreen, xlPicture
  69.             wsTemp.Cells(1, nCols + 2).PasteSpecial
  70.             i = i - 1
  71.             Row_Start = ""
  72.             Row_End = ""
  73.         End If
  74.         
  75.         If myExit_for Then Exit For
  76.     Next i
  77. End Sub

  78. Sub sbPNG_Fill_ForeColor()
  79.     Worksheets("Temp").Activate
  80.     Worksheets("Temp").Shapes.SelectAll
  81.     Selection.ShapeRange.Fill.Visible = msoTrue
  82.     Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
  83. End Sub

  84. Sub sbExport_PNG(wsName As String, PNG_Path As String)
  85.     Application.ScreenUpdating = True
  86.     Dim ws As Worksheet
  87.     Set ws = Worksheets("Temp")
  88.     ws.Activate
  89.    
  90.     Dim nShapes
  91.     nShapes = ws.Shapes.Count
  92.    
  93.     Dim chartObj As ChartObject
  94.     Set chartObj = ws.ChartObjects.Add(Left:=20, Top:=20, Width:=100, Height:=100)
  95.    
  96.     Dim x As Shape
  97.     For Each x In ws.Shapes
  98.         i = i + 1
  99.         DoEvents
  100.         x.Copy
  101.         With chartObj
  102.             .Width = x.Width
  103.             .Height = x.Height
  104.             With .Chart
  105.             .Paste
  106.             .Export fileName:=PNG_Path & "" & wsName & "_PNG_" & i & ".png"
  107.             End With
  108.         End With
  109.         If i = nShapes Then Exit For
  110.     Next x
  111.    
  112.     Application.DisplayAlerts = False
  113.     ws.Delete
  114. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2025-4-1 08:53 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
这是一个保准的求助,建议更多的人看到,参考学习。 有举例的数据,并且也有代表性,包含了自己需要的几个情况,并且有手动整理数据的结果,和模拟了需要生成的结果。然后搭配文字描述具体的规则和要求,而且描述也很有条理性,基本完整的阐述了需求,让人看看一遍基本就懂了。

TA的精华主题

TA的得分主题

发表于 2025-4-1 10:15 | 显示全部楼层
  1. Sub Main()
  2.     '需求是生成图片,但是数据和标题不挨着,肯定没法直接生成图片,需要一个辅助区域
  3.     '既然标题固定都有,那么先准备一个辅助的标题,可以手工去做一个模板
  4.     On Error GoTo ErrLine
  5.     Dim sh As Worksheet, coll As Collection
  6.     Dim fdPth As String, ar
  7.    
  8.     '1
  9.     Set sh = SetSheetTitle                  '建临时表用来导出图片
  10. '    Set sh = Sheets("A表备份")             '其实可以手动建个表,格式自己设置好更方便
  11.    
  12.     '2  数据源整理拆分成独立的数据,用数组存储
  13.     Set coll = GetCleanData(Sheet2)
  14.    
  15.     '3  循环导出图片
  16.     AppSet False
  17.     fdPth = ".....\AA"
  18.     For Each ar In coll
  19.         k = k + 1
  20.         With sh
  21.             .Range("A3:N" & UBound(ar) + 2).Value = ar
  22.             Call ExportPng(.Range("A1:N" & UBound(ar) + 2), fdPth & "图片" & k)
  23.             .Range("A3:N" & UBound(ar) + 2).ClearContents
  24.         End With
  25.     Next
  26. ErrLine:
  27. '    sh.Delete '临时表用完销毁
  28.     AppSet True
  29. End Sub
  30. Function SetSheetTitle() As Worksheet
  31.     Sheets("A").Copy Before:=Sheets(1)
  32.     With ActiveSheet
  33.         .Range("P:az").Clear
  34.         .Rows("3:9999").ClearContents
  35.     End With
  36.     Set SetSheetTitle = ActiveSheet
  37. End Function
  38. Function GetCleanData(sh As Worksheet) As Collection
  39.     With sh
  40.         r = .Cells(.Rows.Count, 2).End(3).Row
  41.         ar = .Range("A1:N" & r + 1).Value
  42.     End With
  43.     Dim cl As New Collection
  44.     r1 = 3
  45.     For j = 3 To r
  46.         If j = r Or ar(j + 1, 1) = 1 Then
  47.             n = j - r1 + 1
  48.             ReDim br(1 To n, 1 To 14)
  49.             For i = 1 To n
  50.             For k = 1 To 14
  51.                 br(i, k) = ar(r1 + i - 1, k)
  52.             Next
  53.             Next
  54.             cl.Add br, "图片" & cl.Count + 1
  55.             r1 = j + 1
  56.         End If
  57.     Next
  58.     Set GetCleanData = cl
  59. End Function
  60. Function ExportPng(rng0 As Range, PicPth As String) '导出图片懒得写了,论坛很多单元格区域导出图片的代码
  61.     r = rng0.Offset(65000, 18).End(3).Offset(2).Row
  62.     rng0.Worksheet.Cells(r, "R").Value = PicPth
  63.     rng0.Copy rng0.Worksheet.Cells(r + 1, "R")        '复制成独立的区域为例演示结果
  64. End Function
  65. Public Sub AppSet(AppBl As Boolean)
  66.     With Application
  67.         .ScreenUpdating = AppBl
  68.         .DisplayAlerts = AppBl
  69.         .EnableEvents = AppBl
  70.     End With
  71. End Sub
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-4-16 11:38 , Processed in 0.027339 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表