zoukankan      html  css  js  c++  java
  • jxl生成和解析Excel

    生成Excle

    package com.jmz.java;
    
    import java.io.File;
    
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    
    public class WriteExcelUseJXL {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String title[] = {"id","name","sex"};
            File file = new File("d:\jxl.xls");
            try {
                file.createNewFile();
                //创建工作簿
                WritableWorkbook workbook = Workbook.createWorkbook(file);
                //创建sheet页
                WritableSheet sheet = workbook.createSheet("sheet1",0);
                //创建label
                Label label = null;
                //添加表头
                for (int i = 0; i < title.length; i++) {
                    //列,行,内容
                    label = new Label(i, 0, title[i]);
                    sheet.addCell(label);
                }
                //添加内容
                for (int i = 1; i < 10; i++) {
                    label = new Label(0, i, "a"+i);
                    sheet.addCell(label);
                    label = new Label(1, i, "jim"+i);
                    sheet.addCell(label);
                    label = new Label(2, i, "男");
                    sheet.addCell(label);
                }
                workbook.write();
                workbook.close();
                
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }

    解析Excle

    package com.jmz.java;
    
    import java.io.File;
    import java.io.IOException;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    public class GetExclUseJXL {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            File file = new File("d:\jxl.xls");
            try {
                //获取workbook
                Workbook workbook = Workbook.getWorkbook(file);
                //获取sheet页
                Sheet sheet = workbook.getSheet(0);
                //循环行
                for (int i = 0; i < sheet.getRows(); i++) {
                    //循环列
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        //创建cell
                        Cell cell = sheet.getCell(j, i);
                        //打印
                        System.out.print(cell.getContents()+" ");
                    }
                    System.out.println();
                }
                //关闭
                workbook.close();
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
    }
  • 相关阅读:
    Milk 结构体
    Milk 结构体
    Repeating Decimals, ACM/ICPC World Finals 1990, UVa202
    Repeating Decimals, ACM/ICPC World Finals 1990, UVa202
    DNA Consensus String, ACM/ICPC Seoul 2006, UVa1368
    DNA Consensus String, ACM/ICPC Seoul 2006, UVa1368
    Crossword Answers, ACM/ICPC World Finals 1994, UVa232
    【编程技巧】 iOS 5的StoryBoard(故事板)的一些用法
    【开发技术】storyboard和nib的差别
    【编程技巧】ExtJs 设置GridPanel表格文本垂直居中
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5693497.html
Copyright © 2011-2022 走看看