java用jxl.jar.对excel的操作

创建文件

    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\aa\\student.xls");
        try {
            //创建一个excel工作簿文件
            WritableWorkbook wwb = Workbook.createWorkbook(file);

            //关闭工作簿
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

写入数据

public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\aa\\student.xls");
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 =new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        Random random =new Random();
        try {
            WritableWorkbook wwb = Workbook.createWorkbook(file);
            WritableSheet ws = wwb.createSheet("学生列表", 0);
            //添加表头
            ws.addCell(new Label(0, 0, "学生编号"));
            ws.addCell(new Label(1, 0, "学生姓名"));
            ws.addCell(new Label(2, 0, "学生年龄"));
            ws.addCell(new Label(3, 0, "学生性别"));
            ws.addCell(new Label(4, 0, "学生帐号"));
            ws.addCell(new Label(5, 0, "学生密码"));
            ws.addCell(new Label(6, 0, "出生日期"));
            ws.addCell(new Label(7, 0, "创建时间"));
            //添加主题
            for (int row = 1; row < 101; row++) {
                ws.addCell(new Label(0, row, "学生编号"+row));
                ws.addCell(new Label(1, row, "张"+row));
                ws.addCell(new Label(2, row, random.nextInt(50)+""));
                ws.addCell(new Label(3, row, random.nextInt(2)==0?"女":"男"));
                ws.addCell(new Label(4, row, ""+123*row));
                ws.addCell(new Label(5, row, "*****"));
                ws.addCell(new Label(6, row, sdf.format(new Date())));
                ws.addCell(new Label(7, row, sdf2.format(new Date())));
            }
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

读取数据

public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\aa\\student.xls");
        try {
            Workbook wb = Workbook.getWorkbook(file);
            //获取第一个工作表,序号从0开始。也可以根据表名称获取
            Sheet sheet = wb.getSheet(0);
            //获取单元格
            int columns = sheet.getColumns();//表格最大列数
            int rows = sheet.getRows();//表格最大行数
            
            Cell[] column = sheet.getColumn(0);//获取某列的所有内容
            Arrays.stream(column).collect(Collectors.toList()).forEach(s-> System.out.println(s.getContents()));
            
            for (int i = 0; i<rows; i++) {
                for (int j = 0; j < columns; j++) {
                    System.out.print(sheet.getCell(j, i).getContents()+"\t");//getContents()将数据转换为字符串
                }
                System.out.println();
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

注意:

2007版本以前的excel,java写入数据只能写到第65536行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值