mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-25 07:04:39 +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
|
tag-ok: OK
|
||||||
csv-to-data:
|
csv-to-data:
|
||||||
title: 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:
|
texts:
|
||||||
label-json: JSON
|
label-json: JSON
|
||||||
label-yaml: YAML
|
label-yaml: YAML
|
||||||
@@ -3610,6 +3610,7 @@ tools:
|
|||||||
label-table-name: 'Table Name:'
|
label-table-name: 'Table Name:'
|
||||||
title-converted-data: Converted data
|
title-converted-data: Converted data
|
||||||
tag-convert: Convert
|
tag-convert: Convert
|
||||||
|
label-xlsx: XLSX
|
||||||
curl-generator:
|
curl-generator:
|
||||||
title: Curl Command Generator
|
title: Curl Command Generator
|
||||||
description: Generate CURL command lines
|
description: Generate CURL command lines
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
|
import * as XLSX from 'xlsx';
|
||||||
|
|
||||||
import { convertCsvToArray } from '../csv-to-json/csv-to-json.service';
|
import { convertCsvToArray } from '../csv-to-json/csv-to-json.service';
|
||||||
import { objectArrayToData } from '@/utils/objectarray.export';
|
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-csv-tab'), value: 'tsv' },
|
||||||
{ label: t('tools.csv-to-data.texts.label-markdown'), value: 'markdown' },
|
{ 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-xml'), value: 'xml' },
|
||||||
|
{ label: t('tools.csv-to-data.texts.label-xlsx'), value: 'xlsx' },
|
||||||
];
|
];
|
||||||
|
|
||||||
async function handleFileUpload(file: File) {
|
async function handleFileUpload(file: File) {
|
||||||
@@ -57,16 +59,32 @@ async function convertFile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = convertCsvToArray(csvContentValue, typedValues.value);
|
const data = convertCsvToArray(csvContentValue, typedValues.value);
|
||||||
convertedData.value = objectArrayToData(data, selectedFormat.value as ExportFormat, {
|
const outFormat = selectedFormat.value;
|
||||||
tableName: tableName.value,
|
if (outFormat === 'xlsx') {
|
||||||
nestify: nestify.value,
|
convertedData.value = '';
|
||||||
});
|
downloadXLSX(data, tableName.value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
convertedData.value = objectArrayToData(data, outFormat as ExportFormat, {
|
||||||
|
tableName: tableName.value,
|
||||||
|
nestify: nestify.value,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e: any) {
|
catch (e: any) {
|
||||||
error.value = e.toString();
|
error.value = e.toString();
|
||||||
return null;
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
Reference in new issue
Block a user