0


前端univer创建、编辑excel

前端univer创建、编辑excel

源码在线demo:https://codesandbox.io/p/sandbox/univer-q87kqg?file=/src/Demo.jsx

univer官网地址:https://univer.ai/zh-CN/guides/sheet/introduction

安装univer

  1. npm install @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/sheets @univerjs/sheets-formula @univerjs/sheets-ui @univerjs/ui @univerjs/facade

安装xlsx处理数据

  1. npm install xlsx

创建实例

  1. 引入 Univer 的样式文件、语言包,插件
  1. import"@univerjs/design/lib/index.css";import"@univerjs/ui/lib/index.css";import"@univerjs/docs-ui/lib/index.css";import"@univerjs/sheets-ui/lib/index.css";import"@univerjs/sheets-formula/lib/index.css";import{ LocaleType, Tools, Univer }from"@univerjs/core";import{ defaultTheme }from"@univerjs/design";import{ UniverFormulaEnginePlugin }from"@univerjs/engine-formula";import{ UniverRenderEnginePlugin }from"@univerjs/engine-render";import{ UniverUIPlugin }from"@univerjs/ui";import{ UniverDocsPlugin }from"@univerjs/docs";import{ UniverDocsUIPlugin }from"@univerjs/docs-ui";import{ UniverSheetsPlugin }from"@univerjs/sheets";import{ UniverSheetsFormulaPlugin }from"@univerjs/sheets-formula";import{ UniverSheetsUIPlugin }from"@univerjs/sheets-ui";import DesignZhCN from"@univerjs/design/locale/zh-CN";import UIZhCN from"@univerjs/ui//locale/zh-CN";import DocsUIZhCN from"@univerjs/docs-ui/locale/zh-CN";import SheetsZhCN from"@univerjs/sheets/locale/zh-CN";import SheetsUIZhCN from"@univerjs/sheets-ui/locale/zh-CN";import{ UniverInstanceType }from"@univerjs/core";import{ FUniver }from"@univerjs/facade";
  1. 创建sheet实例,挂载到dom元素上
  1. const workbookConfig ={id:'workbook',locale:"zhCN",}const univerAPI =useRef();const univer =useRef();useEffect(()=>{createSheet()return()=>{
  2. univerAPI.current.disposeUnit(workbookConfig.id);};},[]);constcreateSheet=(config = workbookConfig)=>{if(univerAPI.current){
  3. univerAPI.current.disposeUnit(workbookConfig.id);}
  4. univer.current =newUniver({theme: defaultTheme,locale: LocaleType.ZH_CN,locales:{[LocaleType.ZH_CN]: Tools.deepMerge(
  5. SheetsZhCN,
  6. DocsUIZhCN,
  7. SheetsUIZhCN,
  8. UIZhCN,
  9. DesignZhCN
  10. ),},});
  11. univer.current.registerPlugin(UniverRenderEnginePlugin);
  12. univer.current.registerPlugin(UniverFormulaEnginePlugin);
  13. univer.current.registerPlugin(UniverUIPlugin,{container:"univer-sheet-container-id",});
  14. univer.current.registerPlugin(UniverDocsPlugin);
  15. univer.current.registerPlugin(UniverDocsUIPlugin);
  16. univer.current.registerPlugin(UniverSheetsPlugin);
  17. univer.current.registerPlugin(UniverSheetsUIPlugin);
  18. univer.current.registerPlugin(UniverSheetsFormulaPlugin);
  19. univer.current.createUnit(UniverInstanceType.UNIVER_SHEET, config);
  20. univerAPI.current = FUniver.newAPI(univer.current);}// 挂在实例<div id="univer-sheet-container-id" className="univer-sheet-container" style={{height:"500px"}}/>

