Chinaunix首页 | 论坛 | 博客
  • 博客访问: 374420
  • 博文数量: 152
  • 博客积分: 6020
  • 博客等级: 准将
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-11 19:20
文章分类

全部博文(152)

文章存档

2017年(1)

2010年(1)

2007年(3)

2006年(147)

我的朋友

分类: Java

2006-04-19 23:26:23

package com.diegoyun.tutorial;


import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import jxl.read.biff.BiffException;

import java.io.*;

/**
 * User: Diegoyun
 * Date: 2006-3-6
 * Time: 22:05:09
 */
public class JxlSample {
    static String excelFilePath = "D:\\backup\\download\\sample.xls";

    static void readXls() {
        System.out.println("reading xls.");
        Workbook book = null;
        Sheet sheet = null;
        try {
            FileInputStream in = new FileInputStream(excelFilePath);


            book = Workbook.getWorkbook(in);
            sheet = book.getSheet(0)//get first sheet.

            int cnt = sheet.getRows();
            System.out.println(" row count is:" + cnt);

            Cell cell = null;
            for (int i = 0; i < cnt; i++) {
                //remember,the first parameter is column index ,and the second parameter is row index.
                cell = sheet.getCell(0, i);
                System.out.println(" cell value is:" + cell.getContents());
            }
            System.out.println("finished.");


        catch (FileNotFoundException e) {
            e.printStackTrace();
        catch (IOException e) {
            e.printStackTrace();
        catch (BiffException e) {
            e.printStackTrace();
        finally {
            if (sheet != null) {
                sheet = null;
            }
            if (book != null) {
                book = null;
            }
        }
    }

    static void writeXls() {
        System.out.println("writing exls.");
        WritableWorkbook wwb = null;
        WritableSheet sheet = null;
        try {
            String xlsName = "D:\\backup\\download\\temp.xls";
            File afile = new File(xlsName);
            if (!afile.exists()) {
                afile.createNewFile();
            }
            wwb = Workbook.createWorkbook(afile);
            sheet = wwb.createSheet(xlsName, 0);

            WritableFont wf = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false);
      WritableCellFormat format = new WritableCellFormat(wf);
      format.setAlignment(Alignment.CENTRE);
      format.setVerticalAlignment(VerticalAlignment.CENTRE);
            //remember,the first parameter is column index.
            Label cell = new Label(0,0,"row 1,column 1",format);//set the value of cell (0,0)
            sheet.addCell(cell);

            cell = new Label(1,0,"row 1,column 2",format);
            sheet.addCell(cell);

            wwb.write();
            System.out.println("finished.");

        catch (WriteException e) {
            e.printStackTrace();
        catch (IOException e) {
            e.printStackTrace();
        }   finally {
      if (wwb != null) {
        try {
          wwb.close();
        catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }

    }

    public static void main(String[] args) {
        readXls();
        writeXls();
    }
}

阅读(1134) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~