java读写excel2003文件_使用java读取excel(包括2003和2007)

读取excel2003需要使用apache提供的jxl.jar这个jar包:

InputStream is = new FileInputStream(fileDir);

jxl.Workbook rwb = Workbook.getWorkbook(is);

Sheet rs = rwb.getSheet(0);

int rsColumns = rs.getColumns();

int rsRows = rs.getRows();

if (i < rsRows) {

for (int j = 0; j < rsColumns; j++) {

rs.getCell(j, i).getContents();

}

}

读取2007需要使用apache得poi包:

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

XSSFWorkbook xwb=new XSSFWorkbook(fileDir);

XSSFSheet sheet=xwb.getSheetAt(0);

XSSFRow row;

String cell=null;

int m=0;

int max=0;

for(int

i=sheet.getFirstRowNum();i

{

m=sheet.getRow(i).getPhysicalNumberOfCells();

if (m>=max) {

max=m;

}

else {

}

}

for(int

i=sheet.getFirstRowNum();i

{

Element root_2 = new Element("columns");

root.appendChild(root_2);

row=sheet.getRow(i);

for(int j=0;j

{

try {

cell=row.getCell(j).toString();

} catch (Exception e) {

cell=null;

}

Element root_3 = new Element("column");

root_3.appendChild(cell);

root_2.appendChild(root_3);

}

}

Document document = new Document(root);

return document.toXML();

JExcelAPI读写excel文件的例子。支持excel2003文件格式 转载

import java.io.*;

import jxl.*;

import jxl.write.*;

import jxl.format.*;

import java.util.*;

import java.awt.Color;

public class TestExcel {

public static void writeExcel(File f) throws

Exception {

jxl.write.WritableWorkbook wwb

= Workbook.createWorkbook(f);

jxl.write.WritableSheet ws =

wwb.createSheet("TestSheet1", 0);

jxl.write.Label labelC = new

jxl.write.Label(0, 0, "我爱中国");

ws.addCell(labelC);

jxl.write.WritableFont wfc =

new jxl.write.WritableFont(

WritableFont.ARIAL,

20, WritableFont.BOLD, false,

UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.GREEN);

jxl.write.WritableCellFormat

wcfFC = new jxl.write.WritableCellFormat(

wfc);

wcfFC.setBackground(jxl.format.Colour.RED);

labelC = new jxl.write.Label(6,

0, "中国爱我a", wcfFC);

ws.addCell(labelC);

// 写入Exel工作表

wwb.write();

// 关闭Excel工作薄对象

wwb.close();

}

public static void readExcel(File os) throws

Exception {

Workbook wb =

Workbook.getWorkbook(os);

Sheet s =

wb.getSheet("Sheet1");

Cell c = s.getCell(0,0);

System.out.println(c.getContents());

}

// 最好写一个这样的main方法来测试一下你的这个class是否写好了。

public static void main(String[] args) throws

Exception {

File f = new

File("c:\\kk1.xls");

// f.createNewFile();

// writeExcel(f);

readExcel(f);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值