java excel demo_JAVA Excel Demo

package

exceldemo.BO;

import

java.io.InputStream;

import

java.io.FileInputStream;

import

java.io.

*

;

import

jxl.Workbook;

import

jxl.read.biff.BiffException;

import

jxl.Sheet;

import

jxl.Cell;

import

jxl.write.WritableWorkbook;

import

jxl.write.WritableSheet;

import

jxl.write.Label;

import

jxl.write.WriteException;

import

jxl.write.WritableCell;

import

jxl.write.WritableImage;

import

jxl.write.WritableFont;

import

jxl.format.UnderlineStyle;

import

jxl.write.WritableCellFormat;

public

class

WriteExcel {

public

WriteExcel() {

}

/**

* 读取Excel文件

*/

public

void

readExcel(){

try

{

InputStream is

=

new

FileInputStream(

"

c:\\ss.xls

"

);

Workbook workbook

=

Workbook.getWorkbook(is);

//

构建Workbook对象, 只读Workbook对象

System.out.println(

"

工作表个数:

"

+

workbook.getNumberOfSheets());

Sheet[] sheet

=

workbook.getSheets();

//

获取Sheet表

int

columns

=

sheet[

0

].getColumns();

int

rows

=

sheet[

0

].getRows();

System.out.println(

"

"

+

rows

+

"

"

+

columns

+

"

"

);

for

(

int

i

=

0

;i

<

rows;i

++

){

for

(

int

j

=

0

;j

<

columns;j

++

){

Cell cell00

=

sheet[

0

].getCell(j,i);

System.out.print(cell00.getContents());

}

System.out.println(

""

);

}

workbook.close();

is.close();

}

catch

(FileNotFoundException ex) {ex.printStackTrace();

}

catch

(BiffException ex) {ex.printStackTrace();

}

catch

(IOException ex) {ex.printStackTrace();}

}

/**

* 写Excel文件

*/

public

void

writeExcel(){

File newExcel

=

new

File(

"

c:\\newExcel.xls

"

);

try

{

WritableWorkbook wwb

=

Workbook.createWorkbook(newExcel);

//

创建可写入的Excel工作薄

//

Method 2:将WritableWorkbook直接写入到输出流

/*

OutputStream os = new FileOutputStream(targetfile);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);

*/

WritableSheet ws

=

wwb.createSheet(

"

TheFirstSheet

"

,

0

);

//

创建Excel工作表

//

普通文本

Label label00

=

new

Label(

0

,

0

,

"

derek

"

);

ws.addCell(label00);

WritableCell wc

=

new

Label(

1

,

0

,

"

man

"

);

ws.addCell(wc);

//

图片

WritableImage wi

=

new

WritableImage(

1

,

5

,

1

,

3

,

new

File(

"

c:\\300.PNG

"

));

//

参数表示(列位置,行位置,宽占列数,高占行数,图片路径)

ws.addImage(wi);

//

格式化

WritableFont wfc

=

new

WritableFont(WritableFont.ARIAL,

10

,WritableFont.BOLD,

false

,UnderlineStyle.DOUBLE,jxl.format.Colour.RED);

WritableCellFormat wcfFC

=

new

jxl.write.WritableCellFormat(wfc);

Label labelCFC

=

new

jxl.write.Label(

2

,

0

,

"

This is a Label Cell

"

, wcfFC);

ws.addCell(labelCFC);

//

写入Exel工作表

wwb.write();

//

关闭Excel工作薄对象

wwb.close();

}

catch

(IOException ex) {ex.printStackTrace();}

catch

(WriteException ex) {

/**

@todo Handle this exception

*/

}

}

public

static

void

main(String[] args) {

WriteExcel writeexcel

=

new

WriteExcel();

writeexcel.writeExcel();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值