在java数据库编程中,常常会用到向excel中读写数据,一方面可以将数据从数据库导出到Excel,进行数据展示,另一方面可以批量的向数据库插入多条数据,这对于软件开发是必不可少的,今天先介绍如何使用java向excel中写入数据,这里以2003版本的excel版本为例,分享我的实战经验。(在后续的经验中会介绍excel数据导出,敬请浏览)

工具/原料

  • eclipse, java poi的jar包

方法/步骤

  1. 1

    导入POI的jar包

    新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:

    步骤阅读
  2. 2

    编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:

    package com.qiang.poi;

    public class Computer {

    private int id;

    private String name;

    private String description;

    private double price;

    private double credit;

    public int getId() {

    return id;

    }

    public Computer(int id, String name, String description, double price,

    double credit) {

    super();

    this.id = id;

    this.name = name;

    this.description = description;

    this.price = price;

    this.credit = credit;

    }

    public void setId(int id) {

    this.id = id;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getDescription() {

    return description;

    }

    public void setDescription(String description) {

    this.description = description;

    }

    public double getPrice() {

    return price;

    }

    public void setPrice(double price) {

    this.price = price;

    }

    public double getCredit() {

    return credit;

    }

    public void setCredit(double credit) {

    this.credit = credit;

    }

    }

  3. 3

    新建一个写入excel的方法,如write2excel,参数可以后面边写边决定(站在一个不熟悉POI的角度)

    public static void write2Excel(){}

  4. 4

    创建操作Excel的HSSFWorkbook对象

    HSSFWorkbook excel= new HSSFWorkbook();

  5. 5

    创建HSSFSheet对象

    Excel中的一个sheet(工作表)对应着java中的一个HSSFSheet对象,利用HSSFWorkbook对象可以创建一个HSSFSheet对象

    如:创建一个sheet名为computer的excel

    HSSFSheet sheet = excel.createSheet("computer");

  6. 6

    创建第一行标题信息的HSSFRow对象

    我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象

    如:创建表格中的第一行(我们常用来做标题的行)  HSSFRow firstRow = sheet.createRow(0); 注意下标从0开始

  7. 7

    创建标题行中的HSSFCell数组

    当然,excel中每一行是由若干个单元格,我们常称为cell,它对应着java中的HSSFCell对象

    如:创建5个单元格       HSSFCell cells[] = new HSSFCell[5];

    //假设我们一行有五列数据

  8. 8

    创建标题数据,并通过HSSFCell对象的setCellValue()方法对每个单元格进行赋值

    既然单元格都准备好了,那最后是不是该填充数据了呀。对的,没错。填充数据之前,得把数据准备好吧,

    数据:String[] titles = new String[]{"id","name","description","price","credit"};

    插入一句话: 在这个时代,能让机器做的,尽量不让人来做,记住这句话。

    好的,继续。现在就通过for循环来填充第一行标题的数据

    for (int i = 0; i < 5; i++) {

    cells[0] = firstRow.createCell(i);

    cells[0].setCellValue(titles[i]);

    }

  9. 9

    数据分析

    第一行标题栏创建完毕后,就准备填充我们要写入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错

    把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。

    创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。

    假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象computers

  10. 10

    数据写入

    具体数据有了,又该让机器帮我们干活了,向excel中写入数据。

    for (int i = 0; i < computers.size(); i++) {

    HSSFRow row = sheet.createRow(i + 1);

    Computer computer = computers.get(i);

    HSSFCell cell = row.createCell(0);

    cell.setCellValue(computer.getId());

    cell = row.createCell(1);

    cell.setCellValue(computer.getName());

    cell = row.createCell(2);

    cell.setCellValue(computer.getDescription());

    cell = row.createCell(3);

    cell.setCellValue(computer.getPrice());

    cell = row.createCell(4);

    cell.setCellValue(computer.getCredit());

    }

  11. 11

    将数据真正的写入excel文件中

    做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。

    OutputStream out = null;

    try {

    out = new FileOutputStream(file);

    excel.write(out);

    out.close();

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    System.out.println("数据已经写入excel"); //温馨提示

  12. 12

    看看我的main方法吧

    public static void main(String[] args) throws IOException {

    File file = new File("test1.xls");

    if(!file.exists()){

    file.createNewFile();

    }

    List<Computer> computers = new ArrayList<Computer>();

    computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));

    computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));

    computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));

    computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));

    computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));

    write2excel(computers, file);

    }

  13. 13

    工程目录及执行main方法后的test1.xls数据展示

    步骤阅读
    步骤阅读

  14. 14

    源码分享,computer就不贴了

    package com.qiang.poi;

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStream;

    import java.util.ArrayList;

    import java.util.List;

    import org.apache.poi.hssf.usermodel.HSSFCell;

    import org.apache.poi.hssf.usermodel.HSSFRow;

    import org.apache.poi.hssf.usermodel.HSSFSheet;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    public class ReadExcel {

    public static void main(String[] args) throws IOException {

    File file = new File("test1.xls");

    if(!file.exists()){

    file.createNewFile();

    }

    List<Computer> computers = new ArrayList<Computer>();

    computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));

    computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));

    computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));

    computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));

    computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));

    write2excel(computers, file);

    }

    public static void write2excel(List<Computer> computers,File file) {

    HSSFWorkbook excel = new HSSFWorkbook();

    HSSFSheet sheet = excel.createSheet("computer");

    HSSFRow firstRow = sheet.createRow(0);

    HSSFCell cells[] = new HSSFCell[5];

    String[] titles = new String[] { "id", "name", "description", "price",

    "credit" };

    for (int i = 0; i < 5; i++) {

    cells[0] = firstRow.createCell(i);

    cells[0].setCellValue(titles[i]);

    }

    for (int i = 0; i < computers.size(); i++) {

    HSSFRow row = sheet.createRow(i + 1);

    Computer computer = computers.get(i);

    HSSFCell cell = row.createCell(0);

    cell.setCellValue(computer.getId());

    cell = row.createCell(1);

    cell.setCellValue(computer.getName());

    cell = row.createCell(2);

    cell.setCellValue(computer.getDescription());

    cell = row.createCell(3);

    cell.setCellValue(computer.getPrice());

    cell = row.createCell(4);

    cell.setCellValue(computer.getCredit());

    }

    OutputStream out = null;

    try {

    out = new FileOutputStream(file);

    excel.write(out);

    out.close();

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

poi方式写入数据到Excel相关推荐

  1. java使用poi.xssf 写入内容到excel表格中 和 读取 表格里面的数据

    一.java使用poi.xssf 写入内容到excel表格中 public class TestExcel {//创建一个excel工作簿public static String outputFile ...

  2. 使用poi导出大量数据到excel遇到的问题

    最近在工作遇到利用poi导出大量数据到excel并提供下载的运用场景,并遇到了一个问题,当数据量过大时(几十万),后台在进行数据写入excel中的过程会非常耗时,导致迟迟没有响应前台,结果数据还没导完 ...

  3. 使用 Python 第三方库 xlwt 写入数据到 Excel 工作表

    使用 Python 第三方库 xlwt 写入数据到 Excel 工作表 1. 安装 xlwt 库 2. 使用 xlwt 库 2.1 向 Excel 工作表写入单个数据 2.2 向 Excel 工作表写 ...

  4. java : enum、创建文件和文件夹、删除文件和文件夹、获得项目绝对路径、写入数据到excel中、java代码中两种路径符号写法、读取、写入text文件...

    java : enum http://www.cnblogs.com/hyl8218/p/5088287.html 创建文件和文件夹.删除文件和文件夹 http://www.cnblogs.com/m ...

  5. python xlwt包写入数据到excel文件出错解决办法

    #调试环境 debian10,python3.7,pycharm 2020.1 读取excel文件内容 # import xlrd import xlwt # workbook=xlrd.open_w ...

  6. python给excel排序_Python实现自定义顺序、排列写入数据到Excel的方法

    本文实例讲述了Python实现自定义顺序.排列写入数据到Excel的方法.分享给大家供大家参考,具体如下: 例1. 数据框顺序写入Excel: data=a import xlsxwriter wor ...

  7. POI读写超大数据量Excel,解决超过几万行而导致内存溢出的问题(附源码)

    来源:cnblogs.com/swordfall/p/8298386.html 1. Excel2003与Excel2007 两个版本的最大行数和列数不同,2003版最大行数是65536行,最大列数是 ...

  8. POI—将表格数据导入excel表格

    POI结构(接口) HSSF - 提供读写Microsoft Excel XLS格式档案的功能 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能 HWPF - ...

  9. 【文件处理】——Python pandas 写入数据到excel中

    目录 1.创建一个新的excel表格 2. 获取写入excel的数据data 3.将data类型转换为pandas接受的类型 4.写入到excel中 5.保存excel 最终结果 #!/usr/bin ...

最新文章

  1. 【青少年编程】【蓝桥杯】排队购票
  2. 大神的xml解析之路
  3. Android 最简单的基于FFmpeg的移动端例子:Android HelloWorld
  4. CF25E-Test【AC自动机,bfs】
  5. html中显示shell脚本的输出,网页从shell脚本中输入并显示结果
  6. 21秋期末考试财务会计(二)10165k2
  7. php变量前下滑_PHP变量
  8. Android自定义事件总线,android事件总线EventBus3.0使用方法详解
  9. 【戴嘉乐】(进阶)基于IPFS和Ngrok构建自维护资源网关
  10. mysql 8.0 ~ 安装
  11. html怎么只操作第一个li,css3如何选择第一个子元素?
  12. 上海富勒wms_【3PL | 宝时物流应用富勒WMS,提升现代化仓储管理水平 】
  13. 该怎么复习安徽省考计算机专业课
  14. C#编程总结(四)多线程应用(进度条的编程问题)——转自http://www.cnblogs.com/yank/p/3232955.html...
  15. 时下常用有效的rss源
  16. 可涂抹什么让指纹加深_碘熏显现指纹纹路的操作方法
  17. 上海基诺墙绘 中荷学生共同创作涂鸦 “We are伐木累”示好
  18. css中text文字超出宽度省略号显示并鼠标悬停显示剩余全部:
  19. Linux权限管理(week1_day5)--技术流ken
  20. 静态综合实验(企业内网访问外网工程)

热门文章

  1. pyqt5与matplotlib结合画图 ,绘制动态图形
  2. Solrj实现增删改查
  3. JavaScript实现唯一路径问题的回溯方法的算法(附完整源码)
  4. boost::process::system相关的测试程序
  5. boost::mp11::mp_power_set相关用法的测试程序
  6. boost::phoenix::function用法的测试程序
  7. boost::make_biconnected_planar用法的测试程序
  8. boost::geometry::correct_closure用法的测试程序
  9. Boost:bimap双图的range范围的测试程序
  10. Boost:ping的测试程序