感谢您的回答,
,具体代码如下,先抽取5万条数据,每一行是58列,然后创建5个sheet,每个sheet的数据一样
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("aa"));
- spread.fromJSON(json);
- designer.setWorkbook(spread);
- var getResult = async function() {
- var data = await call_sql_api("mysqldb", "");// 返回50000条数据,每行58列
- console.log("data=========>", data);
-
- var showData = [];
- showData.push(data.columns);
- var newData = data.rows.slice(0, 20000); // 截取20000条
- console.log("newData=========>", newData);
- newData.forEach(function(row) {
- showData.push(row);
- });
-
- console.log("showData=========>", showData);
-
- // Function to add data to sheet in chunks
- const addDataInChunks = (sheet, data, chunkSize) => {
- return new Promise(resolve => {
- let index = 0;
- const addChunk = () => {
- let end = Math.min(index + chunkSize, data.length);
- for (let i = index; i < end; i++) {
- data[i].forEach((item, colIndex) => {
- sheet.setValue(i + 1, colIndex, item); // +1 to account for header row
- });
- }
- index = end;
- if (index < data.length) {
- setTimeout(addChunk, 0); // Yield to the browser
- } else {
- resolve();
- }
- };
- addChunk();
- });
- };
- spread.suspendPaint();
- spread.suspendCalcService();
- // Loop to create 5 new sheets and fill with data
- for (let i = 0; i < 5; i++) {
- let sheetName = `Sheet${i + 2}`;
- let newSheet = new GC.Spread.Sheets.Worksheet(sheetName);
- // Setting the number of rows and columns
- newSheet.setRowCount(showData.length);
- newSheet.setColumnCount(showData[0].length);
- // Adding the new worksheet to the workbook
- spread.addSheet(spread.getSheetCount(), newSheet);
- // Write headers to the first row
- showData[0].forEach((item, index) => {
- newSheet.setValue(0, index, item);
- });
- // Write data in chunks to avoid freezing the browser
- await addDataInChunks(newSheet, showData.slice(1), 1000);
- }
- // Refresh the workbook to make sure the changes take effect
- spread.repaint();
- spread.resumeCalcService(false);
- spread.resumePaint();
- };
- setButton("Sheet1", "H5", "count");
- setButtonClickEvent("Sheet1", "H5", getResult);
复制代码 |