JAVA读取EXCEL文件

JAVA读取EXCEL主要有以下几种方法:

1、JDBC-ODBC EXCEL Driver

2、jxl.jar

3、jcom.jar

4、poi

本文主要介绍jxl.jar方法。

jxl.jar文件下载地址:http://www.andykhan.com/jexcelapi/download.html

下载压缩包,压缩包中包含了jxl.jar文件和源代码。只需要把jxl.jar文件解压出来,然后导入到eclipse中。


读取EXCEL文件内容:

<span style="font-size:18px;">	public static void readExcel(String fileName){
		File file = new File(fileName);
		int column,row;
		try {
			Workbook wb = Workbook.getWorkbook(file);
			Sheet sheet = wb.getSheet(0);
			row = sheet.getRows();
			column = sheet.getColumns();
			//System.out.println(row + "--" + column);
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < column; j++) {
					//System.out.println(i + "--" + j);
					Cell cell = sheet.getCell(j, i);
					System.out.print(cell.getContents() + " ");
				}
				System.out.println();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
<span style="white-space:pre">			</span>wb.close();
<span style="white-space:pre">		</span>}
		
	}</span>

往EXCEL文件中写内容:

<span style="font-size:18px;">	public static void writeExcel(String fileName){
		File file = new File(fileName);
		int row = 10;
		int column = 10;
		WritableWorkbook  book = null;
		try {
			book = Workbook.createWorkbook(file);
			WritableSheet sheet = book.createSheet("sheet1", 0);
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < column; j++) {
					Label label = new Label(j,i,"ok");
					sheet.addCell(label);
				}
			}
			book.write();
			book.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}</span>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值