package poi;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
* Excel模板实现类<BR>
* 实现通过自定义Excel数据模版,将结果集填充到模版相应位置,自动创建输出到指定的文件,允许Excel模版设置公式,调用方法如下:<BR>
*
* <pre>
* ExcelTemplate template=new ExcelTemplate(templateFilePath,outputFilePath)
* //template.setIncludeFormula(true);设置包含公式
* template.generate(ResultSet);//resultset为ArrayList对象,数据行以Map封装
* //template.generate(titleMap,dataList)//显示主表、明细表信息
* </pre>
*
*/
@SuppressWarnings("deprecation")
public class ExcelTemplate {
// Logger logger = LoggerFactory.getLogger(ExcelTemplate.class);
/**
* 模板文件名
*/
private String templateFile;
/**
* 输出文件名
*/
private String outputFile;
/**
* Excel模板定义的输出字段名数组
*/
private String[] fieldNames;
/**
* 输出的起始行,默认为-1,不输出
*/
private int startRow = -1;
private int tempStartRowNum = -1;
/**
* 默认字体大小
*/
private int fontSize = 10;
/**
* 默认字体
*/
private String fontName = "宋体";
/**
* 是否设置信息标题栏边框,默认情况不设置边框
*/
private boolean titleCellBold = false;
/**
* 是否设置空白栏边框,默认情况不设置边框
*/
private boolean blankCellBold = false;
/**
* 是否自动分工作薄
*/
private boolean autoSheet = false;
/**
* 是否自动分页
*/
private boolean autoPagination = false;
/**
* 分页行数
*/
private int maxrow = -1;
/**
* 是否有公式
*/
private boolean hasFormula = false;
/**
* 关键字 &-表示模版信息内容字段 #-表示模版明细内容字段 formula-表示模版函数关键字 ~-表示Cell当前行,当包含":"时,表示当前行减1
*/
private final String TITLE_FLAG = "&";
private final String CONTENT_FLAG = "#";
private final String FORMULA_FLAG = "formula";
private final String UNLIMIT_FLAG = "~";
private final String FIELD_AUTO_ID = "_id";
/**
* 公式计算操作符号
*/
private final String[] OP_FLAG = new String[] { "+", "-", "*", "/", "%", ":" };
/**
* 默认构造函数
*
*/
public ExcelTemplate() {
}
/**
* 构造器
*
* @param templateFile
* 模版文件
* @param outputFile
* 输出文件
*/
public ExcelTemplate(String templateFile, String outputFile) {
this.templateFile = templateFile;
this.outputFile = outputFile;
}
/**
* 设置模版文件是否包含Excel公式
*
* @param hasFormula
*/
public void setIncludeFormula(boolean hasFormula) {
this.hasFormula = hasFormula;
}
/**
* 设置标题栏是否需要边框
*
* @param b
*/
public void setTitleCellBold(boolean titleCellBold) {
this.titleCellBold = titleCellBold;
}
/**
* 设置空白行是否需要显示边框
*
* @param blankCellBold
*/
public void setBlankCellBold(boolean blankCellBold) {
this.blankCellBold = blankCellBold;
}
/**
* 设置是否分工作薄
*
* @param b
*/
public void setAutoSheet(boolean autoSheet) {
this.autoSheet = autoSheet;
this.autoPagination = (autoSheet ? false : autoPagination);
}
/**
* 是否自动分页
*
* @param autoPagination
*/
public void setAutoPagination(boolean autoPagination) {
this.autoPagination = autoPagination;
this.autoSheet = (autoPagination ? false : autoSheet);
}
/**
* 设置分页最大行
*
* @param maxrow
*/
public void setMaxRow(int maxrow) {
this.maxrow = maxrow;
}
/**
* 设置字体大小,默认10号字体
*
* @param size
*/
public void setFontSize(int size) {
this.fontSize = size;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
/**
* 初始化工作模版,获取模版配置起始行(start)以及对应字段填充位置(fieldNames)
*
* @param sheet
*/
private void initialize(HSSFSheet sheet) {
boolean setStart = false;
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
String value = null;
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
value = "" + cell.getNumericCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
value = "" + cell.getBooleanCellValue();
} else {
value = cell.getRichStringCellValue().getString();
}
if (value != null && !"".equals(value)) {
value = value.trim();
// 内容数据
if (value.startsWith(CONTENT_FLAG)) {
if (!setStart) {
this.startRow = r;// 设置内容填充起始行
this.fieldNames = new String[cells];
setStart = true;
}
this.fieldNames[c] = value.substring(1);// 初始化内容字段
}
}
}
}
}
}
}
/**
* 计算公式,默认范围从0行到工作薄结尾
*
* @param wb
* @param sheet
*/
private void calcFormula(HSSFWorkbook wb, HSSFSheet sheet) {
this.calcFormula(wb, sheet, 0, sheet.getPhysicalNumberOfRows());
}
/**
* 计算公式函数,范围从开始行(start_row)到结束行(end_row)
*
* @param wb
* HSSFWorkbook
* @param sheet
* HSSFSHeet
* @param start_rang
* @param end_rang
*/
private void calcFormula(HSSFWorkbook wb, HSSFSheet sheet, int start_rang, int end_rang) {
// int rows = sheet.getPhysicalNumberOfRows();
HSSFCellStyle borderStyle = this.getBorderStyle(wb);
HSSFCellStyle noneStyle = this.getNoneStyle(wb);
for (int r = start_rang; r < end_rang; r++) {
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
String value = cell.getRichStringCellValue().getString();
if (value != null) {
value = value.trim().toLowerCase();
if (value.startsWith(FORMULA_FLAG)) {
int index = value.indexOf("=");
String formula = value.substring(index + 1);
// 判断函数是否包含以#开头,如果是以#开头表示必须显示边框,
String flag = formula.substring(0, 1);
boolean showBold = false;
if (flag.equals(CONTENT_FLAG)) {
formula = formula.substring(1);
showBold = true;
}
// 如果包含':'符号则统计公式不包含当前行,否则会引发公式循环引用错误.
if (formula.indexOf(":") != -1) {
formula = formula.rep
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
源码资料.zip (21个子文件)
bullionConfigForAg.xml 900B
bullionForAg.xls 16KB
postback.html 1KB
common.js 21KB
poi
ExcelConfigure.java 2KB
ExcelImportErrLog.java 3KB
ExcelConstants.java 351B
ExcelImporter.java 1KB
ExcelRows.java 2KB
ExcelSheet.java 4KB
ExcelConfigureParser.java 5KB
ExcelTemplate.java 31KB
ExcelReader.java 24KB
ExcelCell.java 4KB
controller
ImportationController.java 2KB
service
ExcelService.java 830B
Bullion.java 6KB
bullioncreater.html 5KB
util
MappingFastJsonHttpMessageConverter.java 2KB
CommonUtils.java 3KB
BaseAppException.java 435B
共 21 条
- 1
资源评论
acp_
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功