@sv-print/plugin-api-excel
v0.1.1
Published
add excel export api to print template
Maintainers
Readme
@sv-print/plugin-api-excel
为 sv-print 打印模板添加 Excel 导出功能,生成
.xlsx格式文件,兼容 Microsoft Excel、WPS、LibreOffice、Google Sheets 等主流办公软件。
安装
npm install @sv-print/plugin-api-excel
# or
pnpm add @sv-print/plugin-api-excel使用
import pluginApiExcel from "@sv-print/plugin-api-excel";
// 注册插件
hiprint.init([pluginApiExcel()]);
// 创建模板后即可使用 toExcel
let template = new hiprint.PrintTemplate(/* ... */);
// 下载 Excel 文件
template.toExcel(printData, { name: "导出文件名.xlsx" });
// 获取 Blob(不下载)
template.toExcel(printData, {
isDownload: false,
name: "report.xlsx",
}).then(blob => {
// 处理 blob
});API
template.toExcel(printData, options?)
参数:
| 参数 | 类型 | 说明 |
|------|------|------|
| printData | any | 打印数据 |
| options | object | 可选配置项 |
选项:
| 选项 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| isDownload | boolean | true | 是否自动下载文件 |
| name | string | sv-print-{timestamp}.xlsx | 下载文件名 |
| fontName | string | Microsoft YaHei | 默认字体 |
| fontSize | number | 10 | 默认字号(pt) |
| sheetName | string | Sheet | 工作表名称前缀 |
| pixelRatio | number | 1 | 非标准元素截图像素比 |
| onProgress | (cur, total) => void | console.log | 进度回调 |
返回值: Promise<Blob> — Excel 文件的 Blob 对象
特性
- 生成
.xlsx格式(Office Open XML),最大兼容性 - 表格元素:自动解析 HTML 表格结构,支持合并单元格(colspan/rowspan)、样式保留
- 文本元素:提取文本内容及样式(字体、字号、加粗、颜色、对齐)
- 图片元素:嵌入为 Excel 图片
- 其他元素:通过 snapdom 截图转为图片嵌入(支持 ECharts、Fabric 等自定义元素)
- 多面板/多页面自动创建多个工作表(Sheet)
- 自动列宽适配
