【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" 工作表,为下一次循环做准备。
相关推荐
张太行_9 天前
MySQL与Excel比较
数据库·mysql·excel
cwtlw9 天前
Excel学习03
笔记·学习·其他·excel
郭优秀的笔记9 天前
计算本地Excel某两列的差异值
excel
涔溪10 天前
实现 el-table 中键盘方向键导航功能vue2+vue3(类似 Excel)
前端·vue.js·excel
云博客-资源宝10 天前
Excel函数大全
机器学习·excel·概率论
BillKu10 天前
【前后前】导入Excel文件闭环模型:Vue3前端上传Excel文件,【Java后端接收、解析、返回数据】,Vue3前端接收展示数据
java·前端·excel
焚 城11 天前
Excel数据导出小记
.net·excel
wtsolutions12 天前
Excel to JSON online converter, flat and nested JSON converter
json·excel·excel-to-json·wtsolutions
消失在人海中12 天前
excel 数据透视表介绍
excel