excel - 通过 Angular 调用/运行 VBA 宏
问题描述
我在从角度调用 excel 宏时被卡住了。我正在使用电子表格(https://www.grapecity.com/en/blogs/how-to-import-and-export-excel-spreadsheets-in-angular)加载 Excel 表格。
借助它,我可以在浏览器中打开启用宏的 excel 文件。但是打开后我需要运行一个特定的宏,它有一些通过 angular 6 的参数。
应用程序.html
<div class='maincontainer'>
<gc-spread-sheets [hostStyle]="hostStyle"
(workbookInitialized)="workbookInit($event)">
</gc-spread-sheets>
<div class='loadExcelInput'>
<p>Open Excel File</p>
<input type="file" name="files[]" multiple id="jsonFile" accept=".xlsm" (change)="onFileChange($event)" />
</div>
<div class='exportExcel'>
<p>Save Excel File</p>
<button (click)="onClickMe($event)">Save Excel!</button>
</div>
</div>
应用程序.ts
import { Component } from '@angular/core';
import * as GC from '@grapecity/spread-sheets';
import * as Excel from '@grapecity/spread-excelio';
import '@grapecity/spread-sheets-charts';
import {saveAs} from 'file-saver';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
spreadBackColor = 'aliceblue';
hostStyle = {
width: '95vw',
height: '80vh'
};
private spread: GC.Spread.Sheets.Workbook;
private excelIO;
constructor() {
this.excelIO = new Excel.IO();
}
workbookInit(args) {
const self = this;
self.spread = args.spread;
const sheet = self.spread.getActiveSheet();
sheet.getCell(0, 0).text('Test Excel').foreColor('blue');
sheet.getCell(1, 0).text('Test Excel').foreColor('blue');
sheet.getCell(2, 0).text('Test Excel').foreColor('blue');
sheet.getCell(3, 0).text('Test Excel').foreColor('blue');
sheet.getCell(0, 1).text('Test Excel').foreColor('blue');
sheet.getCell(1, 1).text('Test Excel').foreColor('blue');
sheet.getCell(2, 1).text('Test Excel').foreColor('blue');
sheet.getCell(3, 1).text('Test Excel').foreColor('blue');
sheet.getCell(0, 2).text('Test Excel').foreColor('blue');
sheet.getCell(1, 2).text('Test Excel').foreColor('blue');
sheet.getCell(2, 2).text('Test Excel').foreColor('blue');
sheet.getCell(3, 2).text('Test Excel').foreColor('blue');
sheet.getCell(0, 3).text('Test Excel').foreColor('blue');
sheet.getCell(1, 3).text('Test Excel').foreColor('blue');
sheet.getCell(2, 3).text('Test Excel').foreColor('blue');
sheet.getCell(3, 3).text('Test Excel').foreColor('blue');
}
onFileChange(args) {
const self = this, file = args.srcElement && args.srcElement.files && args.srcElement.files[0];
if (self.spread && file) {
// var aa = self.excelIO.GetApplication();
self.excelIO.open(file, (json) => {
self.spread.fromJSON(json, {});
setTimeout(() => {
alert('load successfully');
}, 0);
}, (error) => {
alert('load fail');
});
}
}
onClickMe(args) {
const self = this;
const filename = 'exportExcel.xlsx';
const json = JSON.stringify(self.spread.toJSON());
self.excelIO.save(json, function (blob) {
saveAs(blob, filename);
}, function (e) {
console.log(e);
});
}
}
在加载 excel 文件的警报后,我想从 .ts 运行该文件的宏请建议解决方案或任何其他满足我要求的库。
解决方案
推荐阅读
- php - 从 Mysql 迁移到 MariaDb 后出错
- android - Android Studio 3.6.1 的 SDK 管理器中缺少 Android SDK 工具选项
- javascript - 为什么我的元素没有响应点击事件?
- c++ - 我可以操作类的每个对象的相同元素吗?
- python-3.x - 无法创建具有 Visual Studio 错误的 conda 环境
- java - 原因:java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册
- java - 覆盖 equals() 方法
- c - 原子写入文件中的多个位置?
- java - 设置后退按钮无法使用波纹管代码?
- java - Javadoc 选项文件中的单行注释支持哪些字符?