1
This commit is contained in:
parent
8ece78a3dc
commit
02a1a087ab
|
|
@ -83,7 +83,8 @@ const TradeList = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出当前订单数据(含入场/离场原因、入场思路等完整字段,便于后续分析)
|
// 导出当前订单数据(含入场/离场原因、入场思路等完整字段,便于后续分析)
|
||||||
const handleExport = () => {
|
// type: 'csv' | 'json'
|
||||||
|
const handleExport = (type = 'csv') => {
|
||||||
if (trades.length === 0) {
|
if (trades.length === 0) {
|
||||||
alert('暂无数据可导出')
|
alert('暂无数据可导出')
|
||||||
return
|
return
|
||||||
|
|
@ -137,58 +138,73 @@ const TradeList = () => {
|
||||||
return row
|
return row
|
||||||
})
|
})
|
||||||
|
|
||||||
// 转换为CSV格式 helper
|
|
||||||
const convertToCSV = (objArray) => {
|
|
||||||
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
|
|
||||||
let str = '';
|
|
||||||
|
|
||||||
if (array.length === 0) return '';
|
|
||||||
|
|
||||||
// Header
|
|
||||||
const headers = Object.keys(array[0]);
|
|
||||||
str += headers.join(',') + '\r\n';
|
|
||||||
|
|
||||||
// Rows
|
|
||||||
for (let i = 0; i < array.length; i++) {
|
|
||||||
let line = '';
|
|
||||||
for (const index in array[i]) {
|
|
||||||
if (line !== '') line += ',';
|
|
||||||
|
|
||||||
let value = array[i][index];
|
|
||||||
if (value === null || value === undefined) {
|
|
||||||
value = '';
|
|
||||||
} else {
|
|
||||||
value = String(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Escape quotes and wrap in quotes if necessary
|
|
||||||
// Excel needs double quotes to be escaped as ""
|
|
||||||
if (value.search(/("|,|\n|\r)/g) >= 0) {
|
|
||||||
value = '"' + value.replace(/"/g, '""') + '"';
|
|
||||||
}
|
|
||||||
line += value;
|
|
||||||
}
|
|
||||||
str += line + '\r\n';
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成文件名
|
|
||||||
const timestamp = new Date().toISOString().slice(0, 19).replace(/:/g, '-')
|
const timestamp = new Date().toISOString().slice(0, 19).replace(/:/g, '-')
|
||||||
const filename = `交易记录_${timestamp}.csv`
|
|
||||||
|
if (type === 'json') {
|
||||||
|
const filename = `交易记录_${timestamp}.json`
|
||||||
|
const dataStr = JSON.stringify(exportData, null, 2)
|
||||||
|
const dataBlob = new Blob([dataStr], { type: 'application/json' })
|
||||||
|
const url = URL.createObjectURL(dataBlob)
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = filename
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
document.body.removeChild(link)
|
||||||
|
URL.revokeObjectURL(url)
|
||||||
|
} else {
|
||||||
|
// 转换为CSV格式 helper
|
||||||
|
const convertToCSV = (objArray) => {
|
||||||
|
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
|
||||||
|
let str = '';
|
||||||
|
|
||||||
|
if (array.length === 0) return '';
|
||||||
|
|
||||||
// 创建并下载文件 (CSV with BOM for Excel)
|
// Header
|
||||||
const csvStr = convertToCSV(exportData)
|
const headers = Object.keys(array[0]);
|
||||||
const bom = '\uFEFF'
|
str += headers.join(',') + '\r\n';
|
||||||
const dataBlob = new Blob([bom + csvStr], { type: 'text/csv;charset=utf-8;' })
|
|
||||||
const url = URL.createObjectURL(dataBlob)
|
// Rows
|
||||||
const link = document.createElement('a')
|
for (let i = 0; i < array.length; i++) {
|
||||||
link.href = url
|
let line = '';
|
||||||
link.download = filename
|
for (const index in array[i]) {
|
||||||
document.body.appendChild(link)
|
if (line !== '') line += ',';
|
||||||
link.click()
|
|
||||||
document.body.removeChild(link)
|
let value = array[i][index];
|
||||||
URL.revokeObjectURL(url)
|
if (value === null || value === undefined) {
|
||||||
|
value = '';
|
||||||
|
} else if (typeof value === 'object') {
|
||||||
|
value = JSON.stringify(value);
|
||||||
|
} else {
|
||||||
|
value = String(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape quotes and wrap in quotes if necessary
|
||||||
|
// Excel needs double quotes to be escaped as ""
|
||||||
|
if (value.search(/("|,|\n|\r)/g) >= 0) {
|
||||||
|
value = '"' + value.replace(/"/g, '""') + '"';
|
||||||
|
}
|
||||||
|
line += value;
|
||||||
|
}
|
||||||
|
str += line + '\r\n';
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = `交易记录_${timestamp}.csv`
|
||||||
|
// 创建并下载文件 (CSV with BOM for Excel)
|
||||||
|
const csvStr = convertToCSV(exportData)
|
||||||
|
const bom = '\uFEFF'
|
||||||
|
const dataBlob = new Blob([bom + csvStr], { type: 'text/csv;charset=utf-8;' })
|
||||||
|
const url = URL.createObjectURL(dataBlob)
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = filename
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
document.body.removeChild(link)
|
||||||
|
URL.revokeObjectURL(url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 复制统计数据到剪贴板
|
// 复制统计数据到剪贴板
|
||||||
|
|
@ -373,9 +389,14 @@ const TradeList = () => {
|
||||||
重置
|
重置
|
||||||
</button>
|
</button>
|
||||||
{trades.length > 0 && (
|
{trades.length > 0 && (
|
||||||
<button className="btn-export" onClick={handleExport} title="导出Excel/CSV(含入场/离场原因、入场思路等),便于后续分析">
|
<>
|
||||||
导出 Excel ({trades.length})
|
<button className="btn-export" onClick={() => handleExport('csv')} title="导出Excel/CSV(含入场/离场原因、入场思路等),便于后续分析">
|
||||||
</button>
|
导出 Excel ({trades.length})
|
||||||
|
</button>
|
||||||
|
<button className="btn-export" onClick={() => handleExport('json')} style={{ backgroundColor: '#607D8B' }} title="导出JSON数据,方便程序处理">
|
||||||
|
导出 JSON ({trades.length})
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user