# @charyliu/data2file

> 提供浏览器、nodejs 环境下，将数据导出为文件的能力

Latest version **2.0.1** (published 2025-05-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install @charyliu/data2file
pnpm add @charyliu/data2file
yarn add @charyliu/data2file
bun add @charyliu/data2file
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-05-30 |
| First published | 2023-07-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | chary |
| Maintainers | charyliu |

## Links

- npm: https://www.npmjs.com/package/@charyliu/data2file
- Repository: https://github.com/Chary-Liu
- npm.io page: https://npm.io/package/@charyliu/data2file

## Recent versions

- 2.0.1 (latest) — 2025-05-30
- 2.0.0 — 2025-05-28
- 1.1.1 — 2025-05-23
- 1.1.0 — 2025-05-23
- 1.0.9 — 2024-02-28
- 1.0.8 — 2024-02-06
- 1.0.7 — 2024-02-06
- 1.0.6 — 2023-07-26
- 1.0.5 — 2023-07-26
- 1.0.4 — 2023-07-26
- 1.0.3 — 2023-07-12
- 1.0.2 — 2023-07-12
- 1.0.1 — 2023-07-12
- 1.0.0 — 2023-07-12

## README

# data2file

提供浏览器、nodejs 环境下，将数据导出为文件的能力

## Install

`npm install @charyliu/data2file`

## Types

```ts
/** 表格 */
export interface Table {
  /** 单元格数据 */
  cells: (string | number)[][];
  /** 配置 */
  option?: {
    /** 内部样式表 */
    styleSheet?: string;
    /** 样式类名方法，用于将样式类名应用到单元格，样式类名取styleSheet中定义的 */
    classFn?: (r: number, c: number, value: string | number) => string;
    /** 样式方法，用于将样式应用到单元格 */
    styleFn?: (r: number, c: number, value: string | number) => string;
    /** 合并单元格配置 */
    mergeConfig?: {
      /** 开始单元格，r-行，c-列，行列从0开始 */
      s: {
        r: number;
        c: number;
      };
      /** 结束单元格，r-行，c-列，行列从0开始 */
      e: {
        r: number;
        c: number;
      };
    }[];
  };
}

/** 浏览器环境导出表格文件 */
export declare const downloadTableFile: (
  table: Table,
  fileName: string
) => void;

/** nodejs环境导出表格文件 */
export declare const exportTableFile: (
  table: Table,
  filePath: string
) => Promise<unknown>;
```

## 使用示范

### 导出表格

```js
// 表格数据
const table = {
  cells: [
    ["index", "name", "score"],
    ["1", "张三", "33"],
    ["2", "李四", "44"],
  ],
  option: {
    styleSheet: `
      td { text-align: center; }
      .fw-b { font-weight: bold; }
    `,
    classFn: (r, c, v) => {
      if (r == 0) return `fw-b`;
    },
  }
};
// 浏览器环境示例：
import { downloadTableFile } from "@charyliu/data2file";
downloadTableFile(table, "test.xlsx");

// nodejs环境示例：
const { exportTableFile } = require("@charyliu/data2file");
const path = require("path");
exportTableFile(table, path.resolve("./test.xlsx"));
```

---
_Source: https://npm.io/package/@charyliu/data2file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
