service接口类
package com.deppon.tps.module.aboutexcel.server.poidlexcel.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisFileEntity;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisResponseEntity;
public interface IPOIdwExcelUtilsService {
/**
* 解析文件并返回成功,失败,失败信息等结果
* @param entity
* @return
*/
public AnalysisResponseEntity importFileData(AnalysisFileEntity entity);
/**
* 不需要模板,返回流,不需要生成excel文件
* @param title
* @param entity
* @return
*/
public ByteArrayOutputStream exportExcel(String title,AnalysisResponseEntity entity);
/**
* 需要模板,返回流,不需要生成excel文件
* @param title
* @param entity
* @return
*/
public ByteArrayOutputStream exportExcel(File file,AnalysisResponseEntity entity);
/**
* 需要模板,返回生成的excel路径,需要生成excel文件
* @param file
* @param entity
* @return
*/
public String createExcel(File file,AnalysisResponseEntity entity);
/**
* 下载excel
* @param path
* @param response
*/
public void download(String path, HttpServletResponse response);
}
service 具体实现类
package com.deppon.tps.module.aboutexcel.server.poidlexcel.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlElement;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.sonatype.aether.util.StringUtils;
import com.deppon.foss.framework.exception.BusinessException;
import com.deppon.tps.module.aboutexcel.server.poidlexcel.service.IPOIdwExcelUtilsService;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisFileEntity;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisResponseEntity;
import com.esafenet.dll.FileDlpUtil;
public class POIdwExcelUtlsServiceImpl implements IPOIdwExcelUtilsService {
Logger log = Logger.getLogger(POIdwExcelUtlsServiceImpl.class);
//sheet0的表头开始在第几行
public static int SHEET_START_ROW_NUMBER = 0;
//sheet0的表头开始在第几列
public static int SHEET_START_COLUMN_NUMBER = 0;
public static final int SHEET_LIMIT_ROW_NUMBER = 5000;
/**
* 导入上传文件
* @param entity
* @return
*/
public AnalysisResponseEntity importFileData(AnalysisFileEntity entity){
final File historyfile = entity.getFile();
final File file = decryptFile(historyfile);
entity.setFile(file);
log.info("导入文件开始,"+file.getName());
//对上传的文件进行验证
judgeFile(file);
//解析上传的文件
AnalysisResponseEntity responseentity = analysisFile(entity);
if(historyfile.exists()){
historyfile.delete();
}
if(file.exists()){
file.delete();
}
return responseentity;
}
private File decryptFile(File file){
String filepath = file.getAbsolutePath().substring(0,(int)file.getAbsoluteFile().toString().length()-file.getName().length());
String filename = file.getName().substring(0, file.getName().indexOf("."))+"_UN"+file.getName().substring(file.getName().indexOf("."));
File copyfile = new File(filepath+filename);
boolean copysuccess = FileDlpUtil.decryptFile(file.getAbsolutePath(),filepath+filename);
if(copysuccess){
return copyfile;
}
return null;
}
/**
* 对上传的文件进行验证
* @param file
*/
public void judgeFile(File file){
judegFileNull(file);
//判断后缀是否正确
judegFileSuffix(file);
//判断文件大小
judgeFilelength(file);
//判断内存大小
judegFreeMemory(file);
}
public void judegFileNull(File file){
if(null == file || file.length()<=0){
log.error("文件为空!");
throw new BusinessException("文件为空!");
}
}
/**
* 判断后缀是否正确
* @param file
*/
public void judegFileSuffix(File file){
String filename = file.getName();
String sufffilename = "";
if ((filename == null) ||((filename != null)&& (filename.length() <= 0))) {
log.error("文件为空:"+file.getName()+"后缀名错误");
throw new BusinessException("文件后缀名错误,请重新上传");
}
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
sufffilename = filename.substring(dot+1);
if(!"xls".equalsIgnoreCase(sufffilename) &&
!"xlsx".equalsIgnoreCase(sufffilename)&&
!"xlsm".equalsIgnoreCase(sufffilename)){
log.error("文件:"+file.getName()+"后缀名错误");
throw new BusinessException("文件后缀名错误,请重新上传");
}
}
}
/**
* 判断文件大小
* @param file
*/
public void judgeFilelength(File file){
long length = file.length();
//如果文件的长度大于3M
if(length>3*1024*1024){
log.error("文件:"+file.getName()+"太大,"+file.length());
throw new BusinessException("文件太大,请重新上传");
}
}
/**
* 判断内存大小
* @param file
*/
public void judegFreeMemory(File file){
long freememory = Runtime.getRuntime().freeMemory();
//判断内存是否还有3M可用内存
if(freememory < 3*1024*1024){
log.error("文件:"+file.getName()+"上传时,内存不足");
throw new BusinessException("内存不足,请稍后上传!");
}
}
/**
* 解析上传的文件
* @param entity
*/
public AnalysisResponseEntity analysisFile(AnalysisFileEntity entity){
AnalysisResponseEntity responseentity = new AnalysisResponseEntity();
// 声明一个工作簿
XSSFWorkbook workbook = null;
//表头汉字对应的class字段--数组
Field[] columnClassNamez = null;
//表头汉字--数组
String[] columnNamez = null;
//把excel中正确的数据存储起来
List