【Excel】【VB和JS】表格内容姓名、卡号、身份证敏感信息转换为图片打印

VB代码:

vbscript 复制代码
Function ConvertCellToImageAndPlace(n As Long, m As Long)
    Dim sourceCell As Range
    Set sourceCell = Sheets("Sheet2").Cells(n, m)
    
    sourceCell.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    
    Dim targetCell As Range
    Set targetCell = Sheets("Sheet3").Cells(n, m)
    
    Sheets("Sheet3").Paste targetCell
    
    Dim shp As Shape
    Set shp = Sheets("Sheet3").Shapes(Sheets("Sheet3").Shapes.Count)
    
    shp.Left = targetCell.Left
    shp.Top = targetCell.Top
    shp.Width = targetCell.Width
    shp.Height = targetCell.Height
End Function
Function ClearSheet3Completely()
    Sheets("Sheet3").Cells.ClearContents '清空单元格内容
    Sheets("Sheet3").Cells.ClearFormats '清空单元格格式
    Dim shp As Shape
    For Each shp In Sheets("Sheet3").Shapes
        shp.Delete
    Next shp
End Function
Sub AutoPrintPicture()

    ClearSheet3Completely
    For i = 1 To 10
         Sheets("Sheet2").Cells(10, 3) = i + 201
         ConvertCellToImageAndPlace 10, 3
         ConvertCellToImageAndPlace 10, 2
         ConvertCellToImageAndPlace 10 + 1, 2
         ConvertCellToImageAndPlace 10 + 2, 2
         Sheets("Sheet3").PrintOut

         ClearSheet3Completely
    Next i
    
End Sub

JS代码:

python 复制代码
function convertCellToImageAndPlace(row, col) {
    // 获取源单元格
    let sourceCell = Sheets("Sheet2").Cells(row, col);
    // 复制为图片,明确参数类型为数字,根据 WPS 的文档可能需要使用特定的数值代表外观
    sourceCell.CopyPicture();
    // 获取目标单元格
    let targetCell = Sheets("Sheet3").Cells(row, col);
    // 粘贴到目标单元格
    Sheets("Sheet3").Paste(targetCell);
}

function clearSheet3Completely(){
	// 获取当前Sheet表对象
	var app = Application.Application,
		acSh = app.Sheets("Sheet3");
	// 循环遍历当前Sheet表中的所有图形
	for (let pic of acSh.Shapes){
		// 如果当前为"图片"、"==13",执行删除操作
		if (pic.Type == msoPicture){
            // Shape.TopLeftCell,代表位于对象左上角下方的单元格
			var r = pic.TopLeftCell.Row,
				c = pic.TopLeftCell.Column;
			pic.Delete();
			Console.log(`${r}行${c}列图片删除成功@SMFY`);
		}
	}
}

function autoPrintPicture() {
   //clearSheet3Completely();
    for (let i = 1; i <= 3;i++) {
        Sheets("Sheet2").Cells(10, 3).Value2 = i + 1;
        convertCellToImageAndPlace(10, 3);
        convertCellToImageAndPlace(10, 2);
        convertCellToImageAndPlace(11, 2);
        convertCellToImageAndPlace(12, 2);
        Sheets("Sheet3").PrintOut();
        clearSheet3Completely();
    }
}

总结:

convertCellToImageAndPlace函数:

  • 从 "Sheet2" 工作表的指定单元格(由参数n和m确定)复制内容为图片。
  • 将图片粘贴到 "Sheet3" 工作表的对应单元格。
  • 获取 "Sheet3" 工作表中刚刚粘贴的图片对应的形状对象(即最后一个形状)。
  • 如果形状对象存在,将其位置和大小设置为目标单元格的位置和大小,并返回该形状对象。

clearSheet3Completely函数:

  • 清空 "Sheet3" 工作表的所有单元格内容。
  • 清空 "Sheet3" 工作表的所有单元格格式。
  • 遍历并删除 "Sheet3" 工作表中的所有形状。

autoPrintPicture函数:

  • 首先调用clearSheet3Completely函数清空 "Sheet3" 工作表。
  • 循环 10 次,每次循环: 在 "Sheet2" 工作表的特定单元格(第 10 行第 3 列)设置一个值,该值为当前循环次数加 201。
  • 分别调用convertCellToImageAndPlace函数将 "Sheet2" 工作表的特定四个位置(10,3)、(10,2)、(11,2)、(12,2)的单元格内容复制为图片并放置在 "Sheet3" 工作表的对应位置,同时获取对应的形状对象。 打印 "Sheet3" 工作表。
  • 再次调用clearSheet3Completely函数清空 "Sheet3" 工作表,为下一次循环做准备。
相关推荐
CodeCraft Studio1 小时前
国产化Excel开发组件Spire.XLS教程:将Python列表转换为Excel表格(3种实用场景)
开发语言·python·excel·spire.xls·python列表转excel·国产化文档开发
企鹅侠客1 小时前
基于python写的PDF表格提取到excel文档
python·pdf·excel·pdf文档表格转excel
忘忧记3 小时前
Excel VLOOKUP函数完全教程:从基础到高级实战
excel
葡萄城技术团队3 小时前
突破Excel局限!SpreadJS让电子表格“活”起来
java·数据库·excel
左师佑图5 小时前
Apache POI SXSSFWorkbook 报错“没有那个文件或目录”问题排查与解决方案
java·apache·excel
monkeyhlj8 小时前
excel-mcp-server rocky linux简单部署
linux·运维·excel
lijingguang13 小时前
excel和word文件默认用office打开而不是用wps
word·excel·wps
xurime14 小时前
Excelize 开源基础库发布 2.10.0 版本更新
golang·开源·github·excel·ai编程·go语言
SunkingYang1 天前
详细介绍C++中通过OLE操作excel时,一般会出现哪些异常,这些异常的原因是什么,如何来解决这些异常
c++·excel·解决方案·闪退·ole·异常类型·异常原因