This commit is contained in:
Kane Wang 2021-06-21 17:45:41 +08:00
parent 10c07f669c
commit 83c26ecec9
1 changed files with 119 additions and 95 deletions

View File

@ -1,19 +1,15 @@
package com.cpic.telsale.DataImport;
/** /**
* Created by Kane on 2017/3/28. * Created by Kane on 2017/3/28.
*/ */
package com.cpic.telsale.DataImport;
import java.io.*; import java.io.*;
import java.sql.*; import java.sql.*;
import java.text.ParseException; import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.poi.hssf.*;
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType; import com.cpic.telsale.TelsalePolicyRecord;
import com.cpic.util.staff.*; import com.cpic.util.staff.*;
public class DataImport public class DataImport
@ -22,6 +18,58 @@ public class DataImport
{ {
} }
/**
* @Description: 从xlsx文件读取电销转介绍保单记录保存在一个ArrayList中并返回
* @Param: fileName xlsx文件的路径
* @return: java.util.ArrayList<com.cpic.telsale.TelsalePolicyRecord>
* @Author: 王炜
* @Date: 2021/6/21
*/
public static ArrayList<TelsalePolicyRecord> readTelsalePolicyDataFromXlsx(String fileName, int sheetIndex) throws
IOException,
SQLException,
XlsFileFormatException
{
ArrayList<TelsalePolicyRecord> policyList = new ArrayList<TelsalePolicyRecord>();
TelsalePolicyRecord record = null;
FileInputStream xlsxFile = null;
HSSFWorkbook workBook = null;
HSSFSheet sheet = null;
HSSFRow row = null;
int rowIndex = 0;
int rowCount = 0;
try {
xlsxFile = new FileInputStream(fileName);
workBook = new HSSFWorkbook(xlsxFile);
sheet = workBook.getSheetAt(sheetIndex);
rowIndex = sheet.getFirstRowNum() + 1;
rowCount = sheet.getLastRowNum();
row = sheet.getRow(rowIndex);
while (rowIndex <= rowCount) {
//用第一个单元格判断是不是空行如果cell为null视为空行
HSSFCell cell = row.getCell(0);
if (cell == null) {
rowIndex++;
continue;
}
String policyNo = cell.getStringCellValue();
String operatorCode = getStringValueFromHSSFCell(row.getCell(1));
}
} catch (IOException error) {
}
return policyList;
}
/** /**
* 读取xls表格插入数据要么全部成功要么全部失败 * 读取xls表格插入数据要么全部成功要么全部失败
* *
@ -34,46 +82,44 @@ public class DataImport
* @throws SQLException 数据库异常 * @throws SQLException 数据库异常
* @throws XlsFileFormatException xls文件格式错误异常 * @throws XlsFileFormatException xls文件格式错误异常
*/ */
public static String importTelsalePolicyDataFromXlsx( String fileName, public static String importTelsalePolicyDataFromXlsx(String fileName,
String telsalePolicyOperatorListTableName, String telsalePolicyOperatorListTableName,
String informixURL ) throws String informixURL) throws
IOException, IOException,
ClassNotFoundException, ClassNotFoundException,
SQLException, SQLException,
XlsFileFormatException XlsFileFormatException
{ {
String importResult = ""; String importResult = "";
String importMessage = null; String importMessage = null;
int rowIndex = 1; //excel文件行索引 int rowIndex = 1; //excel文件行索引
int rowCount = -1; int rowCount = -1;
int successCount = 0; //成功写入的行计数 int successCount = 0; //成功写入的行计数
int failCount = 0; //写入失败的行计数 int failCount = 0; //写入失败的行计数
//excel相关 //excel相关
FileInputStream xlsFileIn = null; FileInputStream xlsFileIn = null;
HSSFWorkbook xlsFile = null; HSSFWorkbook xlsFile = null;
HSSFSheet sheet = null; HSSFSheet sheet = null;
HSSFRow row = null; HSSFRow row = null;
try try {
{ xlsFileIn = new FileInputStream(fileName);
xlsFileIn = new FileInputStream( fileName ); xlsFile = new HSSFWorkbook(xlsFileIn);
xlsFile = new HSSFWorkbook( xlsFileIn ); sheet = xlsFile.getSheetAt(0); //第一个sheet
sheet = xlsFile.getSheetAt( 0 ); //第一个sheet rowIndex = sheet.getFirstRowNum() + 1;
rowIndex = sheet.getFirstRowNum() + 1; rowCount = sheet.getLastRowNum();
rowCount = sheet.getLastRowNum(); row = sheet.getRow(rowIndex);
row = sheet.getRow( rowIndex ); } catch (IOException error) //有文件格式错误的可能
}
catch( IOException error ) //有文件格式错误的可能
{ {
xlsFile.close(); xlsFile.close();
xlsFileIn.close(); xlsFileIn.close();
throw new XlsFileFormatException( "文件格式错误请检查是否xls格式文件。" ); throw new XlsFileFormatException("文件格式错误请检查是否xls格式文件。");
} }
//informix相关 //informix相关
Class.forName( "com.informix.jdbc.IfxDriver" ); Class.forName("com.informix.jdbc.IfxDriver");
String ifxSql = String ifxSql =
"INSERT INTO " + telsalePolicyOperatorListTableName + " \n" + "INSERT INTO " + telsalePolicyOperatorListTableName + " \n" +
" (bdh,\n" + " (bdh,\n" +
@ -107,53 +153,49 @@ public class DataImport
" 1,\n" + " 1,\n" +
" '800',\n" + " '800',\n" +
" today)"; " today)";
Connection ifxConn = DriverManager.getConnection( informixURL ); Connection ifxConn = DriverManager.getConnection(informixURL);
PreparedStatement ifxStmt = ifxConn.prepareStatement( ifxSql ); PreparedStatement ifxStmt = ifxConn.prepareStatement(ifxSql);
ifxConn.setAutoCommit( false );//关闭自动提交整个表格要么全部成写入要么全部失败 ifxConn.setAutoCommit(false);//关闭自动提交整个表格要么全部成写入要么全部失败
//数据相关 //数据相关
String policyNo = null; String policyNo = null;
String operatorCode = null; String operatorCode = null;
StaffInfo staff = null; StaffInfo staff = null;
boolean isSuccess = true; //标志位用来指示在保存过程中是否出现错误 boolean isSuccess = true; //标志位用来指示在保存过程中是否出现错误
while ( rowIndex <= rowCount ) while (rowIndex <= rowCount) {
{
//用来判断是不是空行 //用来判断是不是空行
HSSFCell cell = row.getCell( 0 ); HSSFCell cell = row.getCell(0);
if ( cell == null) if (cell == null) {
{
rowIndex++; rowIndex++;
continue; continue;
} }
policyNo = row.getCell( 0 ).getStringCellValue().trim(); policyNo = row.getCell(0).getStringCellValue().trim();
operatorCode = getStringValueFromHSSFCell( row.getCell( 1 ) ); operatorCode = getStringValueFromHSSFCell(row.getCell(1));
//判断一下是不是空的单元格 //判断一下是不是空的单元格
if ( policyNo.length() == 0 || operatorCode.length() == 0 ) if (policyNo.length() == 0 || operatorCode.length() == 0) {
{
importResult = importResult + "" + rowIndex + "行数据为空。<br>"; importResult = importResult + "" + rowIndex + "行数据为空。<br>";
//isSuccess = false; //isSuccess = false;
} }
else //不是空单元格进行处理 else //不是空单元格进行处理
{ {
try try {
{
//取得经办人信息 //取得经办人信息
staff = new StaffInfo( operatorCode ); staff = new StaffInfo(operatorCode);
//写入informix //写入informix
ifxStmt.setString( 1, policyNo ); ifxStmt.setString(1, policyNo);
ifxStmt.setString( 2, staff.getStaffCode() ); ifxStmt.setString(2, staff.getStaffCode());
ifxStmt.setString( 3, staff.getStaffName() ); ifxStmt.setString(3, staff.getStaffName());
ifxStmt.setString( 4, staff.getSectionOfficeCode() ); ifxStmt.setString(4, staff.getSectionOfficeCode());
ifxStmt.setString( 5, staff.getSectionOfficeName() ); ifxStmt.setString(5, staff.getSectionOfficeName());
ifxStmt.setString( 6, staff.getDepartmentCode() ); ifxStmt.setString(6, staff.getDepartmentCode());
ifxStmt.setString( 7, staff.getDepartmentName() ); ifxStmt.setString(7, staff.getDepartmentName());
//ifxStmt.setString( 8, getStringValueFromHSSFCell( row.getCell( 6 ) ) ); //ifxStmt.setString( 8, getStringValueFromHSSFCell( row.getCell( 6 ) ) );
//ifxStmt.setString( 9, getStringValueFromHSSFCell( row.getCell( 3 ) ) ); //ifxStmt.setString( 9, getStringValueFromHSSFCell( row.getCell( 3 ) ) );
//ifxStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) ); //ifxStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) );
@ -164,9 +206,7 @@ public class DataImport
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",转介绍信息入成功!<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",转介绍信息入成功!<br>";
successCount++; successCount++;
} } catch (StaffCodeNotExistException error) {
catch( StaffCodeNotExistException error )
{
//经办人不存在 //经办人不存在
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + "不存在。<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + "不存在。<br>";
@ -175,9 +215,7 @@ public class DataImport
//计数 //计数
failCount++; failCount++;
} } catch (OracleConnectionException error) {
catch( OracleConnectionException error )
{
//查询工号失败 //查询工号失败
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + ",查询工号失败。<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + ",查询工号失败。<br>";
@ -186,19 +224,17 @@ public class DataImport
//计数 //计数
failCount++; failCount++;
} } catch (SQLException error) {
catch( SQLException error )
{
int errorCode = error.getErrorCode(); int errorCode = error.getErrorCode();
switch ( errorCode ) switch (errorCode) {
{
case -268: //保单号重复 case -268: //保单号重复
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",已录入过转介绍信息!" + error.getCause() + "。<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",已录入过转介绍信息!" + error.getCause() + "。<br>";
break; break;
default: //写入错误 default: //写入错误
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getCause() + "。<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error
.getCause() + "。<br>";
break; break;
} }
@ -207,9 +243,7 @@ public class DataImport
//计数 //计数
failCount++; failCount++;
} } catch (Exception error) {
catch( Exception error )
{
//写入错误 //写入错误
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>"; importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>";
@ -218,15 +252,13 @@ public class DataImport
//计数 //计数
failCount++; failCount++;
} } finally {
finally
{
ifxConn.commit(); ifxConn.commit();
} }
} }
rowIndex++; rowIndex++;
row = sheet.getRow( rowIndex ); row = sheet.getRow(rowIndex);
} }
//判断标志位如果为false则说明在保存过程中出现了错误放弃所有过程进行回滚 //判断标志位如果为false则说明在保存过程中出现了错误放弃所有过程进行回滚
@ -266,45 +298,37 @@ public class DataImport
return importMessage; return importMessage;
} }
private static String getStringValueFromHSSFCell( HSSFCell cell ) private static String getStringValueFromHSSFCell(HSSFCell cell)
{ {
String value = null; String value = null;
if ( cell == null ) if (cell == null) {
{
return ""; return "";
} }
switch ( cell.getCellType() ) switch (cell.getCellType()) {
{ case HSSFCell.CELL_TYPE_NUMERIC: {
case HSSFCell.CELL_TYPE_NUMERIC:
{
//判断是不是整数 //判断是不是整数
double dValue = cell.getNumericCellValue(); double dValue = cell.getNumericCellValue();
if ( dValue == ( int )dValue ) if (dValue == (int) dValue) {
{ value = String.valueOf((int) dValue);
value = String.valueOf( ( int )dValue );
} }
else else {
{ value = String.valueOf(dValue);
value = String.valueOf( dValue );
} }
break; break;
} }
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING: {
{
value = cell.getStringCellValue(); value = cell.getStringCellValue();
break; break;
} }
case HSSFCell.CELL_TYPE_BLANK: case HSSFCell.CELL_TYPE_BLANK: {
{
value = ""; value = "";
break; break;
} }
default: default: {
{
value = ""; value = "";
break; break;
} }