【工作杂记】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();
相关推荐
全栈凯哥1 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
M1A11 小时前
Java集合框架深度解析:LinkedList vs ArrayList 的对决
java·后端
Top`1 小时前
Java 泛型 (Generics)
java·开发语言·windows
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
如何使用Java WebSocket API实现客户端和服务器端的通信?
java·开发语言·websocket
是小崔啊2 小时前
tomcat源码02 - 理解Tomcat架构设计
java·tomcat
没有bug.的程序员2 小时前
JAVA面试宝典 -《安全攻防:从 SQL 注入到 JWT 鉴权》
java·安全·面试
栈溢出了2 小时前
MyBatis实现分页查询-苍穹外卖笔记
java·笔记·mybatis
morningcat20182 小时前
java17 gc笔记
java·jvm·笔记
3 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
Your易元3 小时前
设计模式-模板方法模式
java·设计模式·模板方法模式