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>


你可能感兴趣的:(java,Excel)