/**
* Copyright (c) 2008-2013 by DHCC
* All rights reserved.
*/
package com.dhcc.poi.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
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;
/**
* 该类实现了基于模板的导出
* 如果要导出序号,需要在excel中定义一个标识为sernums
* 如果要替换信息,需要传入一个Map,这个map中存储着要替换信息的值,在excel中通过#来开头
* 要从哪一行那一列开始替换需要定义一个标识为datas
* 如果要设定相应的样式,可以在该行使用styles完成设定,此时所有此行都使用该样式
* 如果使用defaultStyls作为表示,表示默认样式,如果没有defaultStyles使用datas行作为默认样式
*
* @author <a href="mailto:zhangyanyu@dhcc.com.cn">zhangyanyu</a>
* @date 2013-7-18
*/
public class ExcelTemplate {
/**
* 数据行标识
*/
public static final String DATA_LINE = "datas";
/**
* 默认样式标识
*/
public static final String DEFAULT_STYLE = "defaultStyle"; //默认样式
/**
* 列样式标识
*/
public static final String STYLE = "styles"; //样式
/**
* 插入序号标识
*/
public static final String SER_NUM = "sernums"; //序号
private static ExcelTemplate et = new ExcelTemplate();
private Workbook wb;
private Sheet sheet;
private int initColIndex;//数据的初始化列数
private int initRowIndex;//数据的初始化行数
private int curColIndex;//当前列数
private int curRowIndex;//当前行数
private Row curRow;//当前行对象
private int lastRowIndex;//最后一行的行数
private CellStyle defaultStyle;//默认样式
private float rowHeight;//默认行高
//存储每一列所对应的样式
private Map<Integer,CellStyle> styles = new HashMap<Integer,CellStyle>();
private int serColIndex;//序号的列
private ExcelTemplate(){
}
public static ExcelTemplate getInstance(){
return et;
}
/**
* 从classpath路径下读取相应的模板文件
* @param path
* @return
*/
public ExcelTemplate readTemplateByClasspath(String path){
try {
wb = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(path));
initTemplate();
} catch (InvalidFormatException e) {
throw new RuntimeException("读取模板格式有错,请检查");
} catch (IOException e) {
throw new RuntimeException("读取模板不存在,请检查");
}
return this;
}
/**
* 从某个路径来读取模板
* @param path
* @return
*/
public ExcelTemplate readTemplateByPath(String path){
try {
wb = WorkbookFactory.create(new File(path));
initTemplate();
} catch (InvalidFormatException e) {
throw new RuntimeException("读取模板格式有错,请检查");
} catch (IOException e) {
throw new RuntimeException("读取模板不存在,请检查");
}
return this;
}
/**
* 将文件写到相应的路径下
* @param filepath
*/
public void writeToFile(String filePath){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
wb.write(fos);
} catch (FileNotFoundException e) {
throw new RuntimeException("写入的文件不存在");
} catch (IOException e) {
throw new RuntimeException("写入数据失败"+e.getMessage());
} finally{
try {
if(fos!=null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建相应的元素,基于String类型
* @param value
*/
public void createCell(String value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue(value);
curColIndex++;
}
public void createCell(int value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue((int)value);
curColIndex++;
}
public void createCell(Date value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue(value);
curColIndex++;
}
public void createCell(double value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue(value);
curColIndex++;
}
public void createCell(boolean value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue(value);
curColIndex++;
}
public void createCell(Calendar value) {
Cell c = curRow.createCell(curColIndex);
setCellStyle(c);
c.setCellValue(value);
curColIndex++;
}
/**
* 设置某个元素的样式
* @param c
*/
private void setCellStyle(Cell c){
if(styles.containsKey(curColIndex)) {
c.setCellStyle(styles.get(curColIndex));
} else {
c.setCellStyle(defaultStyle);
}
}
/**
* 创建新行,在使用时只要添加完一行,需要调用该方法创建
*/
public void createNewRow(){
if(lastRowIndex > curRowIndex&&curRowIndex!=initRowIndex){
sheet.shiftRows(curRowIndex, lastRowIndex, 1, true, true);//从当前行到最后一行往下移一行
lastRowIndex ++;
}
curRow = sheet.createRow(curRowIndex);
curRow.setHeightInPoints(rowHeight);
curRowIndex++;
curColIndex = initColIndex;
}
/**
* 插入序号,会自动找相应的序号标识的位置完成插入
*/
public void insertSer(){
int index = 1;
Row row = null;
Cell c = null;
for(int i=initRowIndex;i<curRowIndex;i++){
row = sheet.getRow(i);
c = row.createCell(serColIndex);
setCellStyle(c);
c.setCellValue(index++);
}
}
/**
* 根据map替换相应的常量,通过Map中的值来替换#开头的值
*/
public void replaceFinalData(Map<String, String> datas){
if(datas==null) return;
for(Row row : sheet){
for(Cell c : row){
if(c.getCellType()!=Cell.CELL_TYPE_STRING) continue;
String str = c.getStringCellValue().trim();
if(str.startsWith("#")){
if(datas.containsKey(str.substring(1))){
c.setCellValue(datas.get(str.substring(1)));
}
}
}
}
}
/**
* 初始化模板
*/
private void initTemplate(){
sheet = wb.getSheetAt(0);
initConfigData();
lastRowIndex = sheet.getLastRowNum();
curRow = sheet.createRow(curRowIndex);
}
/**
* 初始化数据信息
*/
private void initConfigData() {
boolean findData = false;
boolean findSer = false;
for(Row row : sheet){
if(findData)
没有合适的资源?快使用搜索试试~ 我知道了~
poi基于Excel模板导入导出实例
共23个文件
class:7个
java:7个
xls:4个
需积分: 50 46 下载量 102 浏览量
2016-11-21
16:00:42
上传
评论
收藏 49KB ZIP 举报
温馨提示
poi基于excel模板实现导入导出。代码里附Excel的模板,注释详细
资源推荐
资源详情
资源评论
收起资源包目录
poi_utils.zip (23个子文件)
poi_utils
pom.xml 1KB
target
test-classes
com
dhcc
poi
utils
TestPoi.class 4KB
TestTemplate.class 4KB
classes
com
dhcc
poi
model
User.class 2KB
utils
ExcelTemplate.class 8KB
ExcelResources.class 495B
ExcelUtil.class 9KB
ExcelHeader.class 2KB
excel
user.xls 17KB
default.xls 18KB
.settings
org.maven.ide.eclipse.prefs 218B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
dhcc
poi
utils
TestPoi.java 4KB
TestTemplate.java 4KB
main
java
com
dhcc
poi
model
User.java 2KB
utils
ExcelUtil.java 10KB
ExcelTemplate.java 10KB
ExcelResources.java 735B
ExcelHeader.java 1KB
excel
user.xls 17KB
default.xls 18KB
.project 563B
.classpath 512B
共 23 条
- 1
资源评论
chenjf1259
- 粉丝: 5
- 资源: 35
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功