【工作杂记】groupBy排序-操作word

1.stream 流 groupBy 排序

stream流groupBy默认使用的HashMap,无序的,想要实现有序,需要手动指定接收的map类型

例子:

java 复制代码
Map<Date, List<OrderVo>> dataMap = voList.stream()
	.sorted(Comparator.comparing(OrderVo::getVestingTime))
    .collect(Collectors.groupingBy(OrderVo::getVestingTime,
	LinkedHashMap::new,Collectors.toList()));

2.往word里面写数据

依赖:poi

01.新建一个文档

java 复制代码
Document document = new Document(PageSize.A4);

02.给一个地址,系统会在这个地址下新建word文档

java 复制代码
downUrl = "根路径"+ title + ".doc";
 RtfWriter2.getInstance(document, new FileOutputStream(downUrl));

03.打开word进行填充数据

java 复制代码
document.open(); 

04.往document里面写入数据

java 复制代码
段落    
Paragraph paragraph = new Paragraph("段落内容")
// 段落内容2
paragraph.add("段落内容2");
// 上边距
paragraph.setSpacingBefore(5);
// 下边距
paragraph.setSpacingAfter(5);
// 位置,居中,靠左,靠右
paragraph.setAlignment(Element.ALIGN_CENTER);
// 设置字体
paragraph.setFont();
// 写好的段落加入到文档中
document.add(paragraph);

// 字体
Font font1 = FontFactory.getFont("STSong-Light", "Cp1252", 5);
// 字体大小
font1.setSize(10);
// 样式为加粗
font1.setStyle("bold");

// 图片
Image image = Image.getInstance(pictureUrl);
// 设置图片宽高
image.scaleAbsolute(120, 100);
document.add(image);

// 表格
int maxSize = 7; // 一共7列
Table table = new Table(maxSize);
int widths = 100 / maxSize;
// 设置每列宽度比例
int widths1[] = setWordWith(maxSize, widths);
table.setWidths(widths1);
// 占页面宽度比例
table.setWidth(100);
// 居中
table.setAlignment(Element.ALIGN_CENTER);
// 垂直居中
table.setAlignment(Element.ALIGN_MIDDLE);
// 自动填满
table.setAutoFillEmptyCells(true);
// 边框宽度
table.setBorderWidth(1);
table.setPadding(8);

//单元格
Cell cell = new Cell(p);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(Color.WHITE);
// 合并列,单独成一行 这一个格子单独占满一行
cell.setColspan(maxSize);
table.addCell(cell);

05.关闭文档

java 复制代码
document.close();

代码运行后,文档就会保存在配置的downUrl 下

06.返回给前端

java 复制代码
File file = new File(downUrl);
                is = new BufferedInputStream(new FileInputStream(file));
                os = new BufferedOutputStream(response.getOutputStream());
                response.setCharacterEncoding("utf-8");
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
                response.setContentLength((int) file.length());

                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }
                os.flush();
相关推荐
毕设源码-朱学姐5 小时前
【开题答辩全过程】以 工厂能耗分析平台的设计与实现为例,包含答辩的问题和答案
java·vue.js
Spring AI学习6 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
java1234_小锋7 小时前
Spring IoC的实现机制是什么?
java·后端·spring
xqqxqxxq8 小时前
背单词软件技术笔记(V2.0扩展版)
java·笔记·python
消失的旧时光-19438 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
哈哈老师啊8 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
4311媒体网8 小时前
帝国cms调用文章内容 二开基本操作
java·开发语言·php
zwxu_8 小时前
Nginx NIO对比Java NIO
java·nginx·nio
可观测性用观测云9 小时前
Pyroscope Java 接入最佳实践
java
气π10 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi