您的位置:首页 > 其它

poi 中读取和写入excel 方法

2013-12-07 20:39 288 查看
1 excel读取和写入的方法

package com.poi.importxls;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
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.ss.formula.functions.Value;

public class ImportExcelTest {

public static void main(String[] args) throws Exception {
FileInputStream inputStream = null;
HSSFWorkbook book = null;
HSSFRow row = null;
HSSFCell cell = null;
inputStream = new FileInputStream("d:/docs/22李荣光 - 副本.xls");
book = new HSSFWorkbook(inputStream);

HSSFSheet sheet = book.getSheetAt(0);
int rows = sheet.getLastRowNum();
System.out.println("行数:"+rows);
for (int i = 0; i <= rows; i++) {
if(i>=20) continue;
row = sheet.getRow(i);
int cols = row.getLastCellNum();
System.out.println(i+"行列数"+cols);

for (int j = 0; j < cols; j++) {
cell = row.getCell(j);
Object obj;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
obj = cell.getNumericCellValue();
System.out.println(j+"列"+obj);
break;
case HSSFCell.CELL_TYPE_STRING:
obj = cell.getStringCellValue();
System.out.println(j+"列"+obj);
break;
default:
break;
}
}

}

book.write(new FileOutputStream("d:/docs/22李荣光 - 副本-2.xls"));

}
}

2

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: