@@ -16,7 +16,7 @@ import {
16
16
resolveVariableAndFilter ,
17
17
ScopedContext
18
18
} from 'amis-core' ;
19
- import type { Word } from 'office-viewer' ;
19
+ import type { Word , Excel } from 'office-viewer' ;
20
20
import { Spinner } from 'amis-ui' ;
21
21
import { Payload } from '../types' ;
22
22
@@ -55,13 +55,15 @@ export default class OfficeViewer extends React.Component<
55
55
> {
56
56
rootElement : React . RefObject < HTMLDivElement > ;
57
57
58
- word : Word ;
58
+ office : Word | Excel ;
59
59
60
60
fileName ?: string ;
61
61
62
62
// 文档数据,避免 update 参数的时候重复加载
63
63
document ?: any ;
64
64
65
+ finalSrc ?: string ;
66
+
65
67
constructor ( props : OfficeViewerProps ) {
66
68
super ( props ) ;
67
69
this . rootElement = React . createRef ( ) ;
@@ -112,7 +114,7 @@ export default class OfficeViewer extends React.Component<
112
114
this . renderWord ( ) ;
113
115
} else {
114
116
// 默认只更新变量提升性能
115
- this . word ?. updateVariable ( ) ;
117
+ this . office ?. updateVariable ( ) ;
116
118
}
117
119
}
118
120
}
@@ -124,11 +126,11 @@ export default class OfficeViewer extends React.Component<
124
126
const actionType = action ?. actionType as string ;
125
127
126
128
if ( actionType === 'saveAs' ) {
127
- this . word ?. download ( args ?. name || this . fileName ) ;
129
+ this . office ?. download ( args ?. name || this . fileName ) ;
128
130
}
129
131
130
132
if ( actionType === 'print' ) {
131
- this . word ?. print ( ) ;
133
+ this . office ?. print ( ) ;
132
134
}
133
135
}
134
136
@@ -171,6 +173,9 @@ export default class OfficeViewer extends React.Component<
171
173
console . warn ( 'file src is empty' ) ;
172
174
return ;
173
175
}
176
+
177
+ this . finalSrc = finalSrc ;
178
+
174
179
let response : Payload ;
175
180
176
181
this . setState ( {
@@ -195,32 +200,69 @@ export default class OfficeViewer extends React.Component<
195
200
}
196
201
}
197
202
203
+ async initOffice ( officeViewer : any , file ?: ArrayBuffer ) {
204
+ const {
205
+ wordOptions,
206
+ excelOptions,
207
+ env,
208
+ src,
209
+ data,
210
+ translate : __
211
+ } = this . props ;
212
+ const createOfficeViewer = officeViewer . createOfficeViewer ;
213
+ const office = await createOfficeViewer (
214
+ file || this . document ,
215
+ { } ,
216
+ this . finalSrc
217
+ ) ;
218
+
219
+ if ( office instanceof officeViewer . Word ) {
220
+ office . updateOptions ( {
221
+ ...wordOptions ,
222
+ data,
223
+ evalVar : this . evalVar . bind ( this )
224
+ } ) ;
225
+ } else if ( office instanceof officeViewer . Excel ) {
226
+ office . updateOptions ( {
227
+ ...excelOptions ,
228
+ data,
229
+ evalVar : this . evalVar . bind ( this )
230
+ } ) ;
231
+ await office . loadExcel ( ) ;
232
+ }
233
+
234
+ return office ;
235
+ }
236
+
198
237
/**
199
238
* 渲染远端文件
200
239
*/
201
240
async renderRemoteWord ( ) {
202
- const { wordOptions, env, src, data, display, translate : __ } = this . props ;
241
+ const {
242
+ wordOptions,
243
+ excelOptions,
244
+ env,
245
+ src,
246
+ data,
247
+ display,
248
+ translate : __
249
+ } = this . props ;
203
250
204
251
if ( ! this . document ) {
205
252
return ;
206
253
}
207
254
208
255
import ( 'office-viewer' ) . then ( async ( officeViewer : any ) => {
209
- const Word = officeViewer . Word ;
210
- const word = new Word ( this . document , {
211
- ...wordOptions ,
212
- data,
213
- evalVar : this . evalVar . bind ( this )
214
- } ) ;
256
+ const office = await this . initOffice ( officeViewer ) ;
215
257
216
258
if ( display !== false ) {
217
- word . render ( this . rootElement ?. current ! ) ;
259
+ office . render ( this . rootElement ?. current ! ) ;
218
260
} else if ( display === false && this . rootElement ?. current ) {
219
261
// 设置为 false 后清空
220
262
this . rootElement . current . innerHTML = '' ;
221
263
}
222
264
223
- this . word = word ;
265
+ this . office = office ;
224
266
} ) ;
225
267
}
226
268
@@ -236,18 +278,14 @@ export default class OfficeViewer extends React.Component<
236
278
const data = reader . result as ArrayBuffer ;
237
279
238
280
import ( 'office-viewer' ) . then ( async ( officeViewer : any ) => {
239
- const Word = officeViewer . Word ;
240
- const word = new Word ( data , {
241
- ...wordOptions ,
242
- evalVar : this . evalVar . bind ( this )
243
- } ) ;
281
+ const office = await this . initOffice ( officeViewer , data ) ;
244
282
if ( display !== false ) {
245
- word . render ( this . rootElement ?. current ! ) ;
283
+ office . render ( this . rootElement ?. current ! ) ;
246
284
} else if ( display === false && this . rootElement ?. current ) {
247
285
// 设置为 false 后清空
248
286
this . rootElement . current . innerHTML = '' ;
249
287
}
250
- this . word = word ;
288
+ this . office = office ;
251
289
} ) ;
252
290
} ;
253
291
reader . readAsArrayBuffer ( file ) ;
0 commit comments