先把报表模板截图贴上来

下面是POI编写的报表生成类ExcelReport.java

package com.jadyer.report;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; /**
* 使用POI生成Excel报表
* @see 它所生成的报表是根据Excel模块文件生成的
* @see 这里要用到poi-3.9-20121203.jar和poi-ooxml-3.9-20121203.jar
* @see 另外模板文件<<ReportTemplate.xls>>下载地址为http://download.csdn.net/detail/jadyer/5736263
* @create Jul 5, 2013 9:54:46 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/
public enum ExcelReport {
//实现单例模式的唯一实例
INSTANCE;
/**报表模板文件的存储位置*/
private static final String REPORT_TEMPLATE_FILE_PATH = "/ReportTemplate.xls";
/**本列开始填充序号的标识*/
private static final String SERIAL_NO = "serialNo";
/**本行开始填充数据的标识*/
private static final String DATA_BEGIN = "dataBegin";
/**表格采用同列样式的标识*/
private static final String USE_STYLES = "useStyles";
/**表格样式采用的默认样式*/
private static final String DEFAULT_STYLES = "defaultStyles";
/**初始行的下标(指的是填充数据的第一个单元格下标)*/
private int initRowIndex;
/**初始列的下标(指的是填充数据的第一个单元格下标)*/
private int initColIndex;
/**当前行的下标(指的是填充数据的当前单元格下标)*/
private int currRowIndex;
/**当前列的下标(指的是填充数据的当前单元格下标)*/
private int currColIndex;
/**最后一行的下标*/
private int lastRowIndex;
/**序号列的第一个单元格的下标*/
private int serialColIndex;
/**默认行高(指的是填充数据的第一个单元格的行高)*/
private float defaultRowHeight;
/**存放模板中所有表格样式(键为99表示表格的默认样式)*/
private Map<Integer, CellStyle> allCellStyle = new HashMap<Integer, CellStyle>();
private Row currRow;
private Sheet sheet;
private Workbook wb; /**
* 基础数据初始化
*/
private ExcelReport(){
try {
//从指定目录中读取
//wb = WorkbookFactory.create(new File(REPORT_TEMPLATE_FILE_PATH));
//从classpath中读取模板文档
wb = WorkbookFactory.create(ExcelReport.class.getResourceAsStream(REPORT_TEMPLATE_FILE_PATH));
//获取模板中的第一个Sheet
sheet = wb.getSheetAt(0);
} catch (InvalidFormatException e) {
throw new RuntimeException("模板文件格式无效", e);
} catch (IOException e) {
throw new RuntimeException("模板文件不存在", e);
}
for(Row row : sheet){
for(Cell cell : row){
//报表模板文件default.xls中约定序号和SERIAL_NO和DATA_BEGIN都是String类型的
if(Cell.CELL_TYPE_STRING != cell.getCellType()){
continue;
}
String str = cell.getStringCellValue().trim();
//收集默认的表格样式
if(DEFAULT_STYLES.equals(str)){
this.allCellStyle.put(99, cell.getCellStyle());
}
//收集除默认表格样式以外的所有表格样式
if(USE_STYLES.equals(str)){
this.allCellStyle.put(cell.getColumnIndex(), cell.getCellStyle());
}
//定位序号列的第一个单元格下标
if(SERIAL_NO.equals(str)){
this.serialColIndex = cell.getColumnIndex();
}
//定位开始填充数据的第一个单元格的下标
if(DATA_BEGIN.equals(str)){
this.initColIndex = cell.getColumnIndex();
this.initRowIndex = row.getRowNum();
this.currColIndex = this.initColIndex;
this.currRowIndex = this.initRowIndex;
this.lastRowIndex = sheet.getLastRowNum();
this.defaultRowHeight = row.getHeightInPoints();
}
}
}
} /**
* 创建行
*/
public void createNewRow(){
//下移行的条件有2个:当前行非初始行,且当前行没有超过最后一行
if(this.currRowIndex!=this.initRowIndex && this.lastRowIndex>this.currRowIndex){
//将指定的几行进行下移一行
sheet.shiftRows(this.currRowIndex, this.lastRowIndex, 1, true, true);
//既然下移了那么最后一行下标就也要增大了
this.lastRowIndex++;
}
//在指定的行上创建一个空行(如果此行原本有单元格和数据,那么也会被空行覆盖,且创建出来的空行是没有单元格的)
this.currRow = sheet.createRow(this.currRowIndex);
this.currRow.setHeightInPoints(this.defaultRowHeight);
this.currRowIndex++;
this.currColIndex = this.initColIndex;
} /**
* 构造单元格(包括创建单元格和填充数据)
*/
public void buildCell(String value){
Cell cell = this.currRow.createCell(this.currColIndex);
if(this.allCellStyle.containsKey(this.currColIndex)){
cell.setCellStyle(this.allCellStyle.get(this.currColIndex));
}else{
cell.setCellStyle(this.allCellStyle.get(99));
}
cell.setCellValue(value);
this.currColIndex++;
} /**
* 插入序号
*/
private void insertSerialNo(){
int index = 1;
Row row = null;
Cell cell = null;
for(int i=this.initRowIndex; i<this.currRowIndex; i++){
row = sheet.getRow(i);
cell = row.createCell(this.serialColIndex);
cell.setCellValue(index++);
}
} /**
* 替换模板文件中的常量
*/
private void replaceConstantData(){
Map<String, String> constantData = new HashMap<String, String>();
constantData.put("title", "优秀学生名单");
constantData.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
constantData.put("developer", "玄玉博客<http://blog.csdn.net/jadyer>");
for(Row row : sheet){
for(Cell cell : row){
if(Cell.CELL_TYPE_STRING != cell.getCellType()){
continue;
}
String str = cell.getStringCellValue().trim();
if(str.startsWith("#")){
if(constantData.containsKey(str.substring(1))){
cell.setCellValue(constantData.get(str.substring(1)));
}
}
}
}
} /**
* 将生成的excel文件写到输出流中
* @see 适用于文件下载
*/
public void writeToStream(OutputStream os){
this.insertSerialNo();
this.replaceConstantData();
try {
wb.write(os);
} catch (IOException e) {
throw new RuntimeException("写入流失败", e);
}
} /**
* 将生成的excel文件写到指定的文件中
* @see 适用于硬盘保存
*/
public void writeToFile(String filepath){
this.insertSerialNo();
this.replaceConstantData();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
wb.write(fos);
} catch (FileNotFoundException e) {
throw new RuntimeException("写入的文件[" + filepath + "]不存在", e);
} catch (IOException e) {
throw new RuntimeException("写入数据失败", e);
} finally {
if(null != fos){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

最后是其单元测试类ExcelReportTest.java(即演示实际调用步骤)

package com.jadyer.report;

import java.io.File;

import org.junit.Assert;
import org.junit.Test; import com.jadyer.report.ExcelReport; public class ExcelReportTest {
@Test
public void testExcelReportUtil(){
ExcelReport eru = ExcelReport.INSTANCE;
eru.createNewRow();
eru.buildCell("aa");
eru.buildCell("玄玉");
eru.buildCell("cc");
eru.buildCell("dd");
eru.createNewRow();
eru.buildCell("aa");
eru.buildCell("http://blog.csdn.net/jadyer");
eru.buildCell("cc");
eru.buildCell("dd");
eru.createNewRow();
eru.buildCell("aa");
eru.buildCell("蓄机而动");
eru.buildCell("cc");
eru.buildCell("dd");
eru.writeToFile("D:/test.xls");
Assert.assertTrue(new File("D:/test.xls").exists());
}
}

使用POI生成Excel报表的更多相关文章

  1. poi生成excel报表合并列

    功能任务 poi导出excel统计报表,有合并列的. 根据结构生成层级. 目标 1输入一个linkshashmap LinkedHashMap<String, Object> fieldM ...

  2. ASP.NET MVC 4.0 中使用NPOI 2.2.0 按模板生成Excel报表

    使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/ ...

  3. java生成excel报表文件

    此次简单的操作将数据从数据库导出生成excel报表以及将excel数据导入数据库 首先建立数据库的连接池: package jdbc; import java.io.FileInputStream; ...

  4. POI生成EXCEL文件

    POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...

  5. POI生成EXCEL,公式不自动执行的有关问题

    POI生成EXCEL,公式不自动执行的问题 场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式 ...

  6. poi生成excel整理(设置边框/字体/颜色/加粗/居中/)

    转: poi生成excel整理(设置边框/字体/颜色/加粗/居中/) 2016年12月02日 11:05:23 吃奶的牛 阅读数:34324   HSSFWorkbook wb = new HSSFW ...

  7. java使用poi生成excel

    使用poi生成excel通常包含一下几个步骤 创建一个工作簿 创建一个sheet 创建一个Row对象 创建一个cell对象(1个row+1个cell构成一个单元格) 设置单元格内容 设置单元格样式. ...

  8. 使用node.js生成excel报表下载(excel-export express篇)

    引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...

  9. Java 利用 poi 生成 Excel文件的通用例子

    在用java 写数据库应用的时候, 通常会生成各种报表,而这些报表可能会被导出为各种格式的文件,比如Excel文档,pdf 文档等等. 今天先做了一个生成Excel 文档的例子,主要解决以下问题: 生 ...

随机推荐

  1. ActivityManager

    android.app.ActivityManager 这个类主要用来管理全部设备上的Activities. 权限:android.permission.GET_TASKS 方法:| 返回类型     ...

  2. asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师

    Membership.MembershipUser和Roles类 用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的. Membership:用户成员账号管理, ...

  3. Python全栈开发

    Python全栈开发 一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了. 一.装饰器 装饰器可以使函数执行前和执行后分别执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之为“ ...

  4. 利用VS2005进行dump文件调试(17篇博客)

    前言:利用drwtsn32或NTSD进行程序崩溃处理,都可以生成可用于调试的dmp格式文件.使用VS2005打开生成的DMP文件,能很方便的找出BUG所在位置.本文将讨论以下内容: 1.  程序编译选 ...

  5. MYSQL查询表结构

    mysql查看表结构命令,如下: desc 表名;show columns from 表名;describe 表名;show create table 表名; use information_sche ...

  6. HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  7. 定义自己的仪表板DashBoard - -kankanstyle

    既然做了奶站软件,需要使用的仪表板,显示质量数据 public class MDashboard extends ImageView { private Bitmap mPointerBitmap; ...

  8. poj 3082多边形相交 'Roid Rage

    题意是判断多边形是否相交 主要的思路就是判断每一个点是否在另外的多变形内 判断一个点是否在另一个多边形内主要思路是: 判断的那个点向左边做射线,如果射线与多边形的交点为奇数个则在多边形内,偶数个则不在 ...

  9. JS - 点击 “+” 、“-” 改变数字

    效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.a ...

  10. 控制台程序的参数解析类库 CommandLine

    C#控制台程序的参数解析类库 CommandLine简单使用说明 前言 C#开发的控制台程序,默认接收string[] args参数.如果有多个参数需要输入时,可以按照顺序依次输入:但如果有些参数不是 ...