【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" 工作表,为下一次循环做准备。
相关推荐
云只上7 小时前
前端插件使用xlsx-populate,花样配置excel内容,根据坐添加标替换excel内容,修改颜色,合并单元格...。
excel
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
游客52016 小时前
自动化办公|通过xlwings进行excel格式设置
python·自动化·excel
喝西瓜汁的兔叽Yan17 小时前
第三方库XLSX: 前端上传excel文件,并对excel文件内容做校验。
前端·excel·xlsx
刘_sy1 天前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
PowerBI学谦2 天前
Copilot:Excel中的Python高级分析来了
python·excel·copilot
E-iceblue3 天前
Python 合并 Excel 单元格
python·excel·合并
sszdzq3 天前
Excel 合并列数据
excel
prince_zxill3 天前
Excel中不用复杂公式根据指定X列的数值N复制整行数据N行简单方法
excel