package com.telesound.dataimport;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.telesound.dataimport.excel.ExcelProcessor;
import com.telesound.dataimport.excel.XCell;
import com.telesound.dataimport.excel.XRow;
import com.telesound.dataimport.exception.CellContentTooLongException;
import com.telesound.dataimport.exception.ColumnRuleNotFoundException;
import com.telesound.dataimport.exception.CustomProduceMethodNotFoundException;
import com.telesound.dataimport.exception.DataImportException;
import com.telesound.dataimport.exception.DataLengthOutofBoundException;
import com.telesound.dataimport.exception.DataTypeNotMatchException;
import com.telesound.dataimport.exception.DateFormatNotFoundException;
import com.telesound.dataimport.exception.DicItemNotFoundException;
import com.telesound.dataimport.exception.EmptyDefaultValueException;
import com.telesound.dataimport.exception.ErrorDataListException;
import com.telesound.dataimport.exception.ExcelExistSameColumnException;
import com.telesound.dataimport.exception.FromListNotFoundException;
import com.telesound.dataimport.exception.NoValueOfDictionaryElementException;
import com.telesound.dataimport.exception.NullCellValueException;
import com.telesound.dataimport.exception.TableRuleNotFoundException;
import com.telesound.dataimport.util.StringUtil;
import com.telesound.dataimport.util.variable.DataTypeVariable;
import com.telesound.dataimport.util.variable.ProduceMethodVariable;
/**
* Excel导入器,是整个Excel导入的核心类
* @author zhangchaofeng
* @version 1.0
* @date Sep 28, 2011
*/
public class ExcelImporter{
private String uuid;
private String fileName;
private int sheetIndex=-1;
private ExcelProcessorThread p;
private Thread processorThread;
private DataImportTableRule tableRule; // 应用的导入表规则
private boolean writeLog=false;
private String loggerFile;
private static Map<String,ExcelImporter> importerMap=new HashMap<String,ExcelImporter>();
public ExcelImporter(String uuid,String fileName,String tableRuleId,boolean writeLog,String loggerFile) throws Exception {
this.tableRule=DataImportTableRule.getTableRule(tableRuleId);
if(this.tableRule==null){
throw new TableRuleNotFoundException(tableRuleId);
}
this.uuid=uuid;
this.fileName=fileName;
this.writeLog=writeLog;
this.loggerFile=loggerFile;
}
private Thread getProcessorThread(){
return this.processorThread;
}
/**
* 导入所有sheet的数据
* @throws Exception
*/
public void importAllSheetData() throws Exception {
this.sheetIndex=-1;
ExcelProcessorThread thread=new ExcelProcessorThread(fileName);
this.p=thread;
this.processorThread=new Thread(thread);
processorThread.start();
}
/**
* 导入指定sheet的数据
* @param sheetIndex
* @throws Exception
*/
public void importData(int sheetIndex) throws Exception {
this.sheetIndex=sheetIndex;
ExcelProcessorThread thread=new ExcelProcessorThread(fileName);
this.p=thread;
this.processorThread=new Thread(thread);
processorThread.start();
}
/**
* 导入第一个sheet的数据
* @throws Exception
*/
public void importFirstSheetData() throws Exception {
importData(1);
}
/**
* 暂停导入数据
* @throws InterruptedException
*/
public static void suspend(String uuid) throws InterruptedException{
ExcelImporter importer=importerMap.get(uuid);
if(importer!=null){
//importer.processorThread.wait();
importer.p.setRunFlag(false);
}
}
/**
* 继续导入数据
*/
public static void resume(String uuid){
ExcelImporter importer=importerMap.get(uuid);
if(importer!=null){
//importer.processorThread.notifyAll();
importer.p.setRunFlag(true);
}
}
/**
* 停止导入数据
*/
public static void stop(String uuid){
ExcelImporter importer=importerMap.get(uuid);
if(importer!=null){
importer.processorThread.interrupt();
try {
importer.p.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
importerMap.remove(uuid);
}
public static ImportResult getProcess(String uuid){
ExcelImporter importer=importerMap.get(uuid);
if(importer==null){
return null;
}
ImportResult result=new ImportResult();
ExcelProcessorThread pp=importer.p;
result.setCorrentRow(pp.correntRowIndex+"");
result.setFailed(pp.failedCount);
result.setSuccess(pp.successCount);
if(pp.complete){
result.setComplete(true);
result.setCorrentRow("结束");
File excel=new File(importer.fileName);
if(excel.exists()){
excel.delete();
}
}
return result;
}
public static String getProcessMessage(String uuid){
ExcelImporter importer=importerMap.get(uuid);
if(importer==null){
return null;
}
List importMessages=importer.p.importMessages;
StringBuffer message=new StringBuffer("");
for(int i=0;i<importMessages.size();i++){
message.append(importMessages.get(i)+"<br>");
}
return message.toString();
}
private class ExcelProcessorThread extends ExcelProcessor implements Runnable{
private StringBuffer sql1=new StringBuffer("insert into "+tableRule.getTableName()+" (");
private StringBuffer sql2=new StringBuffer(") values (");
private String sql3=")";
private int correntRowIndex=0;
private int correntColumnIndex=0;
private int successCount=0;
private int failedCount=0;
private boolean runFlag=true;
private boolean complete=false;
private Map<Integer,String> headerNames;
private Map<String,String> columnValues;
private Map<String,DataImportColumnRule> pkColumnRules;
private List importMessages=new ArrayList();
public ExcelProcessorThread(String fileName) throws Exception {
super(fileName);
this.headerNames=new HashMap<Integer,String>();
this.columnValues=new HashMap<String,String>();
this.pkColumnRules=new HashMap<String,DataImportColumnRule>();
}
public void setRunFlag(boolean b){
this.runFlag=b;
}
/**
* 导入策略判断
* @param value
* @param columnRule
* @throws NullCellValueException
* @throws DicItemNotFoundException
* @throws NoValueOfDictionaryElementException
* @throws CellContentTooLongException
* @throws DataTypeNotMatchException
* @throws EmptyDefaultValueException
* @throws DataLengthOutofBoundException
* @throws DateFormatNotFoundException
* @throws CustomProduceMethodNotFoundException
* @throws FromListNotFoundException
* @throws ErrorDataListException
*/
private void step(String value,DataImportColumnRule columnRule) throws DataTypeNotMatchException, CellContentTooLongException, NoValueOfDictionaryElementException, DicItemNotFoundException, NullCellValueException, EmptyDefaultValueException, DataLengthOutofBoundException, DateFormatNotFoundException, CustomProduceMethodNotFoundException, FromListNotFoundException, ErrorDataListException{
String columnName=columnRule.getColumnName();
String dataType=columnRule.getDataType();
int maxLength=columnRule.getMaxLength();
String method=columnRule.getProduceMethod();
String produceMethodValue=columnRule.getProduceMethodValue();
if(value!=null){
value=value.trim();
}
//默认值时的处理策略
if(ProduceMethodVariable.DEFAULT_VALUE.equals(method)){
String defaultValue=columnRule.getProduceMethodValue().trim();
if(!StringUtil.hasValue(defaultValue)){
if(columnRule.getRequired()){
throw new EmptyDefaultValueException("列规则生成策略为默认值,但实际默认值为空,columnName:"+columnName+","+correntRowIndex+"行,"+
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
com.rar (40个子文件)
com
telesound
dataimport
FileUploader.java 3KB
DataImportColumnRule.java 9KB
ImportLogger.java 609B
producor
UuidPKProducor.java 222B
PkProducor.java 115B
CameraIdProducor.java 1KB
DataImportTableRule.java 6KB
ExcelImporter.java 25KB
exception
DicItemNotFoundException.java 268B
ErrorDataListException.java 265B
TableRuleNotFoundException.java 305B
NoValueOfDictionaryElementException.java 290B
NullCellValueException.java 264B
DataLengthOutofBoundException.java 278B
ExcelExistSameColumnException.java 279B
CellContentTooLongException.java 274B
EmptyDefaultValueException.java 292B
DataTypeNotMatchException.java 269B
FromListNotFoundException.java 271B
DateFormatNotFoundException.java 274B
CustomProduceMethodNotFoundException.java 292B
ColumnRuleNotFoundException.java 253B
DataImportException.java 362B
util
variable
ProduceMethodVariable.java 1KB
BooleanVariable.java 1KB
VariableItem.java 364B
DataTypeVariable.java 1KB
StringUtil.java 787B
MD5Util.java 1KB
DateUtil.java 2KB
BeanUtil.java 4KB
excel
TestReadExcel.java 1KB
ExcelProcessor.java 2KB
ExcelRowProcessor.java 409B
XCell.java 641B
Excel2003RowProcessor.java 10KB
Excel2007RowProcessor.java 6KB
XRow.java 641B
DBAccess.java 23KB
ImportResult.java 1KB
共 40 条
- 1
shenshenjp
- 粉丝: 48
- 资源: 532
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页