Java使用Apache.POI中HSSFWorkbook导出到excel

1、POI 介绍

      Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”。

基本功能:
     HSSF - 提供读写Microsoft Excel格式档案(.xls)的功能。
     XSSF - 提供读写Microsoft Excel OOXML格式档案(.xlsx)的功能。
     HWPF - 提供读写Microsoft Word格式档案的功能。
     HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
     HDGF - 提供读写Microsoft Visio格式档案的功能。

2、使用POI中的HSSFWorkbook导出到excel流程

  2.1、环境配置

      2.1.1、如果你的Java项目是基于maven管理,可以直接在pom.xml文件中加入apache.poi依赖。如下所示

          <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
            <dependency>
                  <groupId>org.apache.poi</groupId>
                 <artifactId>poi</artifactId>
                 <version>3.17</version>
           </dependency>

     2.1.2、如果你的项目不是基于maven的,可以直接在 https://poi.apache.org/download.html点击下载jar包,然后导入到项目中去,在该链接里面有关于POI相关的文档可以下载下来学习使用。

  2.2、导出excel的poi相关api介绍

常用类介绍:

HSSFWorkbook                      excel的文档对象

HSSFSheet                         excel的表单

HSSFRow                           excel的行

HSSFCell                          excel的格子单元

HSSFFont                          excel字体
样式:
HSSFCellStyle                       cell样式,用于设置row颜色等等

  2.3、java导出到excel实例

       2.3.1 导出所用到的Users类

public class Users {
private String Hobby;
private  String Name;
private String sex;
public Users() {
}
public String getHobby() {
return Hobby;
}
public void setHobby(String hobby) {
Hobby = hobby;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}

2.3.2导出到excel操作类

package com.jianzhi.offer;


import java.util.List;
import java.io.FileOutputStream;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

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

public class Excel {
public static void main(String[] args) {
     try {
    //设置输出到本地的excel文件的名字和路径
          FileOutputStream out = new FileOutputStream("d:\\excel13.xls");
          //生成excel文档对象
          HSSFWorkbook workBook = new HSSFWorkbook();   
          //创建工作簿
          HSSFSheet mySheet = workBook.createSheet();
          //设置工作簿的名字
          workBook.setSheetName(0, "我的工作簿1");
          //创建第一行,标题行
          int rowNomber=-1;
          HSSFRow myRow = mySheet.createRow(++rowNomber);
          HSSFCellStyle style = workBook.createCellStyle();
          //设置字体样式
          HSSFFont font = workBook.createFont();
          font.setFontName("宋体");
          font.setFontHeightInPoints((short) 10);
          style.setFont(font);
           
          //设置标题行,每一列的标题
          HSSFCell cell = myRow.createCell((short) 0);
          cell.setCellStyle(style);

          cell.setCellValue("姓名");

          cell = myRow.createCell((short) 1);
          cell.setCellStyle(style);

          cell.setCellValue("性别");

          cell = myRow.createCell((short) 2);
          cell.setCellStyle(style);
          cell.setCellValue("爱好");
          
          //自己造数据,将数据填充到excel中
          List<Users> list = new Excel().getUsers();
          System.out.println("==="+list.size());
          for(int i = 1; i <= list.size(); i++){
          //创建行
          HSSFRow  Row = mySheet.createRow(++rowNomber);
               Users user = list.get(i-1);
               //创建行中的列,并赋值
               HSSFCell  cellfirst = Row.createCell((short) 0);
               cellfirst.setCellValue(user.getName());  
               HSSFCell  cellsecond  = Row.createCell((short) 1);
               cellsecond.setCellValue(user.getSex());
               HSSFCell  cellthrid = Row.createCell((short) 2);
               cellthrid.setCellValue(user.getHobby());
          }
          System.out.println("===1"+rowNomber);
          //写文件到本地
          workBook.write(out);
          out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 public List<Users> getUsers(){
     List<Users> users= new ArrayList<Users>();
       Users u = new Users();
        u.setHobby("登山");
        u.setName("Lisa");
        u.setSex("女");
       Users u1 =new Users();
         u1.setHobby("游泳");
         u1.setName("Tom");
         u1.setSex("男");
       Users u2 =new Users();
         u2.setHobby("看电影");
         u2.setName("Harry");
         u2.setSex("男");
       Users u3 =new Users();
         u3.setHobby("看电影");
         u3.setName("Tina");
         u3.setSex("男");
         Users u4 =new Users();
         u4.setHobby("跑步");
         u4.setName("obla");
         u4.setSex("男");
         users.add(u);
         users.add(u1);
         users.add(u2);
         users.add(u3);
         users.add(u4);
   return users;
  }
 }

2.3.4结果显示






猜你喜欢

转载自blog.csdn.net/weixin_42289193/article/details/81060185