如何用poi生成导出excel
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet; import java.util.List;
import java.util.Map; /**
* <p>excel 简单表格 赋值接口<p>
* @version 1.0
* @author li_hao
* @date 2017年1月18日
*/
public interface ColVal { void setColVal(List<Map<String, Object>> list, String[] keys, Sheet sheet, CellStyle colValCellStyle);
}
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import java.util.List;
import java.util.Map;
import java.util.Objects; /**
* <p>excel 简单表格 赋值 实现<p>
* @version 1.0
* @author li_hao
* @date 2017年1月18日
*/
public class CommonColVal implements ColVal { /**
* @param list 数据map
* @param keys 列值 key
* @param sheet {@link Sheet}
* @param colValCellStyle {@link CellStyle}
*/
@Override
public void setColVal(List<Map<String, Object>> list, String[] keys, Sheet sheet, CellStyle colValCellStyle) {
int rowNum = 1; // 行号
int i = 0;
for (; i < list.size(); i++, rowNum++) {
// 创建行
Row row = sheet.createRow(rowNum);
// 获取当前行 数据map
Map<String, Object> rowValMap = list.get(i); // 遍历 key值 给每个列赋值
for (int j = 0; j < keys.length; j++) {
Object cellVal = rowValMap.get(keys[j]); Cell cell = row.createCell(j);
cell.setCellValue(Objects.toString(cellVal, ""));
cell.setCellStyle(colValCellStyle);
}
}
}
}
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map; /**
* <p>导出Excel文档工具类<p>
* @version 1.0
* @author li_hao
* @date 2017年1月18日
*/
public class PoiUtils { private static CellStyle buildColValCellStyle(Workbook wb) {
CellStyle filedValCellStyle = wb.createCellStyle();
Font f2 = wb.createFont();
// 创建第二种字体样式(用于值)
f2.setFontHeightInPoints((short) 10);
f2.setColor(IndexedColors.BLACK.getIndex());
// 设置第二种单元格的样式(用于值)
filedValCellStyle.setFont(f2);
filedValCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
filedValCellStyle.setBorderRight(CellStyle.BORDER_THIN);
filedValCellStyle.setBorderTop(CellStyle.BORDER_THIN);
filedValCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
filedValCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
filedValCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return filedValCellStyle;
} private static CellStyle buildColNameCellStyle(Workbook wb) {
// 创建两种单元格格式
CellStyle filedNameCellStyle = wb.createCellStyle();
// 创建两种字体
Font f = wb.createFont();
// 创建第一种字体样式(用于列名)
f.setFontHeightInPoints((short) 10);
f.setColor(IndexedColors.BLACK.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 设置第一种单元格的样式(用于列名)
filedNameCellStyle.setFont(f);
filedNameCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
filedNameCellStyle.setBorderRight(CellStyle.BORDER_THIN);
filedNameCellStyle.setBorderTop(CellStyle.BORDER_THIN);
filedNameCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
filedNameCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
return filedNameCellStyle;
} /**
* 创建excel文档,
*
* @param list 数据
* @param keys list中map的key数组集合
* @param columnNames excel的列名
*/
public static Workbook createSheet(Workbook wb, String sheetName, String titleName, List<Map<String, Object>> list, String[] keys, String columnNames[], ColVal colVal) { // 创建第一个sheet(页),并命名
Sheet sheet = wb.createSheet(sheetName); setColLength(keys, sheet);
CellStyle colNameCellStyle = buildColNameCellStyle(wb);
CellStyle colValCellStyle = buildColValCellStyle(wb); setTitle(titleName, sheet, colNameCellStyle, new CellRangeAddress(0, 0, 0, keys.length - 1));
setColName(columnNames, sheet, colNameCellStyle, 1);
colVal.setColVal(list, keys, sheet, colValCellStyle); return wb;
} /**
* 创建excel文档,
*
* @param list 数据
* @param keys list中map的key数组集合
* @param columnNames excel的列名
*/
public static Workbook createSheetNoTitle(Workbook wb, String sheetName, List<Map<String, Object>> list, String[] keys, String columnNames[], ColVal colVal) { Sheet sheet = wb.createSheet(sheetName);
setColLength(keys, sheet);
CellStyle colNameCellStyle = buildColNameCellStyle(wb);
CellStyle colValCellStyle = buildColValCellStyle(wb);
setColName(columnNames, sheet, colNameCellStyle, 0);
colVal.setColVal(list, keys, sheet, colValCellStyle); return wb;
} private static void setTitle(String titleName, Sheet sheet1, CellStyle colNameCellStyle, CellRangeAddress cellRangeAddress) {
//表头
Row titleRow = sheet1.createRow(0);
Cell titleRowCell = titleRow.createCell(0);
titleRowCell.setCellValue(titleName);
titleRowCell.setCellStyle(colNameCellStyle);
sheet1.addMergedRegion(cellRangeAddress);
} private static void setColLength(String[] keys, Sheet sheet1) {
// 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
for (int i = 0; i < keys.length; i++) {
sheet1.setColumnWidth((short) i, (short) (35.7 * 150));
}
} private static void setColName(String[] columnNames, Sheet sheet1, CellStyle cs, int rowNum) {
// 列名行
Row row = sheet1.createRow(rowNum); //设置列名
for (int i = 0; i < columnNames.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(columnNames[i]);
cell.setCellStyle(cs);
}
} public static void setResponse(HttpServletResponse response, String fileName, ByteArrayOutputStream os) throws IOException {
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
} }
代码示例:

如何用poi生成导出excel的更多相关文章
- java使用poi生成导出Excel(新)
导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- POI通用导出Excel数据(包括样式设计)
前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...
- POI导入导出Excel(HSSF格式,User Model方式)
1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...
- Java POI导入导出Excel
1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...
- java中使用poi导入导出excel文件_并自定义日期格式
Apache POI项目的使命是创造和保持java API操纵各种文件格式基于Office Open XML标准(OOXML)和微软的OLE复合文档格式(OLE2)2.总之,你可以读写Excel文件使 ...
- SpringMvc 使用poi导入导出Excel
Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...
- springMVC中使用POI方式导出excel至客户端、服务器实例
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 这里的方法支持导出excel至项目所在服务器,或导 ...
- springMVC框架+POI组件导出Excel
目的:访问url(http://localhost:8080/POIOutputExcel/outputexcel.do)实现excel导出,效果图如下: 文件目录(配置文件就不多说了,跟前面的随笔一 ...
随机推荐
- hasura graphql-engine 集成zombodb
zombodb 是一个很不错的pg 扩展,可以方便的把es 与pg 集成起来,使用方便 ,目前尽管有一些docker 镜像 但是版本都比较老,所以基于centos7 做了一个新的docker 镜像,同 ...
- JavaScript图形库
做前端少不了画图,html5 的 canva 很强大,可是如果所有需求都自己画图,恐怕会被 leader 炒掉.记录一下我用过的几个 2D 和 3D 的js图形库,各种功能.我就不分 2D 和 3D ...
- 执行sql语句为什么?用PreparedStatement要比Statement好用
PreparedStatement public interface PreparedStatement extends Statement;可以看到PreparedStatement是Stateme ...
- 自己动手开发智能聊天机器人完全指南(附python完整源码)
一.前言 人工智能时代,开发一款自己的智能问答机器人,一方面提升自己的AI能力,另一方面作为转型AI的实战练习.在此把学习过程记录下来,算是自己的笔记. 二.正文 2.1 下载pyaiml 下载pya ...
- 浅读官方代码--ActionManager
用于管理节点的动作 { CCDirector* pDirector = CCDirector::sharedDirector(); //获得单例 pDirector->getActionMana ...
- 2017-07-06 eclipse在线安装SVN1.9插件
1,百度搜索subeclipse,点击第一个: 2,官网说,文档已移动到github wiki上: 3,打开github wiki,复制最新发布版本地址: 4,在eclipse里面,打开help-&g ...
- 团队第三次 # scrum meeting
github 本此会议项目由PM召开,召开时间为4-7日晚上9点 召开时长15分钟 任务表格 袁勤 继续学习SpringBoot https://github.com/buaa-2016/phyweb ...
- 什么是BOM?,什么是DOM? BOM跟DOM之间的关系
什么是BOM? BOM是browser object model的缩写,简称浏览器对象模型.是用来获取或设置浏览器的属性.行为,例如:新建窗口.获取屏幕分辨率.浏览器版本号等. 比如 alert(); ...
- vue-cli 选项无法选问题
winpty vue.cmd create admin 这样创建就可以了
- Java中线程池的实现原理
知识点总结 ---------------------------------------------------------------------------------------------- ...