//总行数
private int totalRows = 0;
//总条数
private int totalCells = 0;
//错误信息接收器
private String errorMsg;
//构造方法
public ReceiveMailNew2018_5(){}
//获取总行数
public int getTotalRows() { return totalRows;}
//获取总列数
public int getTotalCells() { return totalCells;}
//获取错误信息
public String getErrorInfo() { return errorMsg; }
List<BankBill> bbList = new ArrayList<BankBill>();
public void analysizeBankExcel(String path,InputStream ism) throws IOException{
Workbook wb = null;
if(isExcel2003(path)){
wb = new HSSFWorkbook(ism);
}
else{//当excel是2007时
wb = new XSSFWorkbook(ism);
}
ism.close();
//得到第一个shell
Sheet sheet=wb.getSheetAt(0);
//得到Excel的行数
this.totalRows=sheet.getPhysicalNumberOfRows();
//得到Excel的列数(前提是有行数)
if(totalRows>=1 && sheet.getRow(0) != null){
this.totalCells=sheet.getRow(2).getPhysicalNumberOfCells();
}
System.out.println(totalCells);
Cell cell = null;
// cell.setCellType(Cell.CELL_TYPE_STRING);
// String value = sheet.getRow(1).getCell(1).getStringCellValue();
// sheet.addMergedRegion(new CellRangeAddress( int startRow,int endRow, int startCol, int endCol));// 设置单元格合并
//获取表中合并单元格的数据
/* int sheetMergeCount = sheet.getNumMergedRegions();
System.out.println(sheetMergeCount);
for(int i = 0 ; i < sheetMergeCount ; i++){
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
System.out.println(firstColumn+","+lastColumn+","+firstRow+","+lastRow);
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
System.out.println(getCellValue(fCell));*/
// return getCellValue(fCell) ;
// if(row >= firstRow && row <= lastRow){
//
// if(column >= firstColumn && column <= lastColumn){
// Row fRow = sheet.getRow(firstRow);
// Cell fCell = fRow.getCell(firstColumn);
// return getCellValue(fCell) ;
// }
// }
// }
//获取循环节点的数据
//循环Excel行数,从第3行开始
BankBill bankBill =null;
DecimalFormat df = new DecimalFormat("######0.00");
String money = null;
int rowNum = 0;
int add = 0;
int NoneAdd = 0;
String clearDateStr = null; //清算时间
List<String> lsStrs = new ArrayList<String>();
for(int r=4;r<totalRows-2;r++){
rowNum++;
Row row = sheet.getRow(r);
if (row == null) continue;
bankBill = new BankBill();
try {
//
bankBill.setMerchantNo("XXXXX50450494");
bankBill.setMerchant("XXXXX集团股份有限公司");
bankBill.setReceiveOrgan("银联商务XXX公司");
//
//1清算日期
cell = row.getCell(0);
bankBill.setClearDate(getCellValue(cell));
clearDateStr = getCellValue(cell);
// System.out.print(getCellValue(cell)+",");
//2生成日期
/*cell = row.getCell(1);
System.out.print(excelDate(cell)+",");*/
// System.out.print(generateDate()+",");
bankBill.setGenerateDate(generateDate());
//3交易日期时间
cell = row.getCell(2);
// System.out.print(excelTime(cell)+",");
bankBill.setTranDateTime(excelTime(cell));
/*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date d = dateFormat.parse(excelTime(cell));
System.err.println(d.toString());*/
//4终端号
cell = row.getCell(3);
bankBill.setTerminalNo(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//5交易金额
cell = row.getCell(4);
bankBill.setTranAmount(Double.parseDouble(getCellValue(cell)));
// System.out.print(getCellValue(cell)+",");
//6清算金额
cell = row.getCell(5);
bankBill.setClearAmount(Double.parseDouble(getCellValue(cell)));
// System.out.print(getCellValue(cell)+",");
//7手续费
cell = row.getCell(6);
bankBill.setFee(Double.parseDouble(getCellValue(cell)));
// System.out.print(getCellValue(cell)+",");
//8流水号(跟踪号)
cell = row.getCell(7);
bankBill.setSerialNo(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//9交易类型
cell = row.getCell(8);
bankBill.setTradeType(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//10参考号
cell = row.getCell(9);
bankBill.setRefNo(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//11卡号
cell = row.getCell(10);
bankBill.setCardNo(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//12卡类型
cell = row.getCell(11);
bankBill.setCardType(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//13发卡行
cell = row.getCell(12);
bankBill.setCardBank(getCellValue(cell));
// System.out.print(getCellValue(cell)+",");
//14流水序号
String terminalNo = bankBill.getTerminalNo();
// if ("01096488".equals(terminalNo) || "01096478".equals(terminalNo) || "01096477".equals(terminalNo)) {
if ("CS000001".equals(terminalNo)) {
add++;
lsStrs.add(clearDateStr);
bbList.add(bankBill);
// System.out.println(":::"+bankBill);
}else {
NoneAdd++;
// System.err.println(":::"+bankBill);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// System.out.println("add:::"+add);
// System.out.println("NoneAdd:::"+NoneAdd);
// System.out.println("rowNum:"+rowNum);
List<String> lsNos = new ArrayList<String>();
lsNos = getLsNos(lsStrs);
if (bbList.size()!= 0 && lsNos.size()!=0 && lsNos.size() == bbList.size()) {
for (int i = 0; i < lsNos.size(); i++) {
bbList.get(i).setLsNo(lsNos.get(i));
// System.err.println("bankBill:::"+bbList.get(i).toString());
}
// System.out.println();
} else {
String flag = lsNos.size() == bbList.size()?"true":"false";
System.out.println("lsNos.size() == bbList.size() : "+flag);
}
}
public List<String> getLsNos(List<String> lsStrs) {
int num = 0;
String lsNo = null;
List<String> lsNos = new ArrayList<String>();
if (lsStrs.size()>=1) {
//将一次上传的lsNo号码定义的结束掉以end为准
lsNo = lsStrs.get(0)+"000"+"END";
lsNos.add(lsNo);
/*
*其他的循环给定lsNo号码
*/
for (int i = 1; i < lsStrs.size(); i++) {
num++;
if (lsStrs.get(i).equals(lsStrs.get(i-1))){
lsNo = lsStrs.get(i)+"00000"+num;
lsNos.add(lsNo);
} else {
num = 1;
lsNo = lsStrs.get(i)+"00000"+1;
lsNos.add(lsNo);
}
}
}
String subStr = lsNos.get(0).substring(0,8);
int subNum = 0;
for (int i = 0; i < lsNos.size(); i++) {
if (subStr.equals(lsNos.get(i).substring(0, 8))) {
subNum++;
lsNos.set(i, lsStrs.get(i)+"00000"+subNum);
}
}
return lsNos;
};
public static void main(String[] args){
ReceiveMailNew2018_5 rm = new ReceiveMailNew2018_5();
InputStream ism;
String path=null;
try {
String newPath = "E:\\2018workFolderShun\\pos\\pos18";
ism = new FileInputStream(new File(newPath+"\\2017.10商户清算明细20180117101401test.xls"));
path =newPath+"\\2017.10商户清算明细20180117101401test.xls";
rm.analysizeBankExcel(path,ism);
rm.analysizeWZExcel(path,ism);
rm.conn();
// rm.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author SHUN
* 生成系统日期,并将其格式化返回字符串
* @return
*/
public String generateDate(){
String guarantee_date = null;
Date generateDate = new Date();
DateFormat formater = new SimpleDateFormat("yyyyMMdd");
guarantee_date = formater.format(generateDate);
return guarantee_date;
}
/**
* @author SHUN
* @param cell import org.apache.poi.ss.usermodel.Cell;
* 关于如何将Excel表格中的时间字符串的数字格式 转换成 格式化的时间字符串
* @return
*/
public String excelTime(Cell cell){
String guarantee_time = null;
if(DateUtil.isCellDateFormatted(cell)){
//用于转化为日期格式
Date d = cell.getDateCellValue();
// System.err.println(d.toString());
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat formater = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
guarantee_time = formater.format(d);
}
return guarantee_time;
}
//@描述:是否是2003的excel,返回true是2003
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
//@描述:是否是2007的excel,返回true是2007
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
/**
* 获取单元格的值
* @param cell
* @return
*/
public String getCellValue(Cell cell){
if(cell == null) return "";
if(cell.getCellType() == Cell.CELL_TYPE_STRING){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
return cell.getCellFormula() ;
}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}
return "";
}
/**
* 将带空格的字符串拆分成字符串数组
*
* @author bushy
* @createDate 2013-3-25
* @param strBlank
* @return
*/
private String[] splitBlank2Array(String strBlank) {
if (StringUtils.isNotBlank(strBlank)) {
return strBlank.split("\\s{1,}");
}
return null;
}
建议所用jar包
Excel 表格数据实例