fix(export): preserve numeric types in XLSX files

This commit is contained in:
t8y2 committed 2026-07-12 09:45:27 +08:00
1 parent c2b6aef315
commit be66d6f46b
9 files changed
+282 -45

No files matched your search

+3 -2
View File
@@ -1682,11 +1682,12 @@ function downloadTextFile(filePath: string, fallbackFileName: string, content: s
URL.revokeObjectURL(url);
}
export async function exportQueryResultXlsx(filePath: string, sheetName: string | undefined, columns: string[], rows: readonly (readonly XlsxCellValue[])[]): Promise<void> {
export async function exportQueryResultXlsx(filePath: string, sheetName: string | undefined, columns: string[], columnTypes: string[], rows: readonly (readonly XlsxCellValue[])[]): Promise<void> {
const { buildXlsxWorkbook } = await import("@/lib/export/xlsxExport");
const workbook = buildXlsxWorkbook({
sheetName: sheetName || "Export",
columns,
columnTypes,
rows,
});
const fileName = filePath.split(/[\\/]/).pop() || "export.xlsx";
@@ -1701,7 +1702,7 @@ export async function exportQueryResultXlsx(filePath: string, sheetName: string
URL.revokeObjectURL(url);
}
export async function exportQueryResultsXlsx(filePath: string, worksheets: readonly { sheetName?: string; columns: string[]; rows: readonly (readonly XlsxCellValue[])[] }[]): Promise<void> {
export async function exportQueryResultsXlsx(filePath: string, worksheets: readonly { sheetName?: string; columns: string[]; columnTypes?: string[]; rows: readonly (readonly XlsxCellValue[])[] }[]): Promise<void> {
const { buildXlsxWorkbookMulti } = await import("@/lib/export/xlsxExport");
const workbook = buildXlsxWorkbookMulti(worksheets);
const fileName = filePath.split(/[\\/]/).pop() || "export.xlsx";
+3 -2
View File
@@ -2382,18 +2382,19 @@ export async function exportTableDataCsv(options: TableCsvExportOptions): Promis
return invoke("export_table_data_csv", { request: options });
}
export async function exportQueryResultXlsx(filePath: string, sheetName: string | undefined, columns: string[], rows: readonly (readonly XlsxCellValue[])[]): Promise<void> {
export async function exportQueryResultXlsx(filePath: string, sheetName: string | undefined, columns: string[], columnTypes: string[], rows: readonly (readonly XlsxCellValue[])[]): Promise<void> {
return invoke("export_query_result_xlsx", {
request: {
filePath,
sheetName,
columns,
columnTypes,
rows,
},
});
}
export async function exportQueryResultsXlsx(filePath: string, worksheets: readonly { sheetName?: string; columns: string[]; rows: readonly (readonly XlsxCellValue[])[] }[]): Promise<void> {
export async function exportQueryResultsXlsx(filePath: string, worksheets: readonly { sheetName?: string; columns: string[]; columnTypes?: string[]; rows: readonly (readonly XlsxCellValue[])[] }[]): Promise<void> {
return invoke("export_query_results_xlsx", {
request: {
filePath,
+53 -2
View File
@@ -3,6 +3,7 @@ export type XlsxCellValue = string | number | boolean | null | undefined;
export interface XlsxWorksheetData {
sheetName?: string;
columns: readonly string[];
columnTypes?: readonly string[];
rows: readonly (readonly XlsxCellValue[])[];
}
@@ -103,7 +104,51 @@ function estimateColumnWidths(columns: readonly string[], rows: readonly (readon
});
}
function cellXml(value: XlsxCellValue, rowIndex: number, colIndex: number, style?: number): string {
function isNumericColumnType(columnType?: string): boolean {
const base = (columnType || "")
.trim()
.toLowerCase()
.split(/[\s([]/, 1)[0];
return new Set([
"bit",
"tinyint",
"smallint",
"mediumint",
"int",
"integer",
"bigint",
"int2",
"int4",
"int8",
"uint8",
"uint16",
"uint32",
"uint64",
"uint128",
"uint256",
"float",
"float4",
"float8",
"float32",
"float64",
"real",
"double",
"decimal",
"numeric",
"number",
"money",
"smallmoney",
]).has(base);
}
function safeExcelNumber(value: string): string | undefined {
const trimmed = value.trim();
if (!trimmed || !Number.isFinite(Number(trimmed))) return undefined;
const significantDigits = (trimmed.split(/[eE]/, 1)[0].match(/\d/g) || []).join("").replace(/^0+/, "").length;
return significantDigits <= 15 ? trimmed : undefined;
}
function cellXml(value: XlsxCellValue, rowIndex: number, colIndex: number, style?: number, columnType?: string): string {
const ref = cellRef(rowIndex, colIndex);
const styleAttr = style == null ? "" : ` s="${style}"`;
if (value == null) return `<c r="${ref}"${styleAttr}/>`;
@@ -113,6 +158,12 @@ function cellXml(value: XlsxCellValue, rowIndex: number, colIndex: number, style
if (typeof value === "boolean") {
return `<c r="${ref}" t="b"${styleAttr}><v>${value ? 1 : 0}</v></c>`;
}
if (typeof value === "string" && isNumericColumnType(columnType)) {
// Excel keeps only 15 significant digits, so preserve higher-precision
// database values as text rather than silently rounding them.
const number = safeExcelNumber(value);
if (number !== undefined) return `<c r="${ref}"${styleAttr}><v>${number}</v></c>`;
}
return `<c r="${ref}" t="inlineStr"${styleAttr}><is><t>${escapeXml(String(value))}</t></is></c>`;
}
@@ -127,7 +178,7 @@ function worksheetXml(data: XlsxWorksheetData): string {
const bodyXml = rows
.map((row, rowIndex) => {
const excelRowIndex = rowIndex + 2;
const cells = columns.map((_, colIndex) => cellXml(row[colIndex], excelRowIndex - 1, colIndex)).join("");
const cells = columns.map((_, colIndex) => cellXml(row[colIndex], excelRowIndex - 1, colIndex, undefined, data.columnTypes?.[colIndex])).join("");
return `<row r="${excelRowIndex}">${cells}</row>`;
})
.join("");