关于POI操作Excel的详细步骤

关于POI选择

我这里使用的是4.0.1版本的。需要导入:poi-4.0.1.jar和poi-ooxml-4.0.1.jar,这两个jar包。


97-2003版本的Excel操作方法

读取Excel表格

private HSSFWorkbook wb = null;								//excel操作对象
private HSSFSheet sheet = null;									//表格对象
private HSSFRow row = null;										//定义行对象
private HSSFCell cell = null; 										//定义列对象

File file = new File(文件名);
FileInputStream fis = null;											//文件输入流
POIFSFileSystem fs = null;
try {
			fis = new FileInputStream(file);
			fs = new POIFSFileSystem(fis);
			wb = new HSSFWorkbook(fs);
			sheet = wb.getSheetAt(0);										/第一张表
			int firstRowNum = sheet.getFirstRowNum();			
			row = sheet.getRow(firstRowNum);						//获取第一行
			short lastCellNum = row.getLastCellNum();			//获取多少列
			while(i <= lastCellNum ) {
				cell = row.getCell(i);
				cell.setCellType(CellType.STRING);
				String string1 = cell.getStringCellValue();
				++i;
			}
	} catch (FileNotFoundException e) {
			e.printStackTrace();
	} catch (IOException e) {
			e.printStackTrace();
	} finally {
				//关闭资源
				try {
					fis.close();
					fs.close();
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	}

2007版本以上Excel操作方法

读取Excel表格

private XSSFWorkbook xssfWorkbook = null;
private FileInputStream fis = null;
private XSSFSheet sheetAt = null;
private XSSFRow row = null;
private XSSFCell cell = null;

File file = new File(文件名);
try {
			fis = new FileInputStream(file);
			xssfWorkbook = new XSSFWorkbook(fis);
			sheetAt = xssfWorkbook.getSheetAt(0);
			int firstRowNum = sheetAt.getFirstRowNum();				//获取第一行
			row = sheetAt.getRow(firstRowNum);
			
			short lastCellNum = row.getLastCellNum();					//获取多少列
			while(i < lastCellNum) {
				cell = row.getCell(i);
				cell.setCellType(CellType.STRING);
				String string1 = cell.getStringCellValue();
				++i;
			}	
	} catch (IOException e) {
			e.printStackTrace();
	} finally {
		//关闭资源
		if(fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if(xssfWorkbook != null) {
			try {
				xssfWorkbook.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

写入Excel表格

XSSFWorkbook xssfWorkbook = null;
FileOutputStream fos = null;
XSSFSheet createSheet = null;
XSSFRow createRow = null;
XSSFCell createCell = null;
int size = 10;
File file = new File(文件名);	
try {
			xssfWorkbook = new XSSFWorkbook();
			createSheet = xssfWorkbook.createSheet();
			createRow = createSheet.createRow(0);
			
			for(int i = 0; i < size; ++i) {
				createCell = createRow.createCell(i);
				createCell.setCellType(CellType.STRING);
				createCell.setCellValue(“内容”);
			}
			fos = new FileOutputStream(file);
			xssfWorkbook.write(fos);
}catch(IOException e) {
			e.printStackTrace();
}finally {
		try {
			fos.close();
			xssfWorkbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
}

其他的方法可以查看文档使用,以上只是poi读取、写入excel的简单操作。记录下来,以便以后使用。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值