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的更多相关文章

  1. java使用poi生成导出Excel(新)

    导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...

  2. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  3. POI通用导出Excel数据(包括样式设计)

    前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...

  4. POI导入导出Excel(HSSF格式,User Model方式)

    1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...

  5. Java POI导入导出Excel

    1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...

  6. java中使用poi导入导出excel文件_并自定义日期格式

    Apache POI项目的使命是创造和保持java API操纵各种文件格式基于Office Open XML标准(OOXML)和微软的OLE复合文档格式(OLE2)2.总之,你可以读写Excel文件使 ...

  7. SpringMvc 使用poi导入导出Excel

    Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...

  8. springMVC中使用POI方式导出excel至客户端、服务器实例

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 这里的方法支持导出excel至项目所在服务器,或导 ...

  9. springMVC框架+POI组件导出Excel

    目的:访问url(http://localhost:8080/POIOutputExcel/outputexcel.do)实现excel导出,效果图如下: 文件目录(配置文件就不多说了,跟前面的随笔一 ...

随机推荐

  1. git 在某个分支下创建新分支

    首先要强调一个观念,那就是在某个分支A下创建新的分支B,是指使用A分支下的代码,并不是A/B这样的层级结构. 比如,我想要在非主分支dev 下面创建子分支dev_dev >>>1.创 ...

  2. Java StringBuffer和StringBuilder类

    Java StringBuffer和StringBuilder类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. 和String类不同的是,StringBu ...

  3. 第2章 Java基本语法(下): 流程控制--项目(记账本)

    2-5 程序流程控制 2-5-1 顺序结构 2-5-2 分支语句1:if-else结构 案例 class IfTest1{ public static void main(String[] args) ...

  4. Linux 开机启动 php socket

    问题 php socket 服务在服务器重启后无法自动启动,需要添加开机启动脚本.有以下问题 开机延迟3分钟后,再启动socket服务 socket服务有3个模块需要按照先后顺序启动  registe ...

  5. 强制找回gitlab管理员密码

    强制找回gitlab管理员密码 最近使用gitlab的时候发现管理员密码忘记,现将找回密码的操作过程记录下来. 1.在gitlab登录窗口 如果密码忘记了登录不进入,可以先尝试点击登录框下方的Forg ...

  6. FPGA 中三角函数的实现

    FPGA 中三角函数的实现

  7. 【Python】hasattr() getattr() setattr() 使用方法详解

    本文转自 https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回B ...

  8. git提交代码五部曲

    From: https://jingyan.baidu.com/article/359911f5a4fe4b57fe03060d.html 正常使用git时,提交代码五部曲. 工具/原料   电脑 已 ...

  9. 学习笔记之Machine Learning Crash Course | Google Developers

    Machine Learning Crash Course  |  Google Developers https://developers.google.com/machine-learning/c ...

  10. CSS之img标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...