@@ -19,15 +19,11 @@ import {
1919 ExcelInputOptionsBrowser
2020} from "../../shared/types"
2121import { DataFrame , NDframe , Series } from '../../'
22-
23- let XLSX : any ;
24-
25- try {
26- XLSX = require ( "xlsx" ) ;
27- } catch ( err ) {
28- console . info ( `xlsx not found. Please run "npm install xlsx" or "yarn add xlsx" in order to work with Excel files.` )
29-
30- }
22+ import {
23+ read ,
24+ writeFile ,
25+ utils
26+ } from "xlsx" ;
3127
3228/**
3329 * Reads a JSON file from local or remote location into a DataFrame.
@@ -64,9 +60,9 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
6460 }
6561 response . arrayBuffer ( ) . then ( arrBuf => {
6662 const arrBufInt8 = new Uint8Array ( arrBuf ) ;
67- const workbook = XLSX . read ( arrBufInt8 , { type : "array" } )
63+ const workbook = read ( arrBufInt8 , { type : "array" } )
6864 const worksheet = workbook . Sheets [ workbook . SheetNames [ sheet ] ] ;
69- const data = XLSX . utils . sheet_to_json ( worksheet ) ;
65+ const data = utils . sheet_to_json ( worksheet ) ;
7066 const df = new DataFrame ( data , frameConfig ) ;
7167 resolve ( df ) ;
7268 } ) ;
@@ -78,9 +74,9 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
7874 } else if ( file instanceof File ) {
7975 const arrBuf = await file . arrayBuffer ( )
8076 const arrBufInt8 = new Uint8Array ( arrBuf ) ;
81- const workbook = XLSX . read ( arrBufInt8 , { type : "array" } )
77+ const workbook = read ( arrBufInt8 , { type : "array" } )
8278 const worksheet = workbook . Sheets [ workbook . SheetNames [ sheet ] ] ;
83- const data = XLSX . utils . sheet_to_json ( worksheet ) ;
79+ const data = utils . sheet_to_json ( worksheet ) ;
8480 const df = new DataFrame ( data , frameConfig ) ;
8581 return df ;
8682 } else {
@@ -122,10 +118,10 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
122118 data = [ cols , ...row ]
123119 }
124120
125- const worksheet = XLSX . utils . aoa_to_sheet ( data ) ;
126- const wb = XLSX . utils . book_new ( ) ;
127- XLSX . utils . book_append_sheet ( wb , worksheet , sheetName ) ;
128- XLSX . writeFile ( wb , `${ fileName } ` )
121+ const worksheet = utils . aoa_to_sheet ( data ) ;
122+ const wb = utils . book_new ( ) ;
123+ utils . book_append_sheet ( wb , worksheet , sheetName ) ;
124+ writeFile ( wb , `${ fileName } ` )
129125} ;
130126
131127export {
0 commit comments