@chang-code-hub/univer-import-export

中文文档
0.1.42发布于 2025/08/12MITTypeScriptESM + CJS近 30 天下载 7090

Excel/CSV import and export library for Univer spreadsheets with full format preservation

原始使用文档
文档目录50 节
  1. Features
  2. Installation
  3. Usage
  4. Import Excel to Univer
  5. Import CSV to Univer
  6. Export Univer to Excel
  7. Export Univer to CSV
  8. API Reference
  9. LuckyExcel.transformExcelToUniver(file, callback, errorHand…
  10. LuckyExcel.transformCsvToUniver(file, callback, errorHandle…
  11. LuckyExcel.transformUniverToExcel(params)
  12. LuckyExcel.transformUniverToCsv(params)
  13. Browser Support
  14. Project Structure
  15. Key Implementation Details
  16. Special Character Handling
  17. Empty Sheet Preservation
  18. Formula Support
  19. Development
  20. Building
  21. Publishing
  22. Testing
  23. Key Improvements
  24. v0.1.40 (Latest) - Streamlined Export System
  25. v0.1.39 - Backend Integration
  26. v0.1.24 - Core Stability
  27. Dependencies
  28. Core Dependencies
  29. Build Dependencies
  30. Related Projects & References
  31. Core Dependencies
  32. Implementation Examples
  33. Documentation
  34. Known Issues & Solutions
  35. ✅ Resolved Issues
  36. ⚠️ Export Limitations & Solutions
  37. 🔧 Backend Post-Processing Solution
  38. Architecture
  39. Django Integration Example
  40. What Gets Fixed
  41. Frontend Integration
  42. Setup Requirements
  43. 📊 Current Export Status (v0.1.40)
  44. ✅ Fully Working Features
  45. ⚠️ Known Visual/Display Issues
  46. 🔧 Backend Post-Processing (Recommended)
  47. Contributing
  48. License
  49. Credits
  50. Support

@mertdeveci55/univer-import-export

A robust Excel/CSV import and export library for Univer spreadsheets with full format preservation, including formulas, styling, charts, and conditional formatting.

npm version License: MIT

Features

✅ Import Support

  • Excel files (.xlsx, .xls)
  • CSV files (.csv)
  • Preserves ALL sheets (including empty ones)
  • Handles special characters in sheet names (>>>, etc.)
  • Maintains exact sheet order
  • Full styling preservation (fonts, colors, borders, alignment)
  • Formula and calculated value retention (including TRANSPOSE and array formulas)
  • Merged cells support
  • Images and charts
  • Conditional formatting
  • Data validation
  • Hyperlinks and rich text

✅ Export Support

  • Excel files (.xlsx)
  • CSV files (.csv)
  • Full formatting preservation
  • Formula export
  • Named ranges
  • Multiple sheets

Installation

npm install @mertdeveci55/univer-import-export

or

yarn add @mertdeveci55/univer-import-export

Usage

Import Excel to Univer

import { LuckyExcel } from '@mertdeveci55/univer-import-export';

// Handle file input
const fileInput = document.getElementById('file-input');
fileInput.addEventListener('change', (e) => {
    const file = e.target.files[0];
    
    LuckyExcel.transformExcelToUniver(
        file,
        (univerData) => {
            // Use the Univer data
            console.log('Converted data:', univerData);
            
            // Create Univer instance with the data
            univer.createUnit(UniverInstanceType.UNIVER_SHEET, univerData);
        },
        (error) => {
            console.error('Import error:', error);
        }
    );
});

Import CSV to Univer

import { LuckyExcel } from '@mertdeveci55/univer-import-export';

LuckyExcel.transformCsvToUniver(
    csvFile,
    (univerData) => {
        // Use the converted CSV data
        univer.createUnit(UniverInstanceType.UNIVER_SHEET, univerData);
    },
    (error) => {
        console.error('CSV import error:', error);
    }
);

Export Univer to Excel

import { LuckyExcel } from '@mertdeveci55/univer-import-export';

// Get Univer snapshot
const snapshot = univer.getActiveWorkbook().save();

LuckyExcel.transformUniverToExcel({
    snapshot: snapshot,
    fileName: 'my-spreadsheet.xlsx',
    success: () => {
        console.log('Export successful');
    },
    error: (err) => {
        console.error('Export error:', err);
    }
});

Export Univer to CSV

import { LuckyExcel } from '@mertdeveci55/univer-import-export';

const snapshot = univer.getActiveWorkbook().save();

LuckyExcel.transformUniverToCsv({
    snapshot: snapshot,
    fileName: 'my-data.csv',
    sheetName: 'Sheet1', // Optional: specific sheet to export
    success: () => {
        console.log('CSV export successful');
    },
    error: (err) => {
        console.error('CSV export error:', err);
    }
});

API Reference

LuckyExcel.transformExcelToUniver(file, callback, errorHandler)

Converts Excel file to Univer format.

  • file: File - The Excel file (.xlsx or .xls)
  • callback: (data: IWorkbookData) => void - Success callback with converted data
  • errorHandler: (error: Error) => void - Error callback

LuckyExcel.transformCsvToUniver(file, callback, errorHandler)

Converts CSV file to Univer format.

  • file: File - The CSV file
  • callback: (data: IWorkbookData) => void - Success callback
  • errorHandler: (error: Error) => void - Error callback

LuckyExcel.transformUniverToExcel(params)

Exports Univer data to Excel file.

Parameters object:

  • snapshot: any - Univer workbook snapshot
  • fileName: string - Output filename (optional, default: excel_[timestamp].xlsx)
  • getBuffer: boolean - Return buffer instead of downloading (optional, default: false)
  • success: (buffer?: Buffer) => void - Success callback
  • error: (err: Error) => void - Error callback

LuckyExcel.transformUniverToCsv(params)

Exports Univer data to CSV file.

Parameters object:

  • snapshot: any - Univer workbook snapshot
  • fileName: string - Output filename (optional, default: csv_[timestamp].csv)
  • sheetName: string - Specific sheet to export (optional, exports all if not specified)
  • getBuffer: boolean - Return content instead of downloading (optional, default: false)
  • success: (content?: string) => void - Success callback
  • error: (err: Error) => void - Error callback

Browser Support

The library works in all modern browsers that support:

  • ES6+
  • File API
  • Blob API

Project Structure

src/
├── ToLuckySheet/         # Excel → LuckySheet conversion
│   ├── LuckyFile.ts      # Main file orchestrator - handles sheet discovery
│   ├── LuckySheet.ts     # Individual sheet processor
│   ├── ReadXml.ts        # XML parser with special character escaping
│   ├── LuckyCell.ts      # Cell data processor
│   └── ...
├── LuckyToUniver/        # LuckySheet → Univer conversion
│   ├── UniverWorkBook.ts # Workbook structure converter
│   ├── UniverSheet.ts    # Sheet data converter
│   └── ...
├── UniverToExcel/        # Univer → Excel export
│   ├── Workbook.ts       # Excel workbook builder using ExcelJS
│   └── ...
├── HandleZip.ts          # ZIP file operations using JSZip
└── main.ts               # Entry point with public API

dist/                     # Built output (ESM, CJS, UMD formats)
publish.sh                # Automated publishing script
gulpfile.js              # Build configuration
CLAUDE.md                # Detailed technical documentation

Key Implementation Details

Special Character Handling

Sheet names with special characters (like >>>) are handled through an escape/unescape mechanism in src/ToLuckySheet/ReadXml.ts:

// Escapes ">" to "__GT__" before XML parsing
escapeXmlAttributes(xmlString)
// Restores "__GT__" back to ">" after parsing
unescapeXmlAttributes(xmlString)

Empty Sheet Preservation

All sheets are preserved during import, even if completely empty. This maintains Excel file structure integrity.

Formula Support

Comprehensive formula support including:

  • Standard formulas (SUM, AVERAGE, IF, VLOOKUP, etc.)
  • Array formulas
  • TRANSPOSE formulas with proper array handling
  • Shared formulas
  • Named range references

Development

Building

npm install
npm run build   # Uses gulp to compile TypeScript and bundle

Publishing

Always use the publish script for releases:

./publish.sh

This script:

  1. Builds the project
  2. Increments version
  3. Commits changes
  4. Pushes to GitHub
  5. Publishes to npm

Testing

npm test

Key Improvements

v0.1.40 (Latest) - Streamlined Export System

  1. Excel Compatibility: Eliminated Excel recovery warnings completely
  2. Surgical Post-Processing: Conservative backend fixes that preserve all data integrity
  3. Production Safety: Reverted experimental fixes that caused corruption
  4. Documentation: Comprehensive documentation of remaining limitations and workarounds

v0.1.39 - Backend Integration

  1. openpyxl Integration: Full backend post-processing system for ExcelJS limitations
  2. Defined Names Fix: Complete solution for named ranges using Python/Django backend
  3. Dual-Mode Export: Frontend + backend export with automatic fallback

v0.1.24 - Core Stability

  1. Special Character Support: Handles sheet names with >>> and other special characters via escape/unescape mechanism
  2. Empty Sheet Preservation: Empty sheets are never skipped during import
  3. No Hardcoded Solutions: Removed all hardcoded sheet additions - all solutions are generic
  4. Sheet Order: Maintains exact sheet order from original file
  5. Style Preservation: Complete style mapping including bold, italic, colors, borders
  6. Formula Handling: Preserves both formulas and calculated values, including TRANSPOSE
  7. XLS Support: Automatic conversion of .xls files to .xlsx format
  8. Better Error Handling: Comprehensive error messages and detailed logging

Dependencies

Core Dependencies

Build Dependencies

  • gulp - Build orchestration
  • rollup - Module bundling
  • typescript - Type safety
  • terser - Minification (configured to preserve console.logs)

Core Dependencies

  • Univer - The spreadsheet engine this library supports
  • LuckySheet - Intermediate format inspiration
  • LuckyExcel - Original codebase this fork is based on

Implementation Examples

  • alphafrontend - Production implementation
    • See: src/utils/excel-import.ts for usage example
    • See: src/pages/SpreadsheetsPage.tsx for UI integration

Documentation

Known Issues & Solutions

✅ Resolved Issues

IssueSolutionVersion Fixed
Sheets with special characters (>>>) not importingEscape/unescape mechanism in ReadXml.tsv0.1.23+
AttributeList undefined errorsDefensive initializationv0.1.21+
Duplicate sheets appearingRemoved hardcoded sheet additionsv0.1.24
TRANSPOSE formulas not workingArray formula supportv0.1.18+
Border styles not importingAdded style collection in UniverWorkBookv0.1.38

⚠️ Export Limitations & Solutions

Due to limitations in the underlying ExcelJS library, some advanced Excel features have issues during export. However, we provide comprehensive solutions:

🔧 Backend Post-Processing Solution

For production applications, we recommend using backend post-processing to achieve 100% Excel compatibility:

Architecture

Frontend Export → Backend API → openpyxl Post-Processing → Fixed Excel File

Django Integration Example

from spreadsheets.import_export import UniverToExcelConverter, ExcelPostProcessor

# Step 1: Export with existing functionality (preserves all working features)
converter = UniverToExcelConverter()
excel_buffer = converter.convert(univer_data)

# Step 2: Apply surgical fixes for ExcelJS limitations
post_processor = ExcelPostProcessor()
fixed_buffer = post_processor.process_excel_buffer(
    excel_buffer.getvalue(), 
    univer_data
)

What Gets Fixed

  • ✅ Defined Names: All named ranges work perfectly in Excel
  • ✅ Array Formula Attributes: Proper XML attributes for spill ranges
  • ✅ Excel 365 Compatibility: Full support for modern Excel features
  • ✅ Performance: ~7ms processing overhead
  • ✅ Safety: Preserves ALL existing functionality

Frontend Integration

// Enhanced export with backend post-processing
const result = await exportToExcel({
  workbookData,
  fileName: 'spreadsheet.xlsx',
  useBackendExport: true,        // Enable backend processing
  enablePostProcessing: true,    // Fix ExcelJS limitations
  exportSpreadsheetToExcel: api.exportSpreadsheetToExcel
});

// Automatic fallback to frontend if backend unavailable
if (result.stats?.postProcessingApplied) {
  console.log('✅ Enhanced Excel compatibility applied!');
}

Setup Requirements

  1. Python Backend with openpyxl installed
  2. API Endpoint for post-processing
  3. Optional: Feature flag for gradual rollout

📊 Current Export Status (v0.1.40)

✅ Fully Working Features

  • ✅ Excel Compatibility: Files open without recovery warnings
  • ✅ Data Integrity: All formulas, values, and structure preserved perfectly
  • ✅ Named Ranges: All defined names work correctly in Excel (fixed in backend)
  • ✅ Import Operations: 100% functionality preserved for all Excel features

⚠️ Known Visual/Display Issues

IssueImpactStatusWorkaround
TRANSPOSE @ SymbolsFormulas show =@TRANSPOSE(@$N$43:$N$45)Cosmetic only - calculations workDocument as known issue
Border Style ChangesDashed borders may become solidVisual styling onlyOriginal design preserved in import
ExcelJS Core LimitationsSome advanced Excel features not supportedLibrary limitationUse backend post-processing

🔧 Backend Post-Processing (Recommended)

Status: Production-ready surgical fixes for maximum Excel compatibility.

What it fixes:

  • ✅ Removes Excel recovery warnings completely
  • ✅ Ensures all named ranges work perfectly
  • ✅ Maintains 100% data integrity and formula accuracy

What it doesn't fix (by design):

  • ⚠️ TRANSPOSE @ symbols (requires ExcelJS core changes)
  • ⚠️ Border style inconsistencies (requires ExcelJS improvements)

Performance: ~7ms overhead for complete Excel compatibility.

Import functionality works perfectly - these limitations only affect export operations.

Contributing

Contributions are welcome! Please ensure:

  1. No hardcoded solutions - All fixes must be generic
  2. Extensive logging - Add console.log for debugging
  3. Use publish.sh - Never manually publish to npm
  4. Test edge cases - Including special characters, empty sheets
  5. Follow existing patterns - Check CLAUDE.md for architecture

License

MIT © mertdeveci

Credits

  • Original LuckyExcel by DreamNum
  • Univer spreadsheet engine
  • All contributors and issue reporters

Support

For issues and feature requests:

版本与发布记录

@chang-code-hub/univer-import-export 共发布 3 个版本,首次发布于 2025/08/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.42latest2025/08/121.2 年前
0.1.412025/08/121.2 年前
0.1.402025/08/121.2 年前

关于 @chang-code-hub/univer-import-export 的常见问题

@chang-code-hub/univer-import-export 是什么?

Excel/CSV import and export library for Univer spreadsheets with full format preservation

如何安装 @chang-code-hub/univer-import-export?

在项目目录运行 npm install @chang-code-hub/univer-import-export,或使用 pnpm add / yarn add / bun add。

@chang-code-hub/univer-import-export 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chang-code-hub/univer-import-export 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chang-code-hub/univer-import-export 可以商用吗?

@chang-code-hub/univer-import-export 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chang-code-hub/univer-import-export 的下载量有多少?

根据 npm 官方下载统计,@chang-code-hub/univer-import-export 在 2026/10/10 – 2026/11/08 这 30 天内被下载 7,090 次,日均约 236 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chang-code-hub/univer-import-export 还在维护吗?

最新版本 0.1.42 发布于 2025/08/12(1.2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/08/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。