如何防止用户用JavaScript通过C++打印Excel文件
Contents
[
Hide
]
可能的使用场景
在日常工作中,Excel文件中可能包含一些重要信息;为了防止内部数据传播,公司不会允许我们打印。这份文件将告诉你如何阻止他人打印Excel文件。
如何防止用户在MS-Excel中打印文件
您可以应用以下VBA代码以保护您的特定文件不被打印。
- 打开您不允许他人打印的工作簿。
- 在Excel功能区选择“开发者”标签,然后点击“控件”中的“查看代码”按钮。或者,您可以按下ALT + F11键打开Microsoft Visual Basic for Applications窗口。

- 然后在左侧项目资源管理器中,双击ThisWorkbook以打开模块,并添加一些VBA代码。

- 保存并关闭代码,返回到工作簿,现在当您打印示例文件时,将不允许打印,并会显示以下警告框:

用Aspose.Cells for JavaScript通过C++防止用户打印Excel文件的方法
以下示例代码说明如何防止用户打印Excel文件:
- 加载示例文件。
- 从工作簿的VbaProject属性获取VbaModuleCollection对象。
- 通过"ThisWorkbook"名称获取VbaModule对象。
- 设置VbaModule的代码属性。
- 将示例文件保存为xlsm格式。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Update VBA Module Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.xlsm" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing VBA project and its modules
const modules = workbook.vbaProject.modules;
const module = modules.get("ThisWorkbook");
// Setting module codes (converted from setCodes -> codes assignment)
module.codes = "Private Sub Workbook_BeforePrint(Cancel As Boolean)\r\n Cancel = True\r\n MsgBox \"Refusing to print in paperless office\"\r\nEnd Sub\r\n";
// Saving the modified workbook as macro-enabled workbook
const outputData = workbook.save(SaveFormat.Xlsm);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out.xlsm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">VBA module updated successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>