C/C++教程

如何实现excel导入和导出(jxl)

本文主要是介绍如何实现excel导入和导出(jxl),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

第一步,先下载jxl-2.6.12.jar(我下载的版本),可以换别的版本

下载地址:https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl

下载完jxl-2.6.12.jar包直接CV到resources下

第二步,创建的maven项目,并在pom中导入jxl和excel的依赖

    <dependencies>
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
        <dependency>
            <groupId>cn.tedu</groupId>
            <artifactId>excel</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

 第三步,excel导入(写入),创建写入类JxlWriteExcel

package com.wang;
import java.io.File;
import java.util.Scanner;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class JxlWriteExcel {

    public static void main(String[] args) {

        String[] title = { "序号", "姓名", "性别" ,"年龄"};
        // 1.创建Excel文件
        File file = new File("H:\\staff.xls");
        try {
            file.createNewFile();
            // 2.创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(file);

            // 3.创建工作表
            WritableSheet sheet = workbook.createSheet("sheet01", 0);
            Label label = null;

            // 4.添加表头数据
            for (int i = 0; i < title.length; i++) {
                label = new Label(i, 0, title[i]);
                sheet.addCell(label);
            }
            //5.输入多少行
            System.out.println("请输入行数");
            Integer line = new Scanner(System.in).nextInt();

            // 6.添加行数据
            for (int i =1; i <= line ; i++) {

                label = new Label(0,i,""+i);
                sheet.addCell(label);
                //7.输入姓名
                System.out.println("请输入姓名");
                String name = new Scanner(System.in).nextLine();
                //8.请输入性别
                System.out.println("请输入性别");
                String sex = new Scanner(System.in).nextLine();
                //8.请输入年龄
                System.out.println("请输入年龄");
                Integer age = new Scanner(System.in).nextInt();
                String age1 = String.valueOf(age);

                label = new Label(1, i, name );
                sheet.addCell(label);

                label = new Label(2, i, sex);
                sheet.addCell(label);

                label = new Label(3, i, age1);
                sheet.addCell(label);
            }
            //9.写入数据,关闭工作簿
            workbook.write();
            workbook.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

 

excel导出(读取) ,创建读取类JxlReadExcel

package com.wang;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class JxlReadExcel {
    public static void main(String[] args) {
        try {

            // 1.创建工作簿
            Workbook workbook = Workbook.getWorkbook(new File("H:\\staff.xls"));

            // 2.通过索引获取工作表
            Sheet sheet = workbook.getSheet(0);

            // 3.获取工作表中的数据
            for (int i = 0; i < sheet.getRows(); i++) {
                for (int j = 0; j < sheet.getColumns(); j++) {
                    Cell cell = sheet.getCell(j, i);
                    System.out.print(cell.getContents() + " ");
                }

                System.out.println();
            }
            // 4.关闭workbook对象
            workbook.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

注意:

1.是否有相应盘符(我用的是H盘)

2.excel对应后缀为 .xls

这篇关于如何实现excel导入和导出(jxl)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!