(这里写自定义目录标题)SpringBoot项目POI实现Excel表格的导入导出
POI的版本为4.0.0,网上查的资料大多都是POI3.12版本的,查了好多4.0.0版本的单元格样式,整理了一下。代码如下
pom.xml注入依赖
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.0.0</version>
</dependency>
控制层导出代码
@RequestMapping(value = “/export”)
@ResponseBody
public void export(HttpServletResponse response) throws IOException {
List user =UserService.selectUser();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("系统价格导入");
HSSFCellStyle style=wb.createCellStyle();
HSSFFont font=wb.createFont();
HSSFRow row = null;
row = sheet.createRow(0);//创建第一个单元格
row.setHeight((short) (26.25 * 20));
Cell cell=row.createCell(0);
cell.setCellValue("价格维护");//为第一行单元格设值
//为第一个单元格设置样式
style.setAlignment(HorizontalAlignment.CENTER); //左右居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
font.setFontName("黑体");
font.setFontHeightInPoints((short)18);//设置字体大小
font.setBold(true);//设置字体加粗
style.setFont(font);
cell.setCellStyle(style);
/*为标题设计空间
* firstRow从第1行开始
* lastRow从第0行结束
* 从第1个单元格开始
* 从第3个单元格结束
*/
CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0,12);
sheet.addMergedRegion(rowRegion);
/*
* 动态获取数据库列 sql语句 select COLUMN_NAME from INFORMATION_SCHEMA.Columns where table_name='user' and table_schema='test'
* 第一个table_name 表名字
* 第二个table_name 数据库名称
*/
row = sheet.createRow(1);
//设置背景色
//创建单元格样式
HSSFCellStyle cellstyle=wb.createCellStyle();
cellstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellstyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());//设置图案背景颜色
cellstyle.setAlignment(HorizontalAlignment.CENTER); //左右居中
cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
cellstyle.setWrapText(true);//设置自动换行
row.setHeight((short) (22.50 * 20));//设置行高
row.setRowStyle(cellstyle);
row.createCell(0).setCellValue("序号");//为第一个单元格设值
row.createCell(1).setCellValue("装置");
row.createCell(2).setCellValue("价格分类");
row.createCell(3).setCellValue("指标名称");
row.createCell(4).setCellValue("日期");
row.createCell(5).setCellValue("计算价格");
for (int i = 0; i < user.size(); i++) {
row = sheet.createRow(i + 2);
User user = User.get(i);
row.createCell(1).setCellValue(user.getDevice());
row.createCell(2).setCellValue(user.getPricetype());
row.createCell(3).setCellValue(user.getIndexname());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String udate=sdf.format(user.getUdate());
row.createCell(4).setCellValue(udate);
row.createCell(5).setCellValue(user.getSumprice());
}
sheet.setDefaultRowHeight((short) (16.5 * 20));
//列宽自适应
for (int i = 0; i <= 13; i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=user.xls");//默认Excel名称
wb.write(os);
os.flush();
os.close();
}
前端代码
- 用户信息
-
<li>用户信息</li> <li> <a class="btn btn-default" href="/User/add" target="_blank" rel="external nofollow" ><span class="fa fa-check-square"></span>  新增</a></li> <li> <input type="button" name="Submit2" value="导入" class="btn" onclick="window.open('/User/daoruview','newwindow2', 'height=680, width=1000, top=120, left=400,toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=yes, status=yes')" /></li> <li> <a href="/User/export" target="_blank" rel="external nofollow" ><button type="button" class="btn btn-primary">导出</button></a></li> </ul> <ul class="top-toolbar"></ul> </div>
其他的基本网上查资料就可以查得到