每两列读取Excel文件

chen7788 2011-11-26 01:49:07
如题:

现在有个需求,按照每两列读取,将读取到的数据转成属性文件?

Excel数据格式:

错误码 描述 异常原因 解决办法
490080016 对用户名和密码加密时出错 对用户名和密码加密时出错 加密用户名和密码时出错
490080017 域已经存在 域已经被接入 设备不同的域名或者接入不同的域
490080018 设备已经被接入 这个设备已经被接入
490080019 重复浮动IP、主机IP和备机IP重复 设置不同的浮动IP、主机IP和备机IP


Properties数据格式:

490080016_desc = 对用户名和密码加密时出错。
490080016_detail = 对用户名和密码加密时出错。
490080016_reason = 无法对用户名和密码进行加密。
490080016_advice = 加密用户名和密码时出错。
...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kouyiSC 2011-11-28
  • 打赏
  • 举报
回复

两种方式:

/**
* 读取xls文件
*
* @throws Exception
*/
public static void readXlsfileMap(File filePath) {

try {
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
Sheet[] sheets = rwb.getSheets();// 获得当前Excel表共有几个sheet
int p = sheets.length;
for (int w = 0; w < p; w++) {
// 将每个sheet中的内容全部读取出来
Sheet rs = rwb.getSheet(w);
int rows = rs.getRows();
// 行循环,Excel的行列是从(0,0)开始的,下列for循环从第二行第一列开始。。。
for (int d = 1; d < rows; d++) {
Cell b00 = rs.getCell(0, d);
String strb = b00.getContents(); //每行第一列数据
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

/**
* 读取xls文件
*
* @throws Exception
*/
public static void readXlsfile2(InputStream is){

POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
HSSFSheet sheet = null;
HSSFRow row = null;
try{
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
}catch (Exception e) {
System.out.println(e.getMessage());
}
for (int w = 0; w < 1; w++) {
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
String col0 = row.getCell((short)0).toString()); // 如:col0为每行第1列数据。。
}
}
}
chen7788 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ruogannian 的回复:]

去apache下poi的jar包,共三个
poi.jar
poi-ooxml.jar
poi-ooxml-schemas.jar
[/Quote]

switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
x=cell.getStringCellValue().trim();
break;
default:
break;
}

没有匹配项。。。。

HSSFCell cell = row.getCell(c);
这行报错, row.getCell(Short argument), int 类型报错。
若干年 2011-11-26
  • 打赏
  • 举报
回复
去apache下poi的jar包,共三个
poi.jar
poi-ooxml.jar
poi-ooxml-schemas.jar
若干年 2011-11-26
  • 打赏
  • 举报
回复
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
若干年 2011-11-26
  • 打赏
  • 举报
回复

FileInputStream fis = null;
try {
fis = new FileInputStream(filename) ;
POIFSFileSystem fs = new POIFSFileSystem(fis);

if(fs!=null){
list = new ArrayList<CpVehicleOnline>() ;
HSSFWorkbook wb = new HSSFWorkbook(fs);
for (int k = 0; k < wb.getNumberOfSheets(); k++) {

HSSFSheet sheet = wb.getSheetAt(k);

int rows = sheet.getPhysicalNumberOfRows();

for (int r = 1; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row == null) {
continue;
}

int cells = row.getPhysicalNumberOfCells();

for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell == null) {
continue;
}

switch (c) {
case 0:
String x = "" ;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
x=cell.getStringCellValue().trim();
break;
default:
break;
}
break;
case 1:

break;
break;
}
}

}
}
}

} catch (Exception e) {
log.error("", e);
}finally{
if(fis!=null){
try {
fis.close();
} catch (Exception e2) {
}
}
}
chen7788 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 leehomwong 的回复:]
...
[/Quote]
哥,创建properties文件OK 啊,,问题是怎么从Excel文件中获取到
String parameterName,String parameterValue呢?
24K純帥 2011-11-26
  • 打赏
  • 举报
回复
POI读取EXCEL..
parameterName就是错误码+四个后缀,parameterValue就是后面三列内容
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
Print.print("ConfigInfoError","Visit "+filePath+" for updating "+parameterName+" value error");
}
chen7788 2011-11-26
  • 打赏
  • 举报
回复
提供个思路。。。。。。。不要求提供代码。。。。
感激ing...

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