导入excel文件、通过xlsx对文件进行处理,通过@univerjs/facade导入数据

  1. /** 上传文件 */consthandleChangeUploadExcel=async(fileData)=>{const reader =newFileReader();
  2. reader.onload=(e)=>{const data = e.target.result;const workbook =XLSX.read(data,{type:"binary"});const excelData =convertWorkbookToJson(workbook);
  3. console.log("%c [ excelData ]-87","font-size:13px; background:pink; color:#bf2c9f;",
  4. excelData
  5. );createSheet(excelData);};
  6. reader.readAsBinaryString(fileData.file);};/** 将sheet数据转换为json */constconvertWorkbookToJson=(workbook)=>{const sheets ={};const sheetOrder =[];
  7. workbook.SheetNames.forEach((sheetName, sheetIndex)=>{const worksheet = workbook.Sheets[sheetName];const jsonSheet =XLSX.utils.sheet_to_json(worksheet,{header:1});const cellData ={};let maxColumnCount =0;
  8. jsonSheet.forEach((row, rowIndex)=>{
  9. row.forEach((cell, colIndex)=>{if(cell !==null&& cell !==undefined&& cell !==""){if(!cellData[rowIndex]){
  10. cellData[rowIndex]=[];}
  11. cellData[rowIndex][colIndex]={v: cell };if(colIndex +1> maxColumnCount){
  12. maxColumnCount = colIndex +1;}}});});const sheetId =`sheet_${sheetIndex}`;
  13. sheets[sheetId]={id: sheetId,name: sheetName,rowCount: jsonSheet.length +50,columnCount: maxColumnCount +50,zoomRatio:1,cellData: cellData,showGridlines:1,};
  14. sheetOrder.push(sheetId);});return{...workbookConfig,sheetOrder: sheetOrder,sheets: sheets,};};<Upload
  15. accept=".xls,.xlsx"
  16. onChange={handleChangeUploadExcel}
  17. beforeUpload={()=>false}
  18. showUploadList={false}
  19. multiple={false}><Button type="primary" className="upload-btn">
  20. 选择excel文件
  21. </Button></Upload>

获取表格数据

  1. univerAPI.current.getActiveWorkbook().save()

效果:

初始化
在这里插入图片描述
导入数据

在这里插入图片描述
获取表格数据
在这里插入图片描述

完整代码:

  1. import React,{ useEffect, useRef, useState }from"react";import"antd/dist/antd.css";import"./index.css";import{ Button, Upload }from"antd";import"@univerjs/design/lib/index.css";import"@univerjs/ui/lib/index.css";import"@univerjs/docs-ui/lib/index.css";import"@univerjs/sheets-ui/lib/index.css";import"@univerjs/sheets-formula/lib/index.css";import{ LocaleType, Tools, Univer }from"@univerjs/core";import{ defaultTheme }from"@univerjs/design";import{ UniverFormulaEnginePlugin }from"@univerjs/engine-formula";import{ UniverRenderEnginePlugin }from"@univerjs/engine-render";import{ UniverUIPlugin }from"@univerjs/ui";import{ UniverDocsPlugin }from"@univerjs/docs";import{ UniverDocsUIPlugin }from"@univerjs/docs-ui";import{ UniverSheetsPlugin }from"@univerjs/sheets";import{ UniverSheetsFormulaPlugin }from"@univerjs/sheets-formula";import{ UniverSheetsUIPlugin }from"@univerjs/sheets-ui";import DesignZhCN from"@univerjs/design/locale/zh-CN";import UIZhCN from"@univerjs/ui//locale/zh-CN";import DocsUIZhCN from"@univerjs/docs-ui/locale/zh-CN";import SheetsZhCN from"@univerjs/sheets/locale/zh-CN";import SheetsUIZhCN from"@univerjs/sheets-ui/locale/zh-CN";import{ UniverInstanceType }from"@univerjs/core";import{ FUniver }from"@univerjs/facade";import*asXLSXfrom"xlsx";const workbookConfig ={id:"workbook",locale:"zhCN",};constApp=()=>{const univerAPI =useRef();const univer =useRef();const[excelData, setExcelData]=useState("");useEffect(()=>{
  2. univer.current =newUniver({theme: defaultTheme,locale: LocaleType.ZH_CN,locales:{[LocaleType.ZH_CN]: Tools.deepMerge(
  3. SheetsZhCN,
  4. DocsUIZhCN,
  5. SheetsUIZhCN,
  6. UIZhCN,
  7. DesignZhCN
  8. ),},});
  9. univer.current.registerPlugin(UniverRenderEnginePlugin);
  10. univer.current.registerPlugin(UniverFormulaEnginePlugin);
  11. univer.current.registerPlugin(UniverUIPlugin,{container:"univer-sheet-container-id",});
  12. univer.current.registerPlugin(UniverDocsPlugin);
  13. univer.current.registerPlugin(UniverDocsUIPlugin);
  14. univer.current.registerPlugin(UniverSheetsPlugin);
  15. univer.current.registerPlugin(UniverSheetsUIPlugin);
  16. univer.current.registerPlugin(UniverSheetsFormulaPlugin);
  17. univer.current.createUnit(UniverInstanceType.UNIVER_SHEET,{});
  18. univerAPI.current = FUniver.newAPI(univer.current);return()=>{
  19. univerAPI.current.disposeUnit(workbookConfig.id);};},[]);/** 获取表格数据 */consthandleGetSheetData=()=>{const data = univerAPI.current.getActiveWorkbook().save();
  20. console.log("%c [ data ]-68","font-size:13px; background:pink; color:#bf2c9f;",
  21. data
  22. );setExcelData(JSON.stringify(data));};/** 上传文件 */consthandleChangeUploadExcel=async(fileData)=>{const reader =newFileReader();
  23. reader.onload=(e)=>{const data = e.target.result;const workbook =XLSX.read(data,{type:"binary"});const excelData =convertWorkbookToJson(workbook);
  24. console.log("%c [ excelData ]-87","font-size:13px; background:pink; color:#bf2c9f;",
  25. excelData
  26. );
  27. univerAPI.current.disposeUnit(workbookConfig.id);
  28. univer.current.createUnit(UniverInstanceType.UNIVER_SHEET, excelData);};
  29. reader.readAsBinaryString(fileData.file);};/** 将sheet数据转换为json */constconvertWorkbookToJson=(workbook)=>{const sheets ={};const sheetOrder =[];
  30. workbook.SheetNames.forEach((sheetName, sheetIndex)=>{const worksheet = workbook.Sheets[sheetName];const jsonSheet =XLSX.utils.sheet_to_json(worksheet,{header:1});const cellData ={};let maxColumnCount =0;
  31. jsonSheet.forEach((row, rowIndex)=>{
  32. row.forEach((cell, colIndex)=>{if(cell !==null&& cell !==undefined&& cell !==""){if(!cellData[rowIndex]){
  33. cellData[rowIndex]=[];}
  34. cellData[rowIndex][colIndex]={v: cell };if(colIndex +1> maxColumnCount){
  35. maxColumnCount = colIndex +1;}}});});const sheetId =`sheet_${sheetIndex}`;
  36. sheets[sheetId]={id: sheetId,name: sheetName,rowCount: jsonSheet.length +50,columnCount: maxColumnCount +50,zoomRatio:1,cellData: cellData,showGridlines:1,};
  37. sheetOrder.push(sheetId);});return{...workbookConfig,sheetOrder: sheetOrder,sheets: sheets,};};return(<div className="main-container"><div><Upload
  38. accept=".xls,.xlsx"
  39. onChange={handleChangeUploadExcel}
  40. beforeUpload={()=>false}
  41. showUploadList={false}
  42. multiple={false}><Button type="primary" className="upload-btn">
  43. 选择excel文件
  44. </Button></Upload></div><div
  45. id="univer-sheet-container-id"
  46. className="univer-sheet-container"
  47. style={{height:"500px"}}/><div><Button type="primary" onClick={handleGetSheetData}>
  48. 获取表格数据
  49. </Button></div>
  50. 表格数据:<div>{excelData}</div></div>);};exportdefault App;

源码在线demo:https://codesandbox.io/p/sandbox/univer-q87kqg?file=/src/Demo.jsx

标签: 前端 excel univer

本文转载自: https://blog.csdn.net/weixin_43760969/article/details/142386672
版权归原作者 、昔年 所有, 如有侵权,请联系我们删除。

“前端univer创建、编辑excel”的评论:

还没有评论