zoukankan      html  css  js  c++  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版excel
    public 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()+"		");
                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 + "	");
                }
                System.out.println("");
            }
        }
    }
  • 相关阅读:
    json学习系列(1)-使用json所要用到的jar包下载
    Java 时间架构图
    时间纪元与时区介绍
    HTML5 Canvas 绘制库存变化折线
    HTML5 Canvas 笛卡尔坐标系转换尝试
    像孩童一样欣喜的看着自己的成长
    《老炮儿》结尾貌似历史上的一幕
    很多人还在守着金饭碗要饭
    还是用文本编辑器编程让人愉悦
    Node.js 网页爬虫再进阶,cheerio助力
  • 原文地址:https://www.cnblogs.com/xiufengd/p/4874394.html
Copyright © 2011-2022 走看看