您的位置:首页 > 编程语言 > Java开发

java读取Excel指定格式的数据

2015-08-28 18:33 429 查看
Excel Version 2003

JDK 1.6

帮朋友做的,弄完后留个印象



代码:有点乱,只为实现功能,没有过多修改调试

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class GetExcelInfo {
Boolean flag =true;
Boolean first =true;
InputStream is =null;
Workbook wb =null;
Sheet sheet=null;

public static void main(String[] args) {
GetExcelInfo obj = new GetExcelInfo();
File file = new File("E:/连接信息.xls");
obj.readExcel(file);
}
public void outData(int n){

System.out.print("");

}

// 去读Excel的方法readExcel,该方法的入口参数为一个File对象
public void readExcel(File file) {
try {
// 创建输入流,读取Excel
is = new FileInputStream(file.getAbsolutePath());
wb = Workbook.getWorkbook(is);
// 每个页签创建一个Sheet对象
sheet = wb.getSheet(0);
// sheet.getRows()返回该页的总行数
// sheet.getColumns()返回该页的总列数
for (int i = 2; i < sheet.getRows(); i++) {
if(first==false){
System.out.println();
}
for (int j = 0; j < sheet.getColumns(); j++) {
if(sheet.getCell(j, 0)!=null && flag ==true){
flag =false;
//	                    		if(first==true){
System.out.print(sheet.getCell(j, 0).getContents()+"→→");
System.out.print(sheet.getCell(j, 1).getContents()+":");
//	                    		}else{
System.out.print(sheet.getCell(j, i).getContents()+",");
//	                    		}
}else if(sheet.getCell(j, 1)!=null&&sheet.getCell(j,0).getContents().equals("")){
//	                    		if(first==true){
System.out.print(sheet.getCell(j, 1).getContents()+":");
//	                    		}else{
System.out.print(sheet.getCell(j, i).getContents()+",");
//	                    		}
if(j+1!=sheet.getColumns()&&!sheet.getCell(j+1, 0).getContents().equals("")){
flag =true;
//	                    			System.out.println();
}
}
}
System.out.println();
flag =true;
}
//	                }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


输出结果:

资源基本信息→→ID:1,名称:192.168.1.1,版本:CiscoIOS1.0,IP:192.168.1.1,连接信息→→连接ID:1,传输协议:udp,团体名:public,端口号:161,版本号:1.00 ,类型:,端口:202,联系人→→负责人:大毛,联系电话:,
资源基本信息→→ID:2,名称:192.168.1.2,版本:CiscoIOS1.1,IP:192.168.1.2,连接信息→→连接ID:,传输协议:Tcp,团体名:cisco,端口号:161,版本号:2.00 ,类型:,端口:2336,联系人→→负责人:二毛,联系电话:,
资源基本信息→→ID:3,名称:192.168.1.3,版本:CiscoIOS1.2,IP:192.168.1.3,连接信息→→连接ID:3,传输协议:ftp,团体名:Red,端口号:161,版本号:3.00 ,类型:,端口:,联系人→→负责人:三毛,联系电话:,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: