Java中操作Excel表格方法

引导语:我们都知道Excel 可以进行各种数据的处理、统计分析和辅助决策操作,那么在Java中又是如何操作Excel 表格的呢,以下是本站小编分享给大家的Java中操作Excel表格方法,欢迎阅读!

Java中操作Excel表格方法

利用Java Excel API ,下载地址:

下面给出一段读取数据的例子代码:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package excel;

import InputStream;

import NotFoundException;

import ception;

import tStream;

import l;

import er;

import ;

import t;

import book;

import Exception;

/**

*

* @author

*/

public class Main {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try {

InputStream is = new FileInputStream("d:");

book rwb = orkbook(is);

Sheet rs = heet(0);

//取第一行,第一列的元素

Cell c00 = ell(0, 0);

String strc00 = ontents();

//取第一行,第二列的元素

Cell c10 = ell(1,0);

String strc10= ontents();

tln(strc00+" "+ype()ring());

tln(strc10+" "+ype()ring());

//获取sheet的个数

tln(umberOfSheets());

Sheet [] sheets =heets();

for(int i=0;i

tln(heet(i)ame());

}

//获得列数

tln(heet(0)olumns());

//获得每列的.元素

Cell [] cell = heet(0)olumn(0);

//获得总行数

tln(heet(0)ows());

//获得行元素

Cell [] cell2 = heet(0)ow(0);

//关闭对象

e();

} catch (Exception ex) {

ogger(ame())(RE, null, ex);

}

}

}

再给出一段,创建Excel表格的代码:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package excel;

import ;

import l;

import er;

import book;

import l;

import ableCellFormat;

import ableFont;

import ableSheet;

import ableWorkbook;

import er;

/**

*

* @author

*/

public class Main {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try {

//创建EXCEL表格

WritableWorkbook wwb = teWorkbook(new File("d:"));

//创建工作表

WritableSheet ws = teSheet("",0);

//创建单元

ell(new Label(0,0,"Hello World"));

//添加设置字体格式的单元格

WritableFont wfc = new WritableFont(L,15,,true);

WritableCellFormat wcff= new WritableCellFormat(wfc);

Label labelcf = new Label(1,0,"Format text",wcff);

ell(labelcf);

//向单元格中写入数字

Number labelN = new Number(0,1,12345);

ell(labelN);

e();

e();

} catch (Exception ex) {

ogger(ame())(RE, null, ex);

}

}

}

最后给出一段更新表格的代码:

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package excel;

import ;

import ception;

import l;

import er;

import Type;

import book;

import Exception;

import l;

import ableCell;

import ableSheet;

import ableWorkbook;

/**

*

* @author

*/

public class Main {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try {

Workbook rw = orkbook(new File("d:"));

WritableWorkbook wwb = teWorkbook(new File("d:"),rw);

//修改单元格的值

WritableSheet ws = heet(0);

WritableCell wc = ritableCell(0,0);

if(ype() == L){

Label l= (Label)wc;

tring("Modified!!!");

}

e();

e();

e();

} catch (Exception ex) {

ogger(ame())(RE, null, ex);

}

}

}