×î½üÎÒÃǹ«Ë¾½Óµ½Ò»¸ö¿Í»§µÄÐèÇó,ÒªÇóΪÕýÔÚ¿ª·¢µÄÏîÄ¿¼Ó¸ö¹¦ÄÜ¡£ÏîÄ¿µÄÇ°¶ËʹÓõÄÊÇReact,¿Í»§ÏëÌí¼Ó¾ß±¸Excel µ¼Èë/µ¼³ö¹¦Äܵĵç×Ó±í¸ñÄ£¿é¡£ ¾¹ý¼¸¸öСʱµÄÔÐ͹¹½¨ºó,¼¼ÊõÍŶÓÈ·ÈÏËùÓпͻ§ÐèÇóÎĵµÖÐÃèÊöµÄ¹¦Äܶ¼ÒѾʵÏÖÁË,²¢ÇÒÔÐÍ¿ÉÒÔÔÚ½ØÖ¹ÈÕÆÚÇ°×öºÃÑÝʾ׼±¸¡£µ«ÊÇ,ÔÚ¸ú²úÆ·×éÔÙ´ÎÌÖÂÛ¿Í»§ÐèÇóʱ,ÎÒÃÇ·¢ÏÖ֮ǰ¶ÔÓйصç×Ó±í¸ñµÄ²¿·ÖÀí½â¿ÉÄÜ´æÔÚÆ«²î¡£ ¿Í»§µÄ¾ßÌåÐèÇóµã½ö½öÌáµ½Ö§³ÖË«»÷Ì¡¢¾ß±¸±ß¿òÉèÖᢱ³¾°É«ÉèÖúÍɾ³ýÐÐÁеȹ¦ÄÜ,µ«Õⲿ·ÖÐèÇóÃèÊö²»ÊǺÜÃ÷È·,¶øÇÒ×îºóÌáµ½¡°ÏñExcelµÄÀàËÆÌåÑ顱,ÎÒÃÇ֮ǰºöÂÔÁËÕâ¾ä»°±³ºóµÄÐÅÏ¢Á¿¡£¾¹ýÓë¿Í»§µÄÒµÎñÐèÇó·½µÄÖ±½Ó¹µÍ¨,¿ÉÒÔÈ·ÈÏÖÕ¶ËÓû§¾ÍÊÇÏëÖ±½ÓÔÚÍøÒ³¶Ë²Ù×÷Excel,²¢ÇÒÖ±½Ó°Ñ±à¼Íê³ÉµÄ±í¸ñÒÔExcelµÄ¸ñʽÏÂÔص½±¾µØ¡£
±¾ÎÄdemoÏÂÔصØÖ·: https://gcdn.grapecity.com.cn/forum.php?mod=attachment&aid=MjMzODA0fDFlMjU0OTU2fDE2NjM4MzYxNjZ8NjI2NzZ8OTk3MTg%3D
ÈçºÎ°ÑÇ°¶Ë±í¸ñÌí¼Óµ½ÄãµÄReactÓ¦ÓÃÖÐ
Äã¿ÉÒÔ¿´µ½ÔÚ StackBlitz ÉÏʵʱÔËÐеľ²Ì¬±í¸ñÓ¦ÓóÌÐò,²¢ÇÒ¿ÉÒÔÔÚ´Ë´¦ÕÒµ½ÑÝʾԴ¡£ Èç¹ûÄãÏëÒªÒѾÌí¼ÓÁË SpreadJS µÄ³ÉÊìÓ¦ÓóÌÐò,ÇëÏÂÔØ´ËʾÀý¡£ Íê³Éºó,´ò¿ªÖÕ¶Ë,µ¼º½µ½¿Ë¡´æ´¢¿âµÄĿ¼,È»ºóÔËÐÐ:
> npm install
ÏÖÔÚÄ㽫¿´µ½¸üкóµÄÓ¦ÓóÌÐòÕýÔÚÔËÐС£
Step 1: ÔÉúHTML±í¸ñ
¸ÃÓ¦ÓóÌÐòµÄÇ°¶Ë»ùÓÚ ReactJS ¹¹½¨,²¢ÓÉʹÓà JSX Óï·¨¡¢JavaScript ºÍ HTML ´úÂë×éºÏ´´½¨µÄ×é¼þ¹¹³É¡£¸ÃÓ¦ÓóÌÐòÊÇʹÓù¦ÄÜ×é¼þµÄÓï·¨´´½¨µÄ¡£ÕâÖÖ·½·¨Ê¹ÎÒÃÇ¿ÉÒÔ±ÜÃâ±àдÀà,Õâ»áʹ×é¼þ¸ü¼Ó¸´ÔÓºÍÄÑÒÔÔĶÁ¡£ ÒDZí°åλÓÚ JSX ×é¼þ²ã´Î½á¹¹µÄ¶¥²¿¡£Ëü³ÊÏÖ HTML ÄÚÈݲ¢Î¬»¤Ó¦ÓóÌÐò״̬,Ô´×Ô¾ßÓÐÐéÄâ JSON ÏúÊÛÊý¾ÝµÄÎļþ¡£ ÿ¸ö×Ó×é¼þ¸ºÔð³ÊÏÖÆäÄÚÈÝ¡£ÓÉÓÚÖ»ÓÐ Dashboard ±£´æÓ¦ÓóÌÐò״̬,Òò´ËËüͨ¹ý props ½«Êý¾ÝÏòÏ´«µÝ¸øÿ¸ö×Ó×é¼þ¡£
Import React, { useState } from ¡®react¡¯;
import { NavBar } from ¡®./NavBar¡¯
import { TotalSales } from ¡®./TotalSales¡¯
import { SalesByCountry } from ¡®./SalesByCountry¡¯
import { SalesByPerson } from ¡®./SalesByPerson¡¯
import { SalesTable } from ¡®./SalesTable¡¯
import { groupBySum } from ¡°../util/util¡±;
import { recentSales } from ¡°../data/data¡±;
export const Dashboard = () => {
const sales = recentSales;
function totalSales() {
const items = sales;
const total = items.reduce(
(acc, sale) => (acc += sale.value),
0
);
return parseInt(total);
};
function chartData() {
const items = sales;
const groups = groupBySum(items, ¡°country¡±, ¡°value¡±);
return groups;
};
function personSales() {
const items = sales;
const groups = groupBySum(items, ¡°soldBy¡±, ¡°value¡±);
return groups;
};
function salesTableData() {
return sales;
};
return (
<div style={{ backgroundColor: ¡®#ddd¡¯ }}>
<NavBar title=¡±Awesome Dashboard¡± />
<div className=¡±container¡±>
<div className=¡±row¡±>
<TotalSales total={totalSales()}/>
<SalesByCountry salesData={chartData()}/>
<SalesByPerson salesData={personSales()}/>
<SalesTable tableData={salesTableData()}/>
</div>
</div>
</div>
);
}
Step 2: Ì滻ΪSpreadJS±í¸ñ ÔÚ±àдÈκδúÂëÐÐ֮ǰ,ÎÒÃDZØÐëÊ×ÏÈ°²×° GrapeCity µÄ Spread.Sheets Wrapper Components for React¡£Ö»ÐèÍ£Ö¹Ó¦ÓóÌÐò,È»ºóÔËÐÐÒÔÏÂÁ½¸öÃüÁî:
> npm install @grapecity/spread-sheets-react
> npm start
ÔÚʹÓà SpreadJS ֮ǰ,Äã±ØÐëÐÞ¸Ä SalesTable.js ÎļþÒÔÉùÃ÷ GrapeCity ×é¼þµÄµ¼Èë¡£ÕâЩµ¼È뽫ÔÊÐí·ÃÎÊ SpreadSheets¡¢Worksheet ºÍ SpreadJS ¿âµÄ Column ¶ÔÏó¡£
Import React from ¡®react¡¯;
import { TablePanel } from ¡°./TablePanel¡±;
// SpreadJS imports
import ¡®@grapecity/spread-sheets-react¡¯;
/* eslint-disable */
import ¡°@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css¡±;
import { SpreadSheets, Worksheet, Column } from ¡®@grapecity/spread-sheets-react¡¯;
´ËÍâ,Èç¹ûûÓÐһЩ»ù±¾ÉèÖÃ,SpreadJS ¹¤×÷±í½«ÎÞ·¨Õý³£¹¤×÷,Òò´ËÈÃÎÒÃÇ´´½¨Ò»¸öÅäÖöÔÏóÀ´±£´æ¹¤×÷±í²ÎÊý¡£
Export const SalesTable = ({ tableData } ) => {
const config = {
sheetName: ¡®Sales Data¡¯,
hostClass: ¡® spreadsheet¡¯,
autoGenerateColumns: false,
width: 200,
visible: true,
resizable: true,
priceFormatter: ¡®$ #.00¡¯,
chartKey: 1
}
Ê×ÏÈ,ÎÒÃDZØÐëÏû³ýÔÚ SalesTable ×é¼þÖгÊÏÖ¾²Ì¬Ãæ°åµÄ JSX ´úÂë:
return (
<TablePanel title=¡±Recent Sales¡±>
<table className=¡±table¡±>
<thead>
<tr>
<th>Client</th>
<th>Description</th>
<th>Value</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
{tableData.map((sale) =>
(<tr key={sale.id}>
<td>{sale.client}</td>
<td>{sale.description}</td>
<td>${sale.value}</td>
<td>{sale.itemCount}</td>
</tr>))}
</tbody>
</table>
</TablePanel>
);
ͨ¹ýÏû³ýÕâ¸ö´úÂë¿é,ÎÒÃÇ×îÖÕÖ»µÃµ½ÁË TablePanel,ÕâÊÇÎÒÃÇÔÚÿ¸ö×é¼þÖÐʹÓõÄͨÓà UI °ü×°Æ÷¡£
Return (
<TablePanel title=¡±Recent Sales¡±>
</TablePanel>
);
´Ëʱ,ÎÒÃÇÏÖÔÚ¿ÉÒÔÔÚ TablePanel ÖвåÈë SpreadJS SpreadSheets ×é¼þ¡£Çë×¢Òâ,SpreadSheets ×é¼þ¿ÉÄÜ°üº¬Ò»¸ö»ò¶à¸ö¹¤×÷±í,¾ÍÏñ Excel ¹¤×÷²¾¿ÉÄÜ°üº¬Ò»¸ö»ò¶à¸ö¹¤×÷±íÒ»Ñù¡£
Return (
<TablePanel key={config.chartKey} title=¡±Recent Sales¡±>
<SpreadSheets hostClass={config.hostClass}>
<Worksheet name={config.sheetName} dataSource={tableData} autoGenerateColumns={config.autoGenerateColumns}>
<Column width={50} dataField=¡¯id¡¯ headerText=¡±ID¡±></Column>
<Column width={200} dataField=¡¯client¡¯ headerText=¡±Client¡±></Column>
<Column width={320} dataField=¡¯description¡¯ headerText=¡±Description¡±></Column>
<Column width={100} dataField=¡¯value¡¯ headerText=¡±Value¡± formatter={config.priceFormatter} resizable=¡±resizable¡±></Column>
<Column width={100} dataField=¡¯itemCount¡¯ headerText=¡±Quantity¡±></Column>
<Column width={100} dataField=¡¯soldBy¡¯ headerText=¡±Sold By¡±></Column>
<Column width={100} dataField=¡¯country¡¯ headerText=¡±Country¡±></Column>
</Worksheet>
</SpreadSheets>
</TablePanel>
);
×÷Ϊ»Áúµã¾¦µÄÒ»±Ê,ÎÒÃǽ«ÒÔÏÂÕâЩÐÐÌí¼Óµ½ App.css ÎļþÖÐÒÔÐÞ¸´µç×Ó±í¸ñµÄ³ß´ç,ÒÔ±ã¸Ã×é¼þÕ¼¾Ýµ×²¿Ãæ°åµÄÕû¸ö¿í¶ÈºÍÏúÊÛÒDZí°åÒ³ÃæµÄÊʵ±¸ß¶È¡£
/*SpreadJS Spreadsheet Styling*/
.container.spreadsheet {
width: 100% !important;
height: 400px !important;
border: 1px solid lightgray !important;
padding-right: 0;
padding-left: 0;
}
¶øÇÒ¡¡ÇÆ!ÕâΪÎÒÃÇÌṩÁËÏÂÃæÁîÈ˾ªÌ¾µÄµç×Ó±í¸ñ:
Çë×¢Òâ,SpreadJS ¹¤×÷±íÈçºÎΪÎÒÃÇÌṩÓë Excel µç×Ó±í¸ñÏàͬµÄÍâ¹Û¡£
ÔÚ Worksheet ×é¼þÖÐ,ÎÒÃÇ¿ÉÒÔ¿´µ½ Column ×é¼þ,Ëü¶¨ÒåÁËÿһÁеÄÌØÕ÷,ÀýÈç¿í¶È¡¢°ó¶¨×ֶκͱêÌâÎı¾¡£ÎÒÃÇ»¹ÔÚÏúÊÛ¼ÛÖµÁÐÖÐÌí¼ÓÁË»õ±Ò¸ñʽ¡£
Óë¾ÉµÄ¾²Ì¬±íÒ»Ñù,Ð嵀 SpreadJS µç×Ó±í¸ñ×é¼þ´ÓÒDZí°å´«µÝµÄµÀ¾ß½ÓÊÕÊý¾Ý¡£ÈçÄãËù¼û,µç×Ó±í¸ñÔÊÐíÄãÖ±½Ó¸ü¸ÄÖµ,¾ÍÏñÔÚ Excel µç×Ó±í¸ñÖÐÒ»Ñù¡£µ«ÊÇ,ÕýÈçÄã¶Ô React Ó¦ÓóÌÐòËùÆÚÍûµÄÄÇÑù,ÕâЩ¸ü¸Ä²»»á×Ô¶¯·´Ó³ÔÚÆäËû×é¼þÖС£ÎªÊ²Ã´ÄØ?
´ÓÒDZí°å½ÓÊÕÊý¾Ýºó,SpreadJS ¹¤×÷±í¿ªÊ¼Ê¹Óø±±¾,¶ø²»ÊÇÒDZí°å×é¼þÖÐÉùÃ÷µÄÏúÊÛÊý¾Ý¡£Ê¼þºÍº¯ÊýÓ¦¸Ã´¦ÀíÈκÎÊý¾ÝÐÞ¸ÄÒÔÏàÓ¦µØ¸üÐÂÓ¦ÓóÌÐòµÄ״̬¡£
¶ÔÓÚÏÂÒ»¸öÈÎÎñ,Äã±ØÐëʹӦÓóÌÐò·´Ó³¶ÔËùÓÐ Dashboard ×é¼þÉ쵀 SpreadJS ¹¤×÷±íËù×öµÄ¸ü¸Ä¡£
Step 3: SpreadJSʵÏÖÏìӦʽÊý¾Ý°ó¶¨ Ä¿Ç°,ÔÚ Dashboard.js ÎļþÖÐÉùÃ÷µÄÏúÊÛ³£Á¿¸ºÔðά»¤Ó¦ÓóÌÐòµÄ״̬¡£
Const sales = recentSales;
ÕýÈçÎÒÃÇËù¿´µ½µÄ,ÕâÖֽṹÒâζמ²Ì¬Êý¾Ý,×èÖ¹ÁËÎÒÃÇÏ£ÍûʵÏֵĶ¯Ì¬¸üС£Òò´Ë,ÎÒÃǽ«ÓóÆΪ¹³×ӵĸ³ÖµÌæ»»ÄÇÐдúÂë¡£ÔÚ React ÖÐ,¹³×Ó¾ßÓмò»¯µÄÓï·¨,¿ÉÒÔͬʱÌṩ״ֵ̬ºÍ´¦Àíº¯ÊýµÄÉùÃ÷¡£
Const[sales, setSales] = new useState(recentSales);
ÉÏÃæµÄ´úÂëÐÐÏÔʾÁË JavaScript Êý×é½â¹¹Óï·¨¡£ useState º¯ÊýÓÃÓÚÉùÃ÷ÏúÊÛ³£Á¿,Ëü±£´æ״̬Êý¾Ý,ÒÔ¼° setSales,ËüÒýÓýöÔÚÒ»ÐÐÖиü¸ÄÏúÊÛÊý×éµÄº¯Êý¡£
µ«ÊÇ,ÎÒÃǵÄÓ¦ÓóÌÐòÖл¹²»´æÔÚÕâ¸ö useState º¯Êý¡£ÎÒÃÇÐèÒª´Ó Dashboard.js ×é¼þÎļþ¿ªÍ·µÄ React °üÖе¼ÈëËü:
import React, { useState } from ¡®react¡¯;
ÏÖÔÚ,ÎÒÃÇ×¼±¸ÔÚ±ØҪʱ¸üРsales Êý×éµÄ״̬¡£
ÎÒÃÇÏ£Íû½«¶Ô¹¤×÷±íËù×öµÄ¸ü¸Ä´«²¥µ½ÒDZí°åµÄÆäÓಿ·Ö¡£Òò´Ë,ÎÒÃDZØÐ붩ÔÄÒ»¸öʼþÀ´¼ì²â¶Ô Worksheet ×é¼þµ¥Ôª¸ñËù×öµÄ¸ü¸Ä,²¢ÔÚ SalesTable.js ÎļþÖÐʵÏÖÏàÓ¦µÄʼþ´¦Àí¡£
ÎÒÃǽ«´Ëʼþ´¦Àí³ÌÐò³ÆΪhandleValueChanged¡£
<SpreadSheets hostClass={config.hostClass} valueChanged={handleValueChanged}>
ÎÒÃÇÈÔÈ»ÐèҪʵÏÖÒ»¸öͬÃûµÄº¯Êý¡£ÔÚÆäÖÐ,ÎÒÃÇ»ñÈ¡¹¤×÷±íµÄÒѸü¸ÄÊý¾ÝÔ´Êý×é,²¢½«¸ÃÊý×é´«µÝ¸øÃûΪ valueChangeCallback µÄº¯Êý¡£
Function handleValueChanged(e, obj) {
valueChangedCallback(obj.sheet.getDataSource());
}
handleValueChanged.bind(this);
È»ºó½« valueChangedCallback º¯Êý´Ó Dashboard ´«µÝµ½ SalesTable ×é¼þ:
<SalesTable tableData={salesTableData()}
valueChangedCallback={handleValueChanged}/>
ÏÖÔÚ,Äã±ØÐ뽫´Ë»Øµ÷º¯Êý×÷Ϊ²ÎÊý´«µÝ¸ø SalesTable ×é¼þ:
export const SalesTable = ({ tableData, valueChangedCallback } ) => {
¶Ô¹¤×÷±íÖе¥Ôª¸ñµÄÈκθü¸Ä¶¼»á´¥·¢»Øµ÷º¯Êý,¸Ãº¯Êý»áÖ´ÐÐ Dashboard ×é¼þÖÐµÄ handleValueChanged º¯Êý¡£ÏÂÃæµÄhandleValueChanged º¯Êý±ØÐëÔÚDashboard ×é¼þÖд´½¨¡£Ëüµ÷Óà setSales º¯Êý,¸Ãº¯Êý¸üÐÂ×é¼þµÄ״̬¡£Òò´Ë,¸ü¸Ä»á´«²¥µ½Ó¦ÓóÌÐòµÄÆäËû×é¼þ¡£
Function handleValueChanged(tableData) {
setSales(tableData.slice(0));
}
Äã¿ÉÒÔͨ¹ý±à¼Ò»Ð©ÏúÊÛ¶îÖµ²¢²é¿´ÒDZí°å¶¥²¿µÄÏúÊÛ¶î±ä»¯À´³¢ÊԴ˲Ù×÷:
¿´ÆðÀ´±È¶ûµÄÏúÊÛÒµ¼¨²»´í!
Step 4: ʵÏÖµ¼Èëµ¼³öExcel µ½Ä¿Ç°ÎªÖ¹,ÎÒÃÇÒѾÁ˽âÁËÈçºÎÓà SpreadJS µç×Ó±í¸ñÌæ»»¾²Ì¬ÏúÊÛ±í¡£ÎÒÃÇ»¹Ñ§Ï°ÁËÈçºÎͨ¹ý React µÄ¹³×Ӻͻص÷ÔÚÓ¦ÓóÌÐò×é¼þÉÏ´«²¥Êý¾Ý¸üС£ÎÒÃÇÉè·¨ÓúÜÉٵĴúÂëÌṩÁËÕâЩ¹¦ÄÜ¡£ÄãµÄÓ¦ÓóÌÐò¿´ÆðÀ´ÒѾºÜ°ôÁË,²¢ÇÒÄãÏàÐÅËü½«¸øÄãδÀ´µÄ¿Í»§ÁôÏÂÉî¿ÌÓ¡Ïó¡£µ«ÔÚ´Ë֮ǰ,ÈÃÎÒÃǽõÉÏÌí»¨¡£
ÄãÒѾ֪µÀÄãµÄÆóÒµÓû§ÔÚÈÕ³£Éú»îÖо³£Ê¹Óà Excel¡£ÏàͬµÄÓû§½«¿ªÊ¼ÔÚ React ºÍ SpreadJS Ö®ÉÏʹÓÃÄãµÄÈ«ÐÂÓ¦ÓóÌÐò¡£µ«ÔÚijЩʱºò,ËûÃÇ»á´í¹ý Excel ºÍÄã³öÉ«µÄÒDZí°åÖ®¼äµÄ¼¯³É¡£
Èç¹ûÄãÖ»Äܽ«µç×Ó±í¸ñÊý¾Ýµ¼³öµ½ Excel ²¢½«Êý¾Ý´Ó Excel µ¼Èëµ½ SpreadJS,Ôò¸ÃÓ¦ÓóÌÐò½«¸ü¼ÓÇ¿´ó¡£ÄãÈçºÎʵÏÖÕâЩ¹¦ÄÜ?
ÈÃÎÒÃÇÔÙ´ÎÍ£Ö¹Ó¦ÓóÌÐò²¢°²×° GrapeCity µÄ Spread.Sheets ¿Í»§¶Ë Excel IO °üÒÔ¼°Îļþ±£»¤³ÌÐò°ü:
> npm install @grapecity/spread-excelio
> npm install file-saver
> npm start
Òª½«Êý¾Ý´ÓÎÒÃǵÄÓ¦ÓóÌÐòµ¼³öµ½ Excel Îļþ(À©Õ¹ÃûΪ .xlsx),ÎÒÃDZØÐëÐÞ¸Ä SalesTable ×é¼þ,ÉùÃ÷ Excel IO ºÍÎļþ±£»¤³ÌÐò×é¼þµÄµ¼Èë¡£
Import { IO } from ¡°@grapecity/spread-excelio¡±;
import { saveAs } from ¡®file-saver¡¯;
½ÓÏÂÀ´,ÎÒÃǽ«¸ü¸Ä SalesTable.js ÎļþµÄ JSX ´úÂë,ÒÔÌí¼ÓÒ»¸ö°´Å¥ÒÔ½« SpreadJS ¹¤×÷±íÊý¾Ýµ¼³öµ½±¾µØÎļþ¡£µ¥»÷¸Ã°´Å¥½«´¥·¢Ò»¸öÃûΪ exportSheet µÄʼþ´¦Àí³ÌÐò¡£
{/* EXPORT TO EXCEL */}
<div className=¡±dashboardRow¡±>
<button className=¡±btn btn-primary dashboardButton¡±
onClick={exportSheet}>Export to Excel</button>
</div>
</TablePanel>
·´¹ýÀ´,exportSheet º¯Êý»á½«¹¤×÷±íÖеÄÊý¾Ý±£´æµ½ÃûΪ SalesData.xslx µÄÎļþÖС£¸Ãº¯ÊýÊ×ÏȽ« Spread ¶ÔÏóÖеÄÊý¾ÝÐòÁл¯Îª JSON ¸ñʽ,È»ºóͨ¹ý Excel IO ¶ÔÏó½«Æäת»»Îª Excel ¸ñʽ¡£
Function exportSheet() {
const spread = _spread;
const ilename = ¡°SalesData.xlsx¡±;
const sheet = spread.getSheet(0);
const excelIO = new IO();
const json = JSON.stringify(spread.toJSON({
includeBindingSource: true,
columnHeadersAsFrozenRows: true,
}));
excelIO.save(json, (blob) => {
saveAs(blob, ilename);
}, function € {
al€(
});
}
Çë×¢ÒâÉÏÊöº¯ÊýÈçºÎÐèÒªÒ»¸öÕ¹¿ª¶ÔÏó,¸Ã¶ÔÏó±ØÐëÓëÎÒÃÇÔÚ SalesTable ×é¼þÖÐʹÓÃµÄ SpreadJS ¹¤×÷±íµÄʵÀýÏàͬ¡£Ò»µ©¶¨ÒåÁË SpreadSheet ¶ÔÏó,ÉÏÃæÇåµ¥ÖÐµÄ getSheet(0) µ÷Óþͻá¼ìË÷µç×Ó±í¸ñÊý×éÖеĵÚÒ»¸ö¹¤×÷±í:
const sheet = spread.getSheet(0);
µ«ÊÇÎÒÃÇÈçºÎÒÔ±à³Ì·½Ê½»ñÈ¡µç×Ó±í¸ñµÄʵÀýÄØ?
Ò»µ©µç×Ó±í¸ñ¶ÔÏó±»³õʼ»¯,SpreadJS ¿â¾Í»á´¥·¢Ò»¸öÃûΪ workbookInitialized µÄʼþ¡£ÎÒÃDZØÐë´¦ÀíËü²¢½«ÊµÀý´æ´¢Îª SalesTable ×é¼þµÄ״̬¡£ÈÃÎÒÃÇÊ×ÏÈʹÓà useState ¹³×ÓΪµç×Ó±í¸ñʵÀýÉùÃ÷Ò»¸ö״̬³£Á¿:
const [_spread, setSpread] = useState({});
ÎÒÃÇÐèÒª½« useState º¯Êýµ¼Èëµ½ SalesTable.js ×é¼þÎļþ¿ªÍ·µÄ React ÉùÃ÷ÖÐ:
import React, { useState }¡®from ¡¯react';
ÏÖÔÚÎÒÃÇ¿ÉÒÔÉùÃ÷Ò»¸öº¯ÊýÀ´´¦Àí workbookInit ʼþ¡¡
function workbookInit(sprea
setSpread(spread)
}
¡È»ºó½« workbookInit ʼþ°ó¶¨µ½ÎÒÃǸոմ´½¨µÄº¯Êý:
<SpreadSheets hostClass={config.hostClass} workbookInitialized={workbookInit} valueChanged={handleValueChanged}>
ÏÖÔÚ,¡°µ¼³öµ½ Excel¡±°´Å¥½«ÈçÏÂËùʾ:
ÏÖÔÚÎÒÃÇÀ´ÑÝʾÈçºÎʵÏÖ Excel Êý¾Ýµ¼Èë¡£Õâ¸ö¹ý³ÌÊǵ¼³öµÄÄæ¹ý³Ì,ËùÒÔÈÃÎÒÃÇ´Ó XLSX Îļþ¿ªÊ¼¡£
´Ë¹¦ÄܵķÃÎʵãÊÇÁíÒ»¸ö°´Å¥,ÎÒÃÇÐèÒª½«ÆäÌí¼Óµ½ SalesTable ×é¼þµÄ JSX ´úÂëµÄĩβ¡£Çë×¢Òâ,ÕâÀïÎÒÃÇʹÓò»Í¬µÄ°´Å¥ÀàÐÍ:¡°Îļþ¡±ÀàÐ͵ÄÊäÈëÔªËØ,Ëü²úÉúÒ»¸öÑ¡ÔñÎļþµÄ°´Å¥¡£µ±Îļþ±»Ñ¡ÖÐʱ,onChange ʼþ´¥·¢ fileChangeevent ´¦Àí³ÌÐò:
<div clas¡±Name="dashbo¡±rd>
{/* EXPORT TO EXCE}
<button clas¡±Name="btn btn-primary dashboard¡±utton"
onClick={exportSheet}>Export to Excel</bu>
{/* IMPORT FROM EXCE}
<div>
<b>Import Excel File:</b>
<div>
<input¡±type¡±"file" clas¡±Name="file¡±elect"
onCh€e={(e) => f€Change(e)} />
</div>
</div>
</div>
·´¹ýÀ´,fileChange º¯Êý½«Ê¹Óà Excel IO ¶ÔÏó½«Îļþµ¼È빤×÷±í¶ÔÏó¡£ÔÚº¯Êý½áÊøʱ,»á´¥·¢Ò»¸ö fileImportedCallback ʼþ,½«Êý¾Ý´øµ½ Dashboard ×é¼þÖÐ:
functio€hange(e) {
if (_spread) {
const fileDom = e.target || e.srcElement;
const excelIO = new IO();
const spread = _spread;
const deserializationOptions = {
frozenRowsAsColumnHeaders: true
};
excelIO.open(fileDom.files[0], (data) => {
const newSalesData = extractSheetData(data);
fileImportedCallback(newSalesData });
}
}
µ«ÊÇÕâ¸ö»Øµ÷ÐèÒªÉùÃ÷Ϊ SalesTable ×é¼þµÄ²ÎÊý:
export const SalesTable = ({ tableData, valueChangedCallback,
fileImportedCallback } ) => {
´ËÍâ,ÎÒÃDZØÐëͨ¹ý´Ó util.js ÎļþÖе¼ÈëÀ´Îª SalesTable ×é¼þÌṩ extractSheetData º¯Êý:
import { extractSh¡°etData } from "¡±./util/util.js";
ÎÒÃÇÐèҪΪ Dashboard ×é¼þÉϵı£´æÎļþʵÏÖʼþ´¦Àí³ÌÐò¡£Õâ¸öº¯ÊýΨһҪ×öµÄ¾ÍÊÇʹÓÃÀ´×Ô SpreadJS ¹¤×÷±íµÄÊý¾Ý¸üÐÂÒDZí°åµÄ״̬¡£
function handleFileImportewSales) {
setSales(newSales.slice(0));
}
<SalesTable tableData={saleleData()}
valueChangedCallback={handleValueChanged}
fileImportedCallback={handleFileImported}/>
Ö»Ð輸¸ö¼òµ¥µÄ²½Öè,ÎÒÃǾͿÉÒÔ½«´øÓо²Ì¬Êý¾ÝµÄÎÞÁÄÓ¦ÓóÌÐò±ä³ÉÒÔ¾ßÓÐ Excel µ¼ÈëºÍµ¼³ö¹¦Äܵĵç×Ó±í¸ñΪÖÐÐĵÄÏìӦʽӦÓóÌÐò¡£×îºó,Äã²é¿´¿Í»§µÄÇëÇó²¢ÑéÖ¤ÄãµÄÓ¦ÓóÌÐòÊÇ·ñÂú×ãËùÓÐÒªÇó!
ÎÒÃÇ¿ÉÒÔÀ©Õ¹ÕâЩÏë·¨²¢ÎªÎÒÃǵÄÓ¦ÓóÌÐò̽Ë÷ÆäËûÁîÈËÐ˷ܵŦÄÜ¡£ÀýÈç,ÎÒÃÇ¿ÉÒÔ×Ô¶¯¡¢¾²Ä¬µØ±£´æ¹¤×÷±íÊý¾Ý,´Ó¶øÔÚÐèҪʱ±£Áô¸ü¸ÄÈÕÖ¾ºÍ»Ø¹ö´íÎóµ½±íÖС£
´ËÍâ,Äã¿ÉÒÔ½«±í¸ñÊý¾ÝÓëÔ¶³ÌÊý¾Ý¿âͬ²½¡£»òÕßÄã¿ÉÒÔʵÏÖÒ»¸ö±£´æ°´Å¥,ͨ¹ý Web ·þÎñ·½·¨½«±íÊý¾Ý¸´ÖƵ½Íⲿϵͳ¡£
¸ü¶à´¿Ç°¶Ë±í¸ñÔÚÏßdemoʾÀý :https://demo.grapecity.com.cn/spreadjs/gc-sjs-samples/index.html ´¿Ç°¶Ë±í¸ñÓ¦Óó¡¾°:https://www.grapecity.com.cn/developer/spreadjs#scenarios Òƶ¯¶ËʾÀý(¿ÉɨÂëÌåÑé):http://demo.grapecity.com.cn/spreadjs/mobilesample/
|