package cn.com.excel;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
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.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
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.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
public abstract class ChatExcel<T> {
private Class<?> clazz;
public XSSFWorkbook wb;
public Sheet sheet;
private FileInputStream fileInput=null;
private FileChannel fileChannel=null;
private FileLock fileLock=null;
/**
* 路径是否是文件
*/
public boolean isFile=false;
/**
* 当前行号
*/
public int rownum;
private List<String> headerList=new ArrayList<String>();
private int headnum=0;
/**
* 样式列表
*/
private Map<String, CellStyle> styles;
private String filePath;
/**
* 注解列表(Object[]{ ExcelField, Field/Method })
*/
List<Object[]> annotationList = new ArrayList<>();
public ChatExcel(String filePath) {
this(filePath, "聊天记录",1);
}
//检测文件大小。
private File checkFilePath(String filePath) {
try {
File file=new File(filePath);
if(!file.exists()){
return file;
}else{
//如果文件超出自定义的大小,新建文件
long fileSize=file.length();
if(fileSize > 1328427){//1.26MB左右,大概4-5万行左右
// 自定义的文件名称
// String fileName=DateUtils.formatDate(new Date(), "yyyyMMdd")+"-"+file.getName();
String fileName="2017";
File path=new File(file.getParent());
FileUtils.copyFile(file, new File(path,fileName));
file.delete();
}
return file;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ChatExcel(String filePath,String title,int headnum) {
File file=checkFilePath(filePath);
try {
clazz=Reflections.getClassGenricType(getClass());
if(file.exists() && file.isFile()){//文件存在//追加
isFile=true;
fileInput= new FileInputStream(file);
// fileChannel=new RandomAccessFile(file, "rw").getChannel();
wb=new XSSFWorkbook(fileInput);
sheet=wb.getSheet("message");
// fileLock=fileChannel.lock();
}else{//文件不存在 // 新建
isFile=false;
wb=new XSSFWorkbook();
sheet=wb.createSheet("message");
}
this.filePath=filePath;
this.headnum=headnum;
addHeader(title);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取最后一个数据行号
* @return
*/
public int getLastDataRowNum(){
return this.sheet.getLastRowNum()+headnum;
}
public void setData(Row row,T data){
int colunm = 0;
Object val = null;
for (Object[] os : annotationList){
if(os[1] instanceof Field){
val = Reflections.invokeGetter(data, ((Field)os[1]).getName());
}else if (os[1] instanceof Method){
val = Reflections.invokeMethod(data, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
}
this.addCell(row, colunm++, val, 2, Class.class);
}
}
public void setListData(int rownum,List<T> data){
for(T m:data){
Row row= sheet.createRow(rownum++);
setData(row, m);
}
}
public abstract void appenList(List<T> listdata);
/**
* 添加一个单元格
* @param row 添加的行
* @param column 添加列号
* @param val 添加值
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
* @return 单元格对象
*/
private Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
Cell cell = row.createCell(column);
String cellFormatString = "@";
try {
if(val == null){
cell.setCellValue("");
}else if(fieldType != Class.class){
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
}else{
if(val instanceof String) {
cell.setCellValue((String) val);
}else if(val instanceof Integer) {
cell.setCellValue((Integer) val);
cellFormatString = "0";
}else if(val instanceof Long) {
cell.setCellValue((Long) val);
cellFormatString = "0";
}else if(val instanceof Double) {
cell.setCellValue((Double) val);
cellFormatString = "0.00";
}else if(val instanceof Float) {
cell.setCellValue((Float) val);
cellFormatString = "0.00";
}else if(val instanceof Date) {
cell.setCellValue((Date) val);
cellFormatString = "yyyy-MM-dd HH:mm";
}else {
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
}
}
if (val != null){
CellStyle style = styles.get("data_column_"+column);
if (style == null){
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"+(align>=1&&align<=3?align:"")));
style.setDataFormat(wb.createDataFormat().getFormat(cellFormatString));
styles.put("data_column_" + column, style);
}
cell.setCellStyle(style);
}
} catch (Exception ex) {
cell.setCellValue(val.toString());
}
return cell;
}
private void addHeader(String title){
List<Field> listField= Arrays.asList(clazz.getDeclaredFields());
for(Field field:listField){
ChatField fieldClass=field.getAnnotation(ChatField.class);
if(fieldClass!=null){
annotationList.add(new Object[]{fieldClass,field});
//
没有合适的资源?快使用搜索试试~ 我知道了~
java,poi 实现数据追加功能
共40个文件
xml:24个
class:8个
java:7个
5星 · 超过95%的资源 需积分: 47 39 下载量 27 浏览量
2017-06-02
17:01:02
上传
评论
收藏 41KB RAR 举报
温馨提示
maven 项目。直接运行项目里的TestAddExcel类 即可看效果。
资源推荐
资源详情
资源评论
收起资源包目录
excelAdd.rar (40个子文件)
excelAdd
pom.xml 3KB
target
classes
cn
com
excel
BaseEntity.class 420B
Reflections.class 8KB
ChatExcel$1.class 1KB
ChatField.class 450B
ChatMsg.class 2KB
ChatExcel.class 13KB
ChatMsgExcel.class 2KB
TestAddExcel.class 2KB
generated-sources
annotations
excelAdd.iml 2KB
src
main
webapp
WEB-INF
web.xml 4KB
log4j.xml 2KB
applicationContext.xml 2KB
resources
java
cn
com
excel
TestAddExcel.java 1KB
ChatField.java 344B
ChatMsg.java 2KB
ChatExcel.java 14KB
BaseEntity.java 127B
Reflections.java 11KB
ChatMsgExcel.java 2KB
.idea
misc.xml 1KB
modules.xml 256B
dictionaries
xu_xiang.xml 89B
encodings.xml 200B
compiler.xml 641B
kotlinc.xml 232B
workspace.xml 44KB
libraries
Maven__org_apache_poi_poi_3_9.xml 470B
Maven__stax_stax_api_1_0_1.xml 479B
Maven__commons_codec_commons_codec_1_5.xml 536B
Maven__org_apache_commons_commons_lang3_3_4.xml 556B
Maven__org_apache_poi_poi_ooxml_3_9.xml 512B
Maven__dom4j_dom4j_1_6_1.xml 462B
Maven__xml_apis_xml_apis_1_0_b2.xml 502B
Maven__commons_io_commons_io_1_3_2.xml 517B
Maven__org_apache_poi_poi_ooxml_schemas_3_9.xml 568B
Maven__org_apache_xmlbeans_xmlbeans_2_3_0.xml 539B
artifacts
excelAdd_war.xml 307B
excelAdd_war_exploded.xml 2KB
inspectionProfiles
Project_Default.xml 378B
共 40 条
- 1
资源评论
- haha_hei2017-08-16很有用,多谢啦
WD4871
- 粉丝: 17
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功