【工作杂记】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();
相关推荐
想躺平的咸鱼干6 分钟前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
hqxstudying33 分钟前
java依赖注入方法
java·spring·log4j·ioc·依赖
·云扬·42 分钟前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
Bug退退退1232 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠2 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
Zz_waiting.2 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象
全栈凯哥2 小时前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
兮动人2 小时前
获取终端外网IP地址
java·网络·网络协议·tcp/ip·获取终端外网ip地址
呆呆的小鳄鱼2 小时前
cin,cin.get()等异同点[面试题系列]
java·算法·面试
独立开阀者_FwtCoder2 小时前
"页面白屏了?别慌!前端工程师必备的排查技巧和面试攻略"
java·前端·javascript