mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(CSV to Data): add xlsx format
This commit is contained in:
1 parent
4dc9dcfe91
commit
f5f2938817
2 files changed
+24
-5
No files matched your search
+2
-1
@@ -3587,7 +3587,7 @@ tools:
|
||||
tag-ok: OK
|
||||
csv-to-data:
|
||||
title: Csv to Data
|
||||
description: Convert CSV file to JSON, YAML, CSV, SQL INSERT, XML or Markdown
|
||||
description: Convert CSV file to JSON, YAML, CSV, SQL INSERT, XML, Markdown or XLSX
|
||||
texts:
|
||||
label-json: JSON
|
||||
label-yaml: YAML
|
||||
@@ -3610,6 +3610,7 @@ tools:
|
||||
label-table-name: 'Table Name:'
|
||||
title-converted-data: Converted data
|
||||
tag-convert: Convert
|
||||
label-xlsx: XLSX
|
||||
curl-generator:
|
||||
title: Curl Command Generator
|
||||
description: Generate CURL command lines
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts" setup>
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import * as XLSX from 'xlsx';
|
||||
|
||||
import { convertCsvToArray } from '../csv-to-json/csv-to-json.service';
|
||||
import { objectArrayToData } from '@/utils/objectarray.export';
|
||||
@@ -28,6 +29,7 @@ const formats = [
|
||||
{ label: t('tools.csv-to-data.texts.label-csv-tab'), value: 'tsv' },
|
||||
{ label: t('tools.csv-to-data.texts.label-markdown'), value: 'markdown' },
|
||||
{ label: t('tools.csv-to-data.texts.label-xml'), value: 'xml' },
|
||||
{ label: t('tools.csv-to-data.texts.label-xlsx'), value: 'xlsx' },
|
||||
];
|
||||
|
||||
async function handleFileUpload(file: File) {
|
||||
@@ -57,16 +59,32 @@ async function convertFile() {
|
||||
}
|
||||
|
||||
const data = convertCsvToArray(csvContentValue, typedValues.value);
|
||||
convertedData.value = objectArrayToData(data, selectedFormat.value as ExportFormat, {
|
||||
tableName: tableName.value,
|
||||
nestify: nestify.value,
|
||||
});
|
||||
const outFormat = selectedFormat.value;
|
||||
if (outFormat === 'xlsx') {
|
||||
convertedData.value = '';
|
||||
downloadXLSX(data, tableName.value);
|
||||
}
|
||||
else {
|
||||
convertedData.value = objectArrayToData(data, outFormat as ExportFormat, {
|
||||
tableName: tableName.value,
|
||||
nestify: nestify.value,
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (e: any) {
|
||||
error.value = e.toString();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
function downloadXLSX<T extends Record<string, any>>(data: T[], fileName: string = 'data') {
|
||||
const worksheet = XLSX.utils.json_to_sheet(data);
|
||||
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, fileName);
|
||||
|
||||
XLSX.writeFile(workbook, `${fileName}.xlsx`);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in new issue
Block a user