@@ -10,6 +10,8 @@ import { presetCellRenderFunc } from './column';
1010import { createDownloadLink } from './download' ;
1111import type { Page , Query } from './query' ;
1212
13+ type ExtsType = ( typeof Exporter . exts ) [ number ] ;
14+
1315/**
1416 * 从服务器获取数据的函数签名
1517 *
@@ -102,22 +104,17 @@ export class Exporter<T extends object, Q extends Query> {
102104 }
103105 }
104106
105- #buildHeader( ) : Array < ColumnDef > {
106- return this . #header. map ( h => ( { header : h } ) ) ;
107- }
108-
109107 /**
110- * 导出数据
111- *
112- * 将 {@link Exporter#fetch} 下载的数据导出给用户。
108+ * 导出由 {@link Exporter#fetch} 获取的数据
113109 *
114- * @param filename - 文件名,如果是 excel 和 ods,文件名部分也作为工作表的名称;
110+ * @param filename - 文件名,包含扩展名,根据扩展名决定导出类型。 如果是 excel 和 ods,文件名部分也作为工作表的名称;
115111 *
112+ * @remarks
116113 * NOTE: 这将通过浏览器创建一个自动下载的功能。
117114 */
118- async export ( filename : `${string } ${( typeof Exporter . exts ) [ number ] } `) : Promise < void > {
115+ async export ( filename : `${string } ${ExtsType } `) : Promise < void > {
119116 const index = filename . lastIndexOf ( '.' ) ;
120- const ext = filename . slice ( index ) ;
117+ const ext = filename . slice ( index ) as ( typeof Exporter . exts ) [ number ] ;
121118 const basename = filename . slice ( 0 , index ) ;
122119
123120 switch ( ext ) {
@@ -126,15 +123,6 @@ export class Exporter<T extends object, Q extends Query> {
126123 createDownloadLink ( content , filename , 'text/csv' ) ;
127124 break ;
128125 }
129- case '.xlsx' : {
130- const content = await writeXlsx ( this . buildWriteOptions ( basename ) ) ;
131- createDownloadLink (
132- content . buffer as ArrayBuffer ,
133- filename ,
134- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
135- ) ;
136- break ;
137- }
138126 case '.md' : {
139127 const sheet = {
140128 name : basename ,
@@ -145,15 +133,24 @@ export class Exporter<T extends object, Q extends Query> {
145133 createDownloadLink ( content , filename , 'text/markdown' ) ;
146134 break ;
147135 }
136+ case '.xlsx' : {
137+ const content = await writeXlsx ( this . #buildWriteOptions( basename ) ) ;
138+ createDownloadLink (
139+ content . buffer as ArrayBuffer ,
140+ filename ,
141+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
142+ ) ;
143+ break ;
144+ }
148145 case '.ods' : {
149- const content = await writeOds ( this . buildWriteOptions ( basename ) ) ;
146+ const content = await writeOds ( this . # buildWriteOptions( basename ) ) ;
150147 createDownloadLink ( content . buffer as ArrayBuffer , filename , 'application/vnd.oasis.opendocument.spreadsheet' ) ;
151148 break ;
152149 }
153150 }
154151 }
155152
156- buildWriteOptions ( basename : string ) : WriteOptions {
153+ # buildWriteOptions( basename : string ) : WriteOptions {
157154 const now = new Date ( ) ;
158155 return {
159156 sheets : [
@@ -170,4 +167,8 @@ export class Exporter<T extends object, Q extends Query> {
170167 } ,
171168 } ;
172169 }
170+
171+ #buildHeader( ) : Array < ColumnDef > {
172+ return this . #header. map ( h => ( { header : h } ) ) ;
173+ }
173174}
0 commit comments