poi坑点(springboot)
工作上需要写了一个将数据库数据生成excel表的接口,在此过程中遇到了一些坑点,现在此纪录
PS:一部分可能是因为我没用明白
1. 样式问题
自动调整尽量不要使用,部分列留白过多,空列列宽过窄,可能是只自动调整了一列的缘故。
代码:
for (int index = 0; index < 14; index++){
sheet.autoSizeColumn(index);
}
效果图:
建议使用setCellStyle(),而不是setRowStyle()。直接使用setRowStyle()会导致只有没存入
数据的单元格设置样式成功。
代码:
Row dataRow = sheet.createRow(rowIndex);
dataRow.setRowStyle(dataStyle);
效果图:



- 最终成品
样式代码:
// 标题格式
Font titleFont = xssfWorkbook.createFont();
titleFont.setFontName("黑体");
titleFont.setFontHeightInPoints((short) 12);
titleFont.setBold(true);
XSSFCellStyle titleStyle = xssfWorkbook.createCellStyle();
titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
titleStyle.setFont(titleFont);
titleStyle.setBorderTop(BorderStyle.THIN);//边框线
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderBottom(BorderStyle.THIN);
// 数据格式
Font dataFont = xssfWorkbook.createFont();
dataFont.setFontName("宋体");
dataFont.setFontHeightInPoints((short) 11);
dataFont.setBold(false);
XSSFCellStyle dataStyle = xssfWorkbook.createCellStyle();
dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
dataStyle.setFont(dataFont);
dataStyle.setBorderTop(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setBorderBottom(BorderStyle.THIN);
样式设置代码:
for (StandardColumnEntity columnDetail : columnDetailList){
Row dataRow = sheet.createRow(rowIndex);
// 业务分类
Cell categoryCell = dataRow.createCell(0);
categoryCell.setCellStyle(dataStyle);
categoryCell.setCellValue(columnInfo.getCategoryName());;
// 数据集标识
Cell tableEnNameCell = dataRow.createCell(1);
tableEnNameCell.setCellStyle(dataStyle);
tableEnNameCell.setCellValue(columnDetail.getTableEnName());
...
rowIndex++;
}
效果图:

### 2. 其他问题
- 网上部分代码设置了response的header和Content-Type,但如果不做出相应接收,会报"Could not find acceptable representation"的错误
```
response.setHeader("content-Type", "application/octet-stream");//有的是application/vnd.ms-excel
response.setContentType("application/octet-stream");
poi坑点(springboot)的更多相关文章
- 踩坑之SpringBoot WebSocker 部署Tomcat冲突
启动tomcat容器,部署SpringBoot项目,启动项目报错,如下图: 看1.2的顺序就知道是WebSocket冲突了. 上网找了一下资料发现,使用@ServerEndpoint创立websock ...
- 【IDEA填坑】springboot整合ssm框架
遇到俩问题:一个是mybatis生疏 在EmpMapper.xml中定义resultMap <resultMap id="EmpWithDept" type="c ...
- springboot 填坑一 springboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
这里有个很不明显的错误 初次搭建很容易犯这个错
- Springboot使用Filter以及踩过的坑
Springboot使用Filter以及踩过的坑 在Springboot中使用Filter有两种方式,注解方式,注册bean方式 一.注解@WebFilter 1.实现Filter接口(javax.s ...
- linux上部署SpringBoot项目及遇到的问题
打开sftp步骤, 在显示的已连接的窗口上右键, 选择connect SFTP Session就可以打开文件上传的窗口 从windows上传文件到linux, 首先linux需要先切换到想要保存文件的 ...
- [03] SpringBoot+MyBatis+Shiro搭建杂谈
0.写在前面的话 一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控制 ...
- springboot服务的一些问题
一: springboot踩坑记--springboot正常启动但访问404; 1. spring boot的启动类不能直接放在main(src.java.main)这个包下面,把它放在有包的里面就可 ...
- SpringBoot如何返回页面
SpringBoot中使用Controller和页面的结合能够很好地实现用户的功能及页面数据的传递.但是在返回页面的时候竟然会出现404或者500的错误,我总结了一下如何实现页面的返回以及这里面所包含 ...
- SpringCloud的分布式配置及消息总线
1.在搭建分布式配置时,我们大概看下分布式配置的流程 如图所示: 当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现 ...
随机推荐
- [LeetCode] 727. Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- css设置不可复制
-moz-user-select:none; /* Firefox私有属性 */ -webkit-user-select:none; /* WebKit内核私有属性 */ -ms-user-selec ...
- JVM学习笔记1
1.运行时数据结构 2.堆分代 3.垃圾收集器 Parallel Scavenge收集器:新生代称为PSYoungGen,老年代称为ParOldGen,永久代称为Metaspace ParNew收集器 ...
- SpringBoot+EventBus使用教程(二)
简介 继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava- ...
- mvn手动上传jar到本地仓库
mvn install:install-file -Dfile=G:\elastic-project\workspace\out\artifacts\xxl_job_core_jar\xxl-job- ...
- VHR配置数据库开发环境
一,vhr项目宏观分析 目的:实现机关和事业单位的人事管理信息系统. 软件使用的对象:面向机关和事业单位内人事信息管理人员和在职开发人员. [架构选型] vhr面向的群体范围并不大,并非属于互联网应用 ...
- RDD的转换操作,分三种:单value,双value交互,(k,v)对
import org.apache.spark.rdd.RDDimport org.apache.spark.{Partitioner, SparkConf, SparkContext} object ...
- 关于 Task.Run 简单的示例
1. 关于 Task.Run 简单的示例01 直接贴代码了: public static class TaskDemo01 { public static void Run() { Console.W ...
- k8s网络原理
https://blog.csdn.net/watermelonbig/article/details/80646988 k8s中,每个 Pod 都有一个独立的 IP 地址,所有 Pod 在一个网络空 ...