Skip to content

Commit

Permalink
增加Excel动态sheet和动态列演示。增加读取Excel所有sheet数据的快捷方法
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 18, 2024
1 parent 0412f2f commit 5d896e9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@
/nop/report/demo/base/09-套打.xpt.xlsx
/nop/report/demo/base/10-导出Excel公式.xpt.xlsx
/nop/report/demo/base/11-打印条码和二维码.xpt.xlsx
/nop/report/demo/base/12-动态Sheet和动态列.xlsx
/nop/report/demo/ext/report-with-params.xpt.xlsx
/nop/report/demo/pages/demo.page.yaml
/nop/report/demo/pages/report-with-params.page.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.nop.commons.util.StringHelper;
import io.nop.core.resource.IResource;
import io.nop.core.resource.impl.FileResource;
import io.nop.core.resource.impl.UnknownResource;
import io.nop.core.resource.record.csv.CsvResourceRecordIO;
import io.nop.core.resource.record.list.HeaderListRecordOutput;
import io.nop.excel.model.ExcelWorkbook;
Expand All @@ -22,6 +23,7 @@
import io.nop.xlang.api.XLang;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -44,7 +46,25 @@ public static Object loadXlsxObject(String impModelPath, IResource resource) {
return new XlsxObjectLoader(impModelPath).parseFromResource(resource);
}

public static List<Map<String, Object>> readXlsx(IResource xlsx, String selectedSheetName) {
public static List<ExcelSheetData> readAllSheets(IResource xlsx) {
List<ExcelSheetData> ret = new ArrayList<>();

new XlsxToRecordOutput((r, e) -> new HeaderListRecordOutput<>(xlsx, null, CollectionHelper::toMap) {
@Override
public void close() {
ExcelSheetData data = new ExcelSheetData();
data.setName(r.getName());
data.setData(this.getResult());
ret.add(data);
}
},
null, sheetName -> {
return new UnknownResource("/" + sheetName);
}).parseFromResource(xlsx);
return ret;
}

public static List<Map<String, Object>> readSheet(IResource xlsx, String selectedSheetName) {
HeaderListRecordOutput<Map<String, Object>> output = new HeaderListRecordOutput<>(xlsx, null, CollectionHelper::toMap);

MutableInt index = new MutableInt();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.nop.ooxml.xlsx.util;

import io.nop.api.core.annotations.data.DataBean;

import java.util.List;
import java.util.Map;

@DataBean
public class ExcelSheetData {
private String name;
private List<Map<String, Object>> data;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Map<String, Object>> getData() {
return data;
}

public void setData(List<Map<String, Object>> data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import io.nop.api.core.time.CoreMetrics;
import io.nop.core.initialize.CoreInitialization;
import io.nop.core.lang.json.JsonTool;
import io.nop.core.resource.IResource;
import io.nop.core.unittest.BaseTestCase;
import io.nop.excel.model.ExcelCell;
import io.nop.excel.model.ExcelSheet;
import io.nop.excel.model.ExcelTable;
import io.nop.excel.model.ExcelWorkbook;
import io.nop.ooxml.xlsx.util.ExcelHelper;
import io.nop.ooxml.xlsx.util.ExcelSheetData;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestExcelHelper extends BaseTestCase {
@BeforeAll
Expand Down Expand Up @@ -61,9 +66,19 @@ public void testXlsxToCsv() {
System.out.println("xlsxToCsv:" + (endTime - beginTime) + "ms");

beginTime = CoreMetrics.currentTimeMillis();
ExcelHelper.readXlsx(xlsx, null);
ExcelHelper.readSheet(xlsx, null);
endTime = CoreMetrics.currentTimeMillis();
// 1M => 16312ms
System.out.println("readXlsx:" + (endTime - beginTime) + "ms");
}

@Test
public void readAllSheets() {
IResource xlsx = attachmentResource("test-data.xlsx");
List<ExcelSheetData> sheets = ExcelHelper.readAllSheets(xlsx);
System.out.println(JsonTool.serialize(sheets, true));
assertEquals(2, sheets.size());
assertEquals("Sheet2", sheets.get(1).getName());
assertEquals("[{c=5, d=6}, {c=7, d=8}]", sheets.get(1).getData().toString());
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 5d896e9

Please sign in to comment.