Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse xlsx code update #345

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

package org.apache.linkis.storage.excel;

import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -81,41 +79,53 @@ public static List<List<String>> getBasicInfo(InputStream inputStream, File file
}
}

public static Map<String, Map<String, String>> getSheetsInfo(
InputStream inputStream, Boolean hasHeader) {
// use xlsx file
Workbook workbook = null;
public static Map<String, Map<String, String>> getAllSheetInfo(
InputStream inputStream, File file, Boolean hasHeader) throws IOException {
try {
// 压缩膨胀比率,处理excel行或者列过多的情况,不能设置再小了,会导致内存过大
ZipSecureFile.setMinInflateRatio(0.005);
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 使用完最后需要还原
ZipSecureFile.setMinInflateRatio(0.01);
}
Map<String, Map<String, String>> res = new LinkedHashMap<>(workbook.getNumberOfSheets());
// foreach Sheet
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);

Map<String, String> sheetMap = new LinkedHashMap<>();
Workbook wb = null;
if (inputStream != null) {
wb =
StreamingReader.builder()
// number of rows to keep in memory (defaults to 10)
.rowCacheSize(2)
.open(inputStream);
} else {
wb =
StreamingReader.builder()
// number of rows to keep in memory (defaults to 10)
.rowCacheSize(2)
.open(file);
}
Map<String, Map<String, String>> res = new LinkedHashMap<>(wb.getNumberOfSheets());
for (Sheet sheet : wb) {
Map<String, String> item = new HashMap<>();
Iterator<Row> iterator = sheet.iterator();
Row row = null;
while (iterator.hasNext() && row == null) {
row = iterator.next();
}

// get first row as column name
Row headerRow = sheet.getRow(0);
if (row == null) {
res.put(sheet.getSheetName(), new HashMap<>(0));
continue;
}

// foreach column
for (int j = 0; j < headerRow.getPhysicalNumberOfCells(); j++) {
Cell cell = headerRow.getCell(j);
if (hasHeader) {
sheetMap.put(cell.getStringCellValue(), "string");
} else {
sheetMap.put("col_" + (j + 1), "string");
int cellIdx = 0;
for (Cell cell : row) {
if (hasHeader) {
item.put(cell.getStringCellValue(), "string");
} else {
item.put("col_" + (cellIdx + 1), "string");
}
cellIdx++;
}
res.put(sheet.getSheetName(), item);
}
return res;
} finally {
if (inputStream != null) {
inputStream.close();
}
res.put(sheet.getSheetName(), sheetMap);
}
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void getXlsSheetInfo() throws Exception {
@Test
public void getXlsxSheetInfo() throws Exception {
Map<String, Map<String, String>> sheetsInfo =
XlsxUtils.getSheetsInfo(createExcelAndGetInputStream(1), true);
XlsxUtils.getAllSheetInfo(createExcelAndGetInputStream(1), null, true);
Assertions.assertTrue(sheetsInfo.containsKey("Sheet2"));
Assertions.assertEquals("string", sheetsInfo.get("Sheet2").get("Work1"));
}
Expand Down
2 changes: 1 addition & 1 deletion linkis-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion linkis-engineconn-plugins/flink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion linkis-engineconn-plugins/sqoop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
<artifactId>avro</artifactId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,18 +1047,14 @@ public Message getSheetInfo(
}
String suffix = path.substring(path.lastIndexOf("."));
FsPath fsPath = new FsPath(path);
Map<String, Object> res = new HashMap<>();
Map<String, Map<String, String>> sheetInfo;
FileSystem fileSystem = fsService.getFileSystem(userName, fsPath);
try (InputStream in = fileSystem.read(fsPath)) {
if (".xlsx".equalsIgnoreCase(suffix)) {
res.put("type", "xlsx");
sheetInfo = XlsxUtils.getSheetsInfo(in, hasHeader);
sheetInfo = XlsxUtils.getAllSheetInfo(in, null, hasHeader);
} else if (".xls".equalsIgnoreCase(suffix)) {
res.put("type", "xls");
sheetInfo = XlsUtils.getSheetsInfo(in, hasHeader);
} else if (".csv".equalsIgnoreCase(suffix)) {
res.put("type", "csv");
HashMap<String, String> csvMap = new LinkedHashMap<>();
String[][] column = null;
// fix csv file with utf-8 with bom chart[&#xFEFF]
Expand Down Expand Up @@ -1091,11 +1087,10 @@ public Message getSheetInfo(
}
}
sheetInfo = new HashMap<>(1);
sheetInfo.put("Sheet1", csvMap);
sheetInfo.put("sheet_csv", csvMap);
} else {
throw WorkspaceExceptionManager.createException(80004, path);
}
res.put("sheets", sheetInfo);
return Message.ok().data("sheetInfo", sheetInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<artifactId>spring-cloud-starter-gateway</artifactId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
Loading