https://github.com/paperstrike/db-csv-to-excel
https://github.com/paperstrike/db-csv-to-excel
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/paperstrike/db-csv-to-excel
- Owner: PaperStrike
- License: isc
- Created: 2025-05-01T03:52:38.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-01T07:37:15.000Z (about 1 year ago)
- Last Synced: 2025-10-27T08:09:45.071Z (7 months ago)
- Language: TypeScript
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# db-csv-to-excel
A utility package to convert database CSV exports into formatted Excel (XLSX) files.
> README generated by AI. Looks great generally and I manually corrected some apparent errors but hasn't checked it thoroughly. Feel free to report any issues.
## Features
- Convert CSV data to Excel with custom column mapping
- Support for encrypted database columns (MySQL AES)
- Custom data processing per column
- Data sorting capabilities
- Custom styling for headers and content
- Configurable column widths
## Installation
```bash
npm install db-csv-to-excel
```
## Basic Usage
```typescript
import { convertDBCsvToExcel } from 'db-csv-to-excel'
// Convert CSV to Excel
await convertDBCsvToExcel(
'input.csv',
'output.xlsx',
{
// Required if any columns are encrypted
aesKey: 'your-encryption-key',
// Define your database columns
dbColumns: [
{ name: 'id' },
{ name: 'username' },
{ name: 'email', encrypted: true },
{ name: 'created_at', processor: value => new Date(value) },
],
// Define your Excel columns
excelColumns: [
{
title: 'ID',
from: row => row.id,
width: 10,
},
{
title: 'User',
from: row => row.username,
width: 20,
},
{
title: 'Email Address',
from: row => row.email,
width: 30,
},
{
title: 'Registration Date',
from: row => row.created_at.toLocaleDateString(),
width: 20,
},
],
// Set to true if your CSV has a header row
csvHasColumns: true,
// Set the CSV delimiter if not a comma
csvDelimiter: '\t',
},
)
```
## API Reference
### `convertDBCsvToExcel()`
Main function to convert CSV to Excel.
```typescript
declare function convertDBCsvToExcel(
inputFilePath: string | URL,
outputFilePath: string | URL,
options: ConvertParams
): Promise
```
### Options
| Parameter | Type | Description | Default |
|-----------|------|-------------|---------|
| `dbColumns` | `DBColumn[]` | Database column definitions | *Required* |
| `excelColumns` | `ExcelColumn[]` \| `(firstRow?: DBRow) => ExcelColumn[]` | Excel column definitions, either as array or function that gets first row | *Required* |
| `aesKey` | `string` | Encryption key for AES-encrypted columns | `''` (Required if any column has `encrypted: true`) |
| `sortFns` | `Array<(row) => number>` | Functions to determine sort order | `[]` |
| `sortMode` | `'asc'` \| `'desc'` | Sort direction | `'desc'` |
| `titleStyle` | `CellStyle` | Default style for title cells | `undefined` |
| `contentStyle` | `CellStyle` | Default style for content cells | `undefined` |
| `csvHasColumns` | `boolean` | Whether CSV file includes header row | `false` |
| `csvDelimiter` | `string` | CSV delimiter character | `','` |
### Types
#### `DBColumn`
```typescript
interface DBColumn {
name: string
encrypted?: boolean
processor?: (value: string) => T
}
```
#### `ExcelColumn`
```typescript
interface ExcelColumn {
title: string
from: (row: DBRow) => string | null | undefined
width?: number
titleStyle?: CellStyle
contentStyle?: CellStyle
}
```
## Examples
### Custom Styling
```typescript
import { convertDBCsvToExcel } from 'db-csv-to-excel'
await convertDBCsvToExcel(
'input.csv',
'output.xlsx',
{
dbColumns: [/* ... */],
excelColumns: [/* ... */],
titleStyle: {
font: { bold: true, color: { rgb: 'FFFFFF' } },
fill: { fgColor: { rgb: '4472C4' } },
},
contentStyle: {
font: { name: 'Arial' },
},
},
)
```
### Sorting
```typescript
await convertDBCsvToExcel(
'input.csv',
'output.xlsx',
{
dbColumns: [/* ... */],
excelColumns: [/* ... */],
sortFns: [
// Sort by creation date
row => row.created_at.getTime(),
// Then by ID
row => Number(row.id),
],
// Ascending order
sortMode: 'asc',
},
)
```
### Dynamic Excel Columns
You can generate Excel columns dynamically based on the first row of data:
```typescript
await convertDBCsvToExcel(
'input.csv',
'output.xlsx',
{
dbColumns: [/* ... */],
// Generate columns dynamically based on first row
excelColumns: (firstRow) => {
// Example: Create a column for each property in the first row
if (!firstRow) return []
return Object.keys(firstRow).map(key => ({
title: key.toUpperCase(),
from: row => row[key],
width: 15,
}))
},
},
)
```
## License
ISC