Excel 导入导出

FlexSheet支持通过 'save''load' 方法保存\ load xlsx文件。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjGrid from '@grapecity/wijmo.grid'; import * as wjFlexSheet from '@grapecity/wijmo.grid.sheet'; import { getData, getCountries, getProducts } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { let excelIOSheet = new wjFlexSheet.FlexSheet('#excelIOSheet'); excelIOSheet.addBoundSheet('Country', getData(50)); excelIOSheet.addUnboundSheet('Unbound', 20, 8); excelIOSheet.deferUpdate(() => { let sheetIdx, sheetName, colIdx, rowIdx; for (sheetIdx = excelIOSheet.sheets.length - 1; sheetIdx >= 0; sheetIdx--) { excelIOSheet.selectedSheetIndex = sheetIdx; sheetName = excelIOSheet.selectedSheet.name; if (sheetName === 'Country') { // initialize the dataMap for the bound sheet. initDataMapForBindingSheet(excelIOSheet); } else { for (colIdx = 0; colIdx < excelIOSheet.columns.length; colIdx++) { for (rowIdx = 0; rowIdx < excelIOSheet.rows.length; rowIdx++) { excelIOSheet.setCellData(rowIdx, colIdx, colIdx + rowIdx); } } } } }); onClick('loadXlsx', () => { let fileInput = document.querySelector('#importFile'); if (fileInput.files[0]) { excelIOSheet.loadAsync(fileInput.files[0]); } }); onClick('saveXlsx', () => { let fileName = document.querySelector('#fileName').value; if (fileName == null || fileName === '') { fileName = 'FlexSheet.xlsx'; } excelIOSheet.saveAsync(fileName); }); // initialize the dataMap for the bound sheet. function initDataMapForBindingSheet(flexSheet) { let column; if (flexSheet) { column = flexSheet.columns.getColumn('countryId'); if (column && !column.dataMap) { column.dataMap = buildDataMap(getCountries()); } column = flexSheet.columns.getColumn('productId'); if (column && !column.dataMap) { column.dataMap = buildDataMap(getProducts()); } column = flexSheet.columns.getColumn('amount'); if (column) { column.format = 'c2'; } } } // build a data map from a string array using the indices as keys function buildDataMap(items) { let map = []; for (let i = 0; i < items.length; i++) { map.push({ key: i, value: items[i] }); } return new wjGrid.DataMap(map, 'key', 'value'); } function onClick(id, fn) { document.querySelector('#' + id).addEventListener('click', fn); } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexSheet Excel I/O</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div id="excelIOSheet"></div> <div class="form-inline well well-lg"> <input type="file" class="form-control" id="importFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <button id="loadXlsx" class="btn btn-default">Load</button> </div> <div class="form-inline well well-lg"> File Name: <input id="fileName" type="text" class="form-control" /> <button id="saveXlsx" class="btn btn-default">Save</button> </div> </div> </body> </html> let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], products = ['Widget', 'Gadget', 'Doohickey']; export function getData(count) { let data = [], i = 0, countryId, productId; for (; i < count; i++) { countryId = Math.floor(Math.random() * countries.length); productId = Math.floor(Math.random() * products.length); data.push({ id: i, countryId: countryId, productId: productId, date: new Date(2014, i % 12, i % 28), amount: Math.random() * 10000, active: i % 4 === 0 }); } return data; } export function getCountries() { return countries; } export function getProducts() { return products; } .wj-flexsheet { height: 400px; margin: 6px 0; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { Component, enableProdMode, NgModule, Inject, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { WjGridSheetModule } from '@grapecity/wijmo.angular2.grid.sheet'; import * as wjcGrid from '@grapecity/wijmo.grid'; import * as wjcSheet from '@grapecity/wijmo.grid.sheet'; import { DataService } from './app.data'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; fileName: string = ''; private _countries: string[]; private _products: string[]; constructor(@Inject(DataService) dataSvc: DataService) { this.data = dataSvc.getData(50); this._countries = dataSvc.countries; this._products = dataSvc.products; } @ViewChild('flex') flex: wjcSheet.FlexSheet; initializeFlexSheet(flex: wjcSheet.FlexSheet) { flex.deferUpdate(() => { let sheetIdx, sheetName, colIdx, rowIdx; // initialize the dataMap for the bound sheet. if (flex) { for (sheetIdx = 0; sheetIdx < flex.sheets.length; sheetIdx++) { flex.selectedSheetIndex = sheetIdx; sheetName = flex.selectedSheet.name; if (sheetName === 'Country') { let column = flex.columns.getColumn('countryId'); if (column && !column.dataMap) { column.dataMap = this._buildDataMap(this._countries); } column = flex.columns.getColumn('productId'); if (column && !column.dataMap) { column.dataMap = this._buildDataMap(this._products); } column = flex.columns.getColumn('amount'); if (column) { column.format = 'c2'; } } else { for (colIdx = 0; colIdx < flex.columns.length; colIdx++) { for (rowIdx = 0; rowIdx < flex.rows.length; rowIdx++) { flex.setCellData(rowIdx, colIdx, colIdx + rowIdx); } } } } flex.selectedSheetIndex = 0; } }); } // Load xlsx file to FlexSheet. load () { let fileInput = <HTMLInputElement>document.getElementById('importFile'); if (this.flex && fileInput.files[0]) { this.flex.loadAsync(fileInput.files[0]); } } // Save FlexSheet to xlsx file. save () { let fileName; if (!!this.fileName) { fileName = this.fileName; } else { fileName = 'FlexSheet.xlsx'; } this.flex.saveAsync(fileName); } // build a data map from a string array using the indices as keys private _buildDataMap(items: string[]) { let map = []; for (let i = 0; i < items.length; i++) { map.push({ key: i, value: items[i] }); } return new wjcGrid.DataMap(map, 'key', 'value'); }; } @NgModule({ imports: [WjGridSheetModule, BrowserModule, FormsModule], providers: [DataService], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexSheet Excel I/O</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet #flex (initialized)="initializeFlexSheet(flex)"> <wj-sheet [name]="'Country'" [(itemsSource)]="data"></wj-sheet> <wj-sheet [name]="'Unbound'" [rowCount]="20" [columnCount]="8"></wj-sheet> </wj-flex-sheet> <div class="form-inline well well-lg"> <input type="file" class="form-control" id="importFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <button class="btn btn-default" (click)="load()">Load</button> </div> <div class="form-inline well well-lg"> File Name: <input type="text" class="form-control" [(ngModel)]="fileName" /> <button class="btn btn-default" (click)="save()">Save</button> </div> </div> import { Injectable } from '@angular/core'; @Injectable() export class DataService { countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; products = ['Widget', 'Gadget', 'Doohickey']; getData(count: number) { let data = [], i = 0, countryId, productId; for (; i < count; i++) { countryId = Math.floor(Math.random() * this.countries.length); productId = Math.floor(Math.random() * this.products.length); data.push({ id: i, countryId: countryId, productId: productId, date: new Date(2014, i % 12, i % 28), amount: Math.random() * 10000, active: i % 4 === 0 }); } return data; } } .wj-flexsheet { height: 400px; margin: 6px 0; } <template> <div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet :initialized="initializeFlexSheet"> <wj-sheet :name="'Country'" :itemsSource="data"></wj-sheet> <wj-sheet :name="'Unbound'" :rowCount="20" :columnCount="8"></wj-sheet> </wj-flex-sheet> <div class="form-inline well well-lg"> <input type="file" class="form-control" id="importFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" > <button class="btn btn-default" @click="load">Load</button> </div> <div class="form-inline well well-lg"> File Name: <input type="text" class="form-control" v-model="fileName"> <button class="btn btn-default" @click="save">Save</button> </div> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import Vue from "vue"; import '@grapecity/wijmo.vue2.grid.sheet'; import * as wjcGrid from '@grapecity/wijmo.grid'; import * as wjcSheet from '@grapecity/wijmo.grid.sheet'; import { getData, getCountries, getProducts } from "./data"; let App = Vue.extend({ name: "app", data: function() { return { data: getData(50), countries: getCountries(), products: getProducts(), fileName: "", flex: null }; }, methods: { initializeFlexSheet: function(flex) { flex.deferUpdate(() => { let sheetIdx, sheetName, colIdx, rowIdx; // initialize the dataMap for the bound sheet. if (flex) { for ( sheetIdx = 0; sheetIdx < flex.sheets.length; sheetIdx++ ) { flex.selectedSheetIndex = sheetIdx; sheetName = flex.selectedSheet.name; if (sheetName === "Country") { let column = flex.columns.getColumn("countryId"); if (column && !column.dataMap) { column.dataMap = this._buildDataMap( this.countries ); } column = flex.columns.getColumn("productId"); if (column && !column.dataMap) { column.dataMap = this._buildDataMap( this.products ); } column = flex.columns.getColumn("amount"); if (column) { column.format = "c2"; } } else { for ( colIdx = 0; colIdx < flex.columns.length; colIdx++ ) { for ( rowIdx = 0; rowIdx < flex.rows.length; rowIdx++ ) { flex.setCellData( rowIdx, colIdx, colIdx + rowIdx ); } } } } flex.selectedSheetIndex = 0; } }); this.flex=flex; }, // Load xlsx file to FlexSheet. load: function() { let fileInput = document.getElementById("importFile"); if (this.flex && fileInput.files[0]) { this.flex.loadAsync(fileInput.files[0]); } }, // Save FlexSheet to xlsx file. save: function() { let fileName; if (!!this.fileName) { fileName = this.fileName; } else { fileName = "FlexSheet.xlsx"; } this.flex.saveAsync(fileName); }, // build a data map from a string array using the indices as keys _buildDataMap: function(items) { let map = []; for (let i = 0; i < items.length; i++) { map.push({ key: i, value: items[i] }); } return new wjcGrid.DataMap(map, "key", "value"); } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <style> .container-fluid .wj-flexsheet { height: 400px; margin: 6px 0; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/jszip/dist/jszip.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], products = ['Widget', 'Gadget', 'Doohickey']; export function getData(count) { let data = [], i = 0, countryId, productId; for (; i < count; i++) { countryId = Math.floor(Math.random() * countries.length); productId = Math.floor(Math.random() * products.length); data.push({ id: i, countryId: countryId, productId: productId, date: new Date(2014, i % 12, i % 28), amount: Math.random() * 10000, active: i % 4 === 0 }); } return data; } export function getCountries() { return countries; } export function getProducts() { return products; } import './app.css'; import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjcGrid from '@grapecity/wijmo.grid'; import * as wjGridSheet from "@grapecity/wijmo.react.grid.sheet"; import { getData, getCountries, getProducts } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(50), countries: getCountries(), products: getProducts(), fileName: "", flex: null }; } initializeFlexSheet(flex) { flex.deferUpdate(() => { let sheetIdx, sheetName, colIdx, rowIdx; // initialize the dataMap for the bound sheet. if (flex) { for (sheetIdx = 0; sheetIdx < flex.sheets.length; sheetIdx++) { flex.selectedSheetIndex = sheetIdx; sheetName = flex.selectedSheet.name; if (sheetName === "Country") { let column = flex.columns.getColumn("countryId"); if (column && !column.dataMap) { column.dataMap = this._buildDataMap(this.state.countries); } column = flex.columns.getColumn("productId"); if (column && !column.dataMap) { column.dataMap = this._buildDataMap(this.state.products); } column = flex.columns.getColumn("amount"); if (column) { column.format = "c2"; } } else { for (colIdx = 0; colIdx < flex.columns.length; colIdx++) { for (rowIdx = 0; rowIdx < flex.rows.length; rowIdx++) { flex.setCellData(rowIdx, colIdx, colIdx + rowIdx); } } } } flex.selectedSheetIndex = 0; } }); this.setState({ flex: flex }); } // Load xlsx file to FlexSheet. load() { let fileInput = document.getElementById("importFile"); if (this.state.flex && fileInput.files[0]) { this.state.flex.loadAsync(fileInput.files[0]); } } // Save FlexSheet to xlsx file. save() { let fileName; if (!!this.state.fileName) { fileName = this.state.fileName; } else { fileName = "FlexSheet.xlsx"; } this.state.flex.saveAsync(fileName); } // build a data map from a string array using the indices as keys _buildDataMap(items) { let map = []; for (let i = 0; i < items.length; i++) { map.push({ key: i, value: items[i] }); } return new wjcGrid.DataMap(map, "key", "value"); } setFileName(name) { this.setState({ fileName: name.target.value }); } render() { return (<div className="container-fluid"> <wjGridSheet.FlexSheet initialized={this.initializeFlexSheet.bind(this)}> <wjGridSheet.Sheet name="Country" itemsSource={this.state.data}></wjGridSheet.Sheet> <wjGridSheet.Sheet name="Unbound" rowCount={20} columnCount={9}></wjGridSheet.Sheet> </wjGridSheet.FlexSheet> <div className="form-inline well well-lg"> <input type="file" className="form-control" id="importFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/> <button className="btn btn-default" onClick={this.load.bind(this)}>Load</button> </div> <div className="form-inline well well-lg"> File Name: <input type="text" className="form-control" onChange={this.setFileName.bind(this)} value={this.state.fileName}/> <button className="btn btn-default" onClick={this.save.bind(this)}>Save</button> </div> </div>); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> .container-fluid .wj-flexsheet { height: 400px; margin: 6px 0; } let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], products = ['Widget', 'Gadget', 'Doohickey']; export function getData(count) { let data = [], i = 0, countryId, productId; for (; i < count; i++) { countryId = Math.floor(Math.random() * countries.length); productId = Math.floor(Math.random() * products.length); data.push({ id: i, countryId: countryId, productId: productId, date: new Date(2014, i % 12, i % 28), amount: Math.random() * 10000, active: i % 4 === 0 }); } return data; } export function getCountries() { return countries; } export function getProducts() { return products; }