-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve] extract some common methods (#2691)
Signed-off-by: yuluo-yx <[email protected]>
- Loading branch information
Showing
9 changed files
with
161 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.common.util.export; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.Font; | ||
import org.apache.poi.ss.usermodel.HorizontalAlignment; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
/** | ||
* Excel export utils | ||
*/ | ||
|
||
public final class ExcelExportUtils { | ||
|
||
private ExcelExportUtils() { | ||
} | ||
|
||
/** | ||
* set cell style | ||
* @param workbook workbook entity | ||
*/ | ||
public static CellStyle setCellStyle(Workbook workbook) { | ||
|
||
CellStyle cellStyle = workbook.createCellStyle(); | ||
cellStyle.setAlignment(HorizontalAlignment.CENTER); | ||
return cellStyle; | ||
} | ||
|
||
/** | ||
* @param clazz Export entity class | ||
*/ | ||
public static <T extends Class<?>> Sheet setSheet(String sheetName, Workbook workbook, T clazz) { | ||
|
||
var sheet = workbook.createSheet(sheetName); | ||
sheet.setDefaultColumnWidth(20); | ||
sheet.setColumnWidth(9, 40 * 256); | ||
sheet.setColumnWidth(10, 40 * 256); | ||
|
||
// set header style | ||
CellStyle headerCellStyle = workbook.createCellStyle(); | ||
Font headerFont = workbook.createFont(); | ||
headerFont.setBold(true); | ||
headerCellStyle.setFont(headerFont); | ||
headerCellStyle.setAlignment(HorizontalAlignment.CENTER); | ||
|
||
List<String> headers = new ArrayList<>(); | ||
ReflectionUtils.doWithFields(clazz, field -> { | ||
|
||
field.setAccessible(true); | ||
headers.add(field.getName()); | ||
}); | ||
|
||
// set header | ||
Row headerRow = sheet.createRow(0); | ||
String[] headerArray = headers.toArray(new String[0]); | ||
for (int i = 0; i < headerArray.length; i++) { | ||
Cell cell = headerRow.createCell(i); | ||
cell.setCellValue(headerArray[i]); | ||
cell.setCellStyle(headerCellStyle); | ||
} | ||
|
||
return sheet; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
common/src/main/java/org/apache/hertzbeat/common/util/export/YamlExportUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.common.util.export; | ||
|
||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import org.yaml.snakeyaml.DumperOptions; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
/** | ||
* Yaml Export Utils | ||
*/ | ||
|
||
public final class YamlExportUtils { | ||
|
||
private YamlExportUtils() { | ||
} | ||
|
||
public static <T> void exportWriteOs(List<T> list, OutputStream os) { | ||
|
||
var options = new DumperOptions(); | ||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | ||
options.setIndent(2); | ||
options.setPrettyFlow(true); | ||
Yaml yaml = new Yaml(options); | ||
yaml.dump(list, new OutputStreamWriter(os, StandardCharsets.UTF_8)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters