poi方式写入数据到Excel

在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();

            }

     }

    }

时间: 2024-09-17 04:44:33

poi方式写入数据到Excel的相关文章

POI向带有表头的excel模板里写入数据,写完后就把表头置空了

问题描述 如题,利用POI向带有表头的excel模板里写入数据,hsWorkbook.write(fos);fos.close();执行完后就把表头置空了,请问各位高手怎么处理?谢谢大家. 解决方案 解决方案二:FileInputStreamfile=newFileInputStream("SampleTest.xls");HSSFWorkbookhsWorkbook=newHSSFWorkbook(newPOIFSFileSystem(file));HSSFSheethssheet=

如何通过poi读取Excel中合并的单元格再以同样的样式写入另外一个Excel中?

问题描述 现在有个需求就是通过解析一个Excel模板来生成Excel报表,取写数据不谈,但得保证的一个是生成的这个Excel报表与模板的样式肯定是一样的,这样就遇到个问题,比如报表的标题,通常都是几个单元格合并起来的,通过poi读取的时候,第一行只有第一列有数据,其他都为空(Excel本身就是这样的吧),那这样我如何取到模板的合并单元格的样式再写入另外的excel中呢? 问题补充:hudingchen 写道 解决方案 以前的一个例子,你参考下:FileOutputStream fileOut =

导出大数据量excel,用POI

问题描述 导出大数据量excel,用POI 需要导出的数据量可能非常大(超过10W条),而且要照顾到没装07Excel的机器,不能用SXSSFworkbook.鉴于一个sheet页最多65000多条记录,那么大数据就需要多个sheet页.由于导出前要先查数据库,是应该一次查出所有数据然后导出还是分次查询?每次查询65000条数据? 解决方案 这么大的文件,excel 2003未必能打开.建议分页存入多个excel文件. 解决方案二: 看速度和效率了,分次是不是速度还快点

poi方式导出excel报表

问题描述 如何在poi方式导出的excel报表中,在某个整列添加超链接?谢谢 问题补充:renpeng301 写道 解决方案 你试试不用你导出的值 直接写个"测试" 看我刚才说的能否成功.解决方案二:那你的值不对 应该和我刚提供的那种方法没什么关系的,我的那种方法确实可以打出链接的,我经常用的.有可能你取值的时候本身就不对,望lz仔细检查一下下.不懂的也可以站内联系我啦.解决方案三:你的值打印出来是对的吧??解决方案四:renpeng301 写道Java代码 cell.setCellT

各位大侠,请问java POI能实现用流文件读写方式拷贝一个模板excel的图片到另外一个excel中吗?

问题描述 如题,请问javaPOI能实现用流文件读写方式拷贝一个模板excel的图片到另外一个excel中吗?还有一个问题,就是POI能否流文件读写excel中插入的自定义图形,如下图所示

Java 使用poi把数据库中数据导入Excel的解决方法_java

Java 利用poi把数据库中数据导入Excel 效果: 使用时先把poi包导入工程的path,注意只需要导入poi包即可,下载后有三个jar包 核心代码: 连接数据库:DBConnection.java 复制代码 代码如下: package org.xg.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;i

我用c#向excel写入数据,每写入一行excel就弹出来一次

问题描述 我用c#向excel写入数据,这时ExcelApp.Visible=true,每写入一行excel就弹出来一次,;如果ExcelApp.Visible=false:又打不开excel,请问怎么不让excel弹出来,打开excel 解决方案 解决方案二:用npoi组件,最好不要用com组件.解决方案三:推荐使用NOPI去写Excel,参考链接:,你也可以使用COM组件去做,参考例子:

客户端调用服务器端的excel模板,并向此excel中特定的单元格中写入数据,最后将这个excel文件保存在客户端机器上或打印,而服务器端excel模板不变

问题描述 bs模式,vs2008,sql2005工作中遇到一个难题,向大家请教:我自己先做好excel模板,比如说"检定证书模版.xlsx",主要是设置好表头.第一列.以及表的底部的各种格式,然后将此文件放到服务器网站的文件下的一个文件夹内.客户端通过浏览器进行操作,并将从数据库把数据调入上面的模板中.通过点击一个按钮,客户端就可以完成保存此excel文件并可以直接打印.这个过程中我会从sqlserver数据库中读取到符合要求的数据,并写入到此excel文件中的特定单元格里,单元格是固

jxl操作excel写入数据不覆盖原有数据示例_java

需要导入的包:jxl.jar 复制代码 代码如下: public void readTO() {        Workbook wb = null;        WritableWorkbook wwb = null;        try {            File is = new File(System.getProperty("user.dir") + "\\in.xls");            File os = new File(Syst