【工作杂记】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();
相关推荐
小小小米粒2 小时前
Maven Tools
java
骆驼爱记录2 小时前
Word样式检查器使用指南
自动化·word·excel·wps·新人首发
kali-Myon2 小时前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
我是咸鱼不闲呀2 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
清水白石0082 小时前
深入解析 LRU 缓存:从 `@lru_cache` 到手动实现的完整指南
java·python·spring·缓存
符哥20083 小时前
C++ 进阶知识点整理
java·开发语言·jvm
Sayuanni%33 小时前
初阶_多线程1(线程含义与关键属性)
java
程序媛徐师姐3 小时前
Java基于微信小程序的模拟考试系统,附源码+文档说明
java·微信小程序·java模拟考试系统小程序·模拟考试微信小程序·模拟考试系统小程序·模拟考试小程序·java模拟考试小程序
疯狂敲代码的老刘3 小时前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk
夕除3 小时前
js--15
java·jvm·spring