JAVA读取EXCEL文件号段解析

EXCEL文件  有地区的号段  读取excel文件并解析它.

EXCEL 的格式大致如下:


1330+551 = 1330551,目的是为了得到1330551.

代码如下:



import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


import org.apache.poi.ss.usermodel.Cell;
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.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.DateUtil;




public class AnalyseMobile {


static String PATH = "E://area.xls";//E://area.xls
static String[][] startWithMobile = new String[8][10];
static String[][][] endWithMobile = new String[8][17][11];


public static void getAllByExcel(String filepath) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
// 同时支持Excel 2003、2007
File excelFile = new File(filepath); // 创建文件对象
FileInputStream is = new FileInputStream(excelFile); // 文件流
Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel
// 2003/2007/2010
// 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); // Sheet的数量
// 遍历每个Sheet


for (int s = 0; s < sheetCount; s++) {
// System.out.println("当前sheet页码:" + s);
Sheet sheet = workbook.getSheetAt(s);
int rowCount = sheet.getPhysicalNumberOfRows(); // 获取总行数
// 遍历每一行
for (int r = 0; r < rowCount; r++) {
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); // 获取总列数
// 遍历每一列
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c);
int cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case Cell.CELL_TYPE_STRING: // 文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: // 数字、日期
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); // 日期型
} else {
cellValue = String.valueOf((int) cell
.getNumericCellValue()); // 数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: // 布尔型
cellValue = String.valueOf(cell
.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK: // 空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: // 错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: // 公式
cellValue = "错误";
break;
default:
cellValue = "错误";
}
if (r == 1 && c > 2 && c < 13) {
startWithMobile[s][c - 3] = cellValue;
}
if (r > 1 && r < 19 && c > 2 && c < 14) {
/*
* System.out.print("第" + r + "行,第" + c + "列" +
* cellValue + "\t");
*/
endWithMobile[s][r - 2][c - 3] = cellValue;
}
}


}


}
} catch (Exception e) {
e.printStackTrace();
}


}


public static List<String> getAreaNumber() {


getAllByExcel(PATH);
List<String> finalist = new ArrayList<String>();
List<String> list = new ArrayList<String>();
for (int a = 0; a < endWithMobile.length; a++) {
for (int b = 0; b < endWithMobile[a].length - 1; b++) {
for (int c = 0; c < endWithMobile[a][b].length - 1; c++) {
String[] endPart = endWithMobile[a][b][c].split("、");
for (int i = 0; i < endPart.length; i++) {


String[] endPartsp = endPart[i].split("-");
if (endPartsp.length == 1) {
String com = startWithMobile[a][c] + endPartsp[0];
/*if(b==0){
list.add(com+"--->"+"合肥");
}*/
switch (b) {
case 0:
list.add(com+"_"+"合肥");
break;
case 1:
list.add(com+"_"+"池州");
break;
case 2:
list.add(com+"_"+"毫州");
break;
case 3:
list.add(com+"_"+"阜阳");
break;
case 4:
list.add(com+"_"+"宿州");
break;
case 5:
list.add(com+"_"+"安庆");
break;
case 6:
list.add(com+"_"+"马鞍山");
break;
case 7:
list.add(com+"_"+"淮南");
break;
case 8:
list.add(com+"_"+"芜湖");
break;
case 9:
list.add(com+"_"+"蚌埠");
break;
case 10:
list.add(com+"_"+"滁州");
break;
case 11:
list.add(com+"_"+"巢湖");
break;
case 12:
list.add(com+"_"+"六安");
break;
case 13:
list.add(com+"_"+"宣城");
break;
case 14:
list.add(com+"_"+"铜陵");
break;
case 15:
list.add(com+"_"+"淮北");
break;
case 16:
list.add(com+"_"+"黄山");
break;
default:
break;
}


} else {
int diffnum = Integer.parseInt(endPartsp[1])
- Integer.parseInt(endPartsp[0]);


for (int j = 0; j < diffnum + 1; j++) {
int num = Integer.parseInt(endPartsp[0]) + j;
String numStr = "";
if(num<10){
numStr = "00"+num;
}else if(num<100&&num>=10){
numStr = "0"+num;
} else{
numStr = ""+num;
}
String com = startWithMobile[a][c] + numStr;

//list.add(com+"--->"+b+c);
switch (b) {
case 0:
list.add(com+"_"+"合肥");
break;
case 1:
list.add(com+"_"+"池州");
break;
case 2:
list.add(com+"_"+"毫州");
break;
case 3:
list.add(com+"_"+"阜阳");
break;
case 4:
list.add(com+"_"+"宿州");
break;
case 5:
list.add(com+"_"+"安庆");
break;
case 6:
list.add(com+"_"+"马鞍山");
break;
case 7:
list.add(com+"_"+"淮南");
break;
case 8:
list.add(com+"_"+"芜湖");
break;
case 9:
list.add(com+"_"+"蚌埠");
break;
case 10:
list.add(com+"_"+"滁州");
break;
case 11:
list.add(com+"_"+"巢湖");
break;
case 12:
list.add(com+"_"+"六安");
break;
case 13:
list.add(com+"_"+"宣城");
break;
case 14:
list.add(com+"_"+"铜陵");
break;
case 15:
list.add(com+"_"+"淮北");
break;
case 16:
list.add(com+"_"+"黄山");
break;
default:
break;
}

}
}
}


}
}
}
/*
* System.out.println(startWithMobile[1].length 10 );
* System.out.println(endWithMobile.length 8 );
* System.out.println(endWithMobile[1].length-1 16 );
* System.out.println(endWithMobile[1][2].length-1 10);
*/
/*
* String [] endPart = endWithMobile[0][0][3].split("、"); for (int i =
* 0; i < endPart.length; i++) {

* String [] endPartsp = endPart[i].split("-"); if(endPartsp.length==1){
* list.add(startWithMobile[0][3]+endPartsp[0]); }else{ int diffnum =
* Integer.parseInt(endPartsp[1]) - Integer.parseInt(endPartsp[0]);

* for (int j = 0; j < diffnum+1; j++) { int num =
* Integer.parseInt(endPartsp[0])+j;
* list.add(startWithMobile[0][3]+num); } } }
*/
for (int i = 0; i < list.size(); i++) {
if (list.get(i).length() > 9) {
finalist.add(list.get(i));
}
}
for (int i = 0; i < finalist.size(); i++) {
finalist.get(i);
}


return finalist;
}


public static void main(String[] args) {


List<String> list = getAreaNumber();
for (int i = 0; i < list.size(); i++) {
String citytoal[] = list.get(i).split("_");
System.out.println("号段:"+citytoal[0]+"城市:"+citytoal[1]);
}


}


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值