【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" 工作表,为下一次循环做准备。
相关推荐
Channing Lewis1 小时前
sql server如何创建表导入excel的数据
数据库·oracle·excel
沉到海底去吧Go17 小时前
【工具教程】PDF电子发票提取明细导出Excel表格,OFD电子发票行程单提取保存表格,具体操作流程
pdf·excel
开开心心就好19 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
沉到海底去吧Go2 天前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
Abigail_chow3 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
xiaohezi3 天前
Rag chunk 之:Excel 文档解析
excel
weixin_472339463 天前
python批量解析提取word内容到excel
python·word·excel
4 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel
金融小白数据分析之路4 天前
Excel高级函数使用FILTER、UNIQUE、INDEX
excel
未来之窗软件服务4 天前
Excel表格批量下载 CyberWin Excel Doenlaoder 智能编程-——玄武芯辰
excel·批量下载·仙盟创梦ide·东方仙盟