excel2003 操作

本文提供使用Java读写Excel 2003和2007文件的示例代码,包括利用jxl和Apache POI库进行操作的方法。演示了如何创建、读取单元格内容及设置样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取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(inti=sheet.getFirstRowNum();i<sheet.getPhysicalNumberOfRows();i++)
    {
     m=sheet.getRow(i).getPhysicalNumberOfCells();
     if (m>=max) {
      max=m;
     }
     else {

     }
    }

     for(inti=sheet.getFirstRowNum();i<sheet.getPhysicalNumberOfRows();i++)
     {
      Element root_2 = new Element("columns");
      root.appendChild(root_2);
      row=sheet.getRow(i);
      for(int j=0;j<max+1;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文件格式  转载

http://blog.youkuaiyun.com/wonder4/archive/2006/07/04/874541.aspx

 

 

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) throwsException {
  jxl.write.WritableWorkbook wwb= Workbook.createWorkbook(f);
  jxl.write.WritableSheet ws =wwb.createSheet("TestSheet1", 0);
  jxl.write.Label labelC = newjxl.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.WritableCellFormatwcfFC = 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) throwsException {
  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) throwsException {
  File f = newFile("c:\\kk1.xls");
//  f.createNewFile();
//  writeExcel(f);
  readExcel(f);
 }

分享:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值