使用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文件格式   转载

http://blog.csdn.net/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) 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);
 }

 

 

 

 

List list = new ArrayList();
  try {
   Workbook rwb = null;
   Cell cell = null;

   // 创建输入流
   InputStream stream = new FileInputStream(excelFileName);

   // 获取Excel文件对象
   rwb = Workbook.getWorkbook(stream);

   // 获取文件的指定工作表 默认的第一个
   Sheet sheet = rwb.getSheet(0);

   // 行数(表头的目录不需要,从1开始)
   for (int i = 1; i < sheet.getRows(); i++) {

    // 创建一个数组 用来存储每一列的值
    String[] str = new String[sheet.getColumns()];

    // 列数
    for (int j = 0; j < sheet.getColumns(); j++) {

     // 获取第i行,第j列的值
     cell = sheet.getCell(j, i);
     str[j] = cell.getContents();

    }
    // 把刚获取的列存入list
    list.add(str);
   }

   // 返回值集合
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (BiffException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

你可能感兴趣的:(使用java读取excel(包括2003和2007))