一句话的需求
需要将List数据库结果集转换为Markdwon格式在邮件内容中体现。
画个边界的圈
仅在阿里maven仓库https://maven.aliyun.com/nexus/content/groups/public
请教过ChatGPT给到过其他两个答案,但受限于仓库环境无法下载依赖,这里也补充一下:
-
其中一个
<dependency> <groupId>io.github.jtables-generator</groupId> <artifactId>tables-generator</artifactId> <version>1.0.0</version> </dependency>//代码实现
import io.github.jtables.generator.MarkdownTable;
import java.util.List;
import java.util.Map;public class MarkdownTableExample {
public static void main(String[] args) {
List<Map<String, Object>> data = List.of(
Map.of("汇总", "A", "商品编码", "1001", "销售数量", "10", "含税二次销售金额", "200", "未税二次销售金额", "180"),
Map.of("汇总", "B", "商品编码", "1002", "销售数量", "20", "含税二次销售金额", "400", "未税二次销售金额", "360")
);// 生成 Markdown 表格 String markdownTable = MarkdownTable.from(data).toMarkdown(); System.out.println(markdownTable); }
}
-
其中另一个
<dependency> <groupId>io.github.drewcarlson</groupId> <artifactId>markdown-tables</artifactId> <version>1.0.0</version> </dependency>//代码实现
import io.github.drewcarlson.markdowntables.TableFormatter;
import io.github.drewcarlson.markdowntables.TableFormatter.Builder;import java.util.List;
import java.util.Map;public class MarkdownTablesExample {
public static void main(String[] args) {
List<Map<String, Object>> data = List.of(
Map.of("汇总", "A", "商品编码", "1001", "销售数量", "10", "含税二次销售金额", "200", "未税二次销售金额", "180"),
Map.of("汇总", "B", "商品编码", "1002", "销售数量", "20", "含税二次销售金额", "400", "未税二次销售金额", "360")
);String markdownTable = generateMarkdownTable(data); System.out.println(markdownTable); } public static String generateMarkdownTable(List<Map<String, Object>> data) { if (data.isEmpty()) { return "No data available."; } // 获取表头 String[] headers = data.get(0).keySet().toArray(new String[0]); // 构建 Markdown 表格 Builder tableBuilder = TableFormatter.builder() .addRow(headers); for (Map<String, Object> row : data) { String[] rowData = new String[headers.length]; for (int i = 0; i < headers.length; i++) { rowData[i] = String.valueOf(row.getOrDefault(headers[i], "")); } tableBuilder.addRow(rowData); } return tableBuilder.build(); }
}
-
道友先别急着往下滑
如果有条件的话,可以测试一下上述两种方式,成功的话帮忙打在评论区上。个人感觉io.github.drewcarlson会靠谱一些,并且支持自动对齐。
上成功的代码
<!-- 引入tech.tablesaw依赖 -->
<dependency>
<groupId>tech.tablesaw</groupId>
<artifactId>tablesaw-core</artifactId>
<version>0.43.1</version>
</dependency>
// 代码实现
List<String> markdownList = new ArrayList<>();
String sqlOne = "加密";
List<Map<String, Object>> oneList = depeOpenFeignService.executeMapSql(sqlOne);
if (CollectionUtil.isNotEmpty(oneList)) {
// 创建Table
Table table = Table.create();
// 创建表头
String[] headers = {"汇总", "商品编码", "销售数量", "含税二次销售金额", "未税二次销售金额"};
for (String header : headers) {
table.addColumns(StringColumn.create(header));
}
// 填充数据
for (Map<String, Object> row : oneList) {
table.column("汇总").appendObj(MapUtils.getString(row, "summary_sale", "").replace("null", ""));
table.column("商品编码").appendObj(MapUtils.getString(row, "product_code_sale", "").replace("null", ""));
table.column("销售数量").appendObj(MapUtils.getString(row, "sale_quantity_sale", "").replace("null", ""));
table.column("含税二次销售金额").appendObj(MapUtils.getString(row, "second_tax_included_sales_amount_sale", "").replace("null", ""));
table.column("未税二次销售金额").appendObj(MapUtils.getString(row, "second_tax_excluded_sales_amount_sale", "").replace("null", ""));
}
// 转换为Markdown
String markdown = table.printAll();
markdownList.add(markdown);
}
return markdownList;
结束语
好了,各位道友拿去用吧,记得有成功依赖io.github.drewcarlson或com.github.drewcarlson的麻烦在评论区留言,我们一起得道升仙!