JXL操作excel

需要的JAR包:jxl-2.4.2.jar

 

import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JxlExcel {

    // JXL导出Excel
    public static void exportExcel() {
        // 行数
        int rows = 555;
        // 列数
        int columns = 5;
        // 文件地址
        String excelName = "C:\\Users\\YY\\Desktop\\table.xls";
        try {
            File excelFile = new File(excelName);
            // 如果文件存在就删除它
            if (excelFile.exists()) {
                excelFile.delete();
            }
            // 打开文件
            WritableWorkbook workbook = Workbook.createWorkbook(excelFile);
            // 创建工作表,参数1为工作表名称,参数2为工作表显示顺序
            WritableSheet sheet = workbook.createSheet("工作表一", 0);
            // 循环创建单元格(Excel2003最大数据行为65536)
            Label label = null;
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    // 参数1为列,参数2为行,参数3为值
                    label = new Label(j, i, "Test1_" + (j + 1) + "_" + (i + 1));
                    // 添加单元格到工作表Sheet
                    sheet.addCell(label);
                }
            }
            // 写入数据并关闭文件
            workbook.write();
            workbook.close();
            System.out.println("导出Excel完成");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    // JXL读取Excel
    public static void readExcel() {
        // 文件地址
        String excelName = "C:\\Users\\YY\\Desktop\\table.xls";
        try {
            File excelFile = new File(excelName);
            // 获得文件
            Workbook workbook = Workbook.getWorkbook(excelFile);
            // 获得工作表
            Sheet[] sheet = workbook.getSheets();
            // 循环工作表
            for (int i = 0; i < sheet.length; i++) {
                for (int j = 0; j < sheet[i].getRows(); j++) {
                    for (int k = 0; k < sheet[i].getColumns(); k++) {
                        // 获得单元格值
                        System.out.print(sheet[i].getCell(k, j).getContents()
                                + "  ");
                    }
                    System.out.println();
                }
            }
            // 关闭文件
            workbook.close();
            System.out.println("读取Excel完成");
        } catch (IOException e) {
            System.out.println(e);
        } catch (BiffException e) {
            System.out.println(e);
        }
    }

    // JXL修改Excel
    public static void updateExcel() {
        // 文件地址
        String excelName = "C:\\Users\\YY\\Desktop\\table.xls";
        try {
            File excelFile = new File(excelName);
            // 获得文件
            Workbook wb = Workbook.getWorkbook(excelFile);
            // 打开一个文件的副本,并指定数据写回到原文件
            WritableWorkbook book = Workbook.createWorkbook(excelFile, wb);
            // 获得第一个工作表
            WritableSheet sheet = book.getSheet(0);
            // 循环修改指定单元格
            for (int i = 0; i < sheet.getRows(); i++) {
                for (int j = 0; j < sheet.getColumns(); j++) {
                    // 如果单元格的值为"Test1_1_1",则修改为"Test_111"
                    String cells = sheet.getCell(j, i).getContents();
                    if (cells.equals("Test1_1_1")) {
                        Label labelCFC = new Label(j, i, "Test_111");
                        sheet.addCell(labelCFC);
                    }
                }
            }
            // 添加一个工作表
            WritableSheet sheet2 = book.createSheet("第二页 ", 1);
            sheet2.addCell(new Label(0, 0, "第二页的测试数据 "));
            // 写入数据并关闭文件
            book.write();
            book.close();
            System.out.println("修改Excel完成");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        exportExcel();
        updateExcel();
        readExcel();
    }

}

 

posted @ 2014-12-23 20:24  Hehe_yy  阅读(175)  评论(0编辑  收藏  举报