【工作杂记】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();
相关推荐
正远数智9 分钟前
深度解析:SRM系统如何赋能采购库存协同
java·lowcode
青岛少儿编程-王老师12 分钟前
CCF编程能力等级认证GESP—C++1级—20251227
java·c++·算法
hashiqimiya44 分钟前
java程序的并发
java·开发语言·python
.try-1 小时前
cssTab卡片式
java·前端·javascript
ulias2121 小时前
多态理论与实践
java·开发语言·前端·c++·算法
幽络源小助理1 小时前
SpringBoot+Vue攀枝花水果在线销售系统源码 | Java项目免费下载 – 幽络源
java·vue.js·spring boot
inBuilder低代码平台1 小时前
浅谈安卓Webview从初级到高级应用
android·java·webview
豌豆学姐1 小时前
Sora2 短剧视频创作中如何保持人物一致性?角色创建接口教程
android·java·aigc·php·音视频·uniapp
SadSunset1 小时前
关于springAOP的总结
java·后端·spring
计算机学姐2 小时前
基于SpringBoot的演出购票系统【2026最新】
java·vue.js·spring boot·后端·spring·tomcat·intellij-idea