工作上需要写了一个将数据库数据生成excel表的接口,在此过程中遇到了一些坑点,现在此纪录

PS:一部分可能是因为我没用明白

1. 样式问题

  • 自动调整尽量不要使用,部分列留白过多,空列列宽过窄,可能是只自动调整了一列的缘故。

    代码:

for (int index = 0; index < 14; index++){
sheet.autoSizeColumn(index);
}

效果图:

  • 建议使用setCellStyle(),而不是setRowStyle()。直接使用setRowStyle()会导致只有没存入

    数据的单元格设置样式成功。

    代码:

Row dataRow = sheet.createRow(rowIndex);

dataRow.setRowStyle(dataStyle);

效果图:
![](https://i.imgur.com/ktbPhHX.png)
![](https://i.imgur.com/j2s6597.png)
![](https://i.imgur.com/A63oPyu.png) - 最终成品 样式代码:
// 标题格式
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++;

}


效果图:
![](https://i.imgur.com/RlGrUpl.png) ### 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)的更多相关文章

  1. 踩坑之SpringBoot WebSocker 部署Tomcat冲突

    启动tomcat容器,部署SpringBoot项目,启动项目报错,如下图: 看1.2的顺序就知道是WebSocket冲突了. 上网找了一下资料发现,使用@ServerEndpoint创立websock ...

  2. 【IDEA填坑】springboot整合ssm框架

    遇到俩问题:一个是mybatis生疏  在EmpMapper.xml中定义resultMap <resultMap id="EmpWithDept" type="c ...

  3. springboot 填坑一 springboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

    这里有个很不明显的错误  初次搭建很容易犯这个错

  4. Springboot使用Filter以及踩过的坑

    Springboot使用Filter以及踩过的坑 在Springboot中使用Filter有两种方式,注解方式,注册bean方式 一.注解@WebFilter 1.实现Filter接口(javax.s ...

  5. linux上部署SpringBoot项目及遇到的问题

    打开sftp步骤, 在显示的已连接的窗口上右键, 选择connect SFTP Session就可以打开文件上传的窗口 从windows上传文件到linux, 首先linux需要先切换到想要保存文件的 ...

  6. [03] SpringBoot+MyBatis+Shiro搭建杂谈

    0.写在前面的话 一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控制 ...

  7. springboot服务的一些问题

    一: springboot踩坑记--springboot正常启动但访问404; 1. spring boot的启动类不能直接放在main(src.java.main)这个包下面,把它放在有包的里面就可 ...

  8. SpringBoot如何返回页面

    SpringBoot中使用Controller和页面的结合能够很好地实现用户的功能及页面数据的传递.但是在返回页面的时候竟然会出现404或者500的错误,我总结了一下如何实现页面的返回以及这里面所包含 ...

  9. SpringCloud的分布式配置及消息总线

    1.在搭建分布式配置时,我们大概看下分布式配置的流程 如图所示: 当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现 ...

随机推荐

  1. Location配置与ReWrite语法(五)

    原文链接:https://www.cnblogs.com/crazylqy/p/6892010.html location表示uri方式定位,基础语法有三种: location = pattern { ...

  2. OSI网络七层模型、TCP/IP 模型(四)

    OSI 是 Open System Interconnection 的缩写,译为“开放式系统互联”. OSI 模型把网络通信的工作分为 7 层,从下到上分别是物理层.数据链路层.网络层.传输层.会话层 ...

  3. appium--解决每次安装appium setting和Unlock

    前戏 每次启动appium进行自动化的时候,都会提示我们需要安装appium setting和Unlock,而且还都要手动确认 那这两个文件是做什么的呢? Appium settings:用于设置网络 ...

  4. 基于paramiko将文件上传到服务器上

    通过安装使用paramiko模块,将本地文件上传到服务器上 import paramiko import datetime import os hostname = '服务器ip' username ...

  5. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  6. 关于c语言的逻辑短路规则

    原来的代码是 if (temp == 3 && (a % b != 0 || b == 0 )){ printf("go"); } dev-c 报错: progra ...

  7. 本地手动一步步搭建WNMP环境(nginx+php+mysql) Windows平台

    环境:Windows 10 x64 参考文章: WNMP完整教程      windows下PHP环境的搭建 我自定义安装后的目录结构: +WNMP ++MySQL_Server-8.0.13 ++n ...

  8. 企业级Nginx负载均衡与keepalived高可用实战(一)Nginx篇

    1.集群简介 1.1.什么是集群 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器. ...

  9. C/C++深度优先搜索(递归树模拟)

    //C++深度优先搜索(递归树模拟) #define _CRT_SECURE_NO_WARNINGS #include <iostream> #define MAX_N 1000 usin ...

  10. linux中断子系统

    参考引用:http://www.wowotech.net/sort/irq_subsystem wowotech:一个很好的linux技术博客. 一.概述 目的 kernel管理硬件设备的方式:轮询. ...