星空网 > 软件开发 > Java

java读取03、07版EXCEL

03版excel,需要用到jxl.jar这个jar包

package test.poi;import java.io.File;import java.io.IOException;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;//03版excelpublic class getExcel {  public static void main(String[] args){    show03Excel();  }  public static void show03Excel(){    Sheet sheet;    Workbook book = null;    try {      book= Workbook.getWorkbook(new File("F:\\Book1.xls"));    } catch (BiffException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }//    System.out.println(book);    //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)    sheet=book.getSheet(0);    int rows = 0;    int r=0,l=0;    String value=null;    while(sheet.getCell(0,r).getContents()!=""&&sheet.getCell(0,r).getContents()!=null){      r++;    }    while(sheet.getCell(l,0).getContents()!=""&&sheet.getCell(l,0).getContents()!=null){      l++;    }    for(int i=0;i<(r);i++){      for(int j=0;j<(l);j++)        System.out.print(sheet.getCell(j,i).getContents()+"\t\t");      System.out.println();    }  }}

07版EXCEL,需要用到poi这个jar包

package test.poi;import java.io.IOException;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class get07Excel {  public static void main(String[] arg){    show07excel();  }  public static void show07excel(){    XSSFWorkbook xwb =null;    try {      xwb= new XSSFWorkbook("F:\\Book1.xlsx");    } catch (IOException e) {      e.printStackTrace();    }    XSSFSheet sheet=xwb.getSheetAt(0);    XSSFRow row;    String cell;    int r=0,l=0;    row = sheet.getRow(0);    while(row.getCell(l).toString()!=""&&row.getCell(l).toString()!=null){      l++;    }    while(row.getCell(0).toString()!=""&&row.getCell(0).toString()!=null){      r++;      row = sheet.getRow(r);    }    System.out.println("the rows is "+r+" and the number of one row is "+l);    // 循环输出表格中的内容    for (int i = sheet.getFirstRowNum(); i < r; i++)    {      row = sheet.getRow(i);      for (int j = row.getFirstCellNum(); j < l; j++)      {        cell = row.getCell(j).toString();        System.out.print(cell + "\t");      }      System.out.println("");    }  }}




原标题:java读取03、07版EXCEL

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流