java 生成Excel开门篇

本随笔的Excel所用的poi jar包(3.17版本)链接: https://pan.baidu.com/s/1gaa3dJueja8IraUDYCSLIQ  提取密码: 9xr7

简单实现:两个类:UserPojo 和ExcelUtil

废话不多说,直接上代码:

UserPojo.java 类

 1 package pojo;
 2 
 3 public class UserPojo {
 4     private int uid;
 5     private String uname;
 6     private String upass;
 7     private String udate;
 8 
 9     public UserPojo(int uid, String uname, String upass, String udate) {
10         super();
11         this.uid = uid;
12         this.uname = uname;
13         this.upass = upass;
14         this.udate = udate;
15     }
16 
17     public int getUid() {
18         return uid;
19     }
20 
21     public void setUid(int uid) {
22         this.uid = uid;
23     }
24 
25     public String getUname() {
26         return uname;
27     }
28 
29     public void setUname(String uname) {
30         this.uname = uname;
31     }
32 
33     public String getUpass() {
34         return upass;
35     }
36 
37     public void setUpass(String upass) {
38         this.upass = upass;
39     }
40 
41     public String getUdate() {
42         return udate;
43     }
44 
45     public void setUdate(String udate) {
46         this.udate = udate;
47     }
48 
49 }

ExcelUtil.java 类

  1 package util;
  2 
  3 import java.io.FileOutputStream;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 
  8 import org.apache.poi.hssf.usermodel.HSSFCell;
  9 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 10 import org.apache.poi.hssf.usermodel.HSSFRow;
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 13 import org.apache.poi.ss.usermodel.HorizontalAlignment;
 14 
 15 import pojo.UserPojo;
 16 
 17 public class ExcelUtil {
 18     
 19     /**
 20      * 
 21      * @Title: getUserPojo  
 22      * @Description: 创建一组测试数据
 23      * @param 
 24      * @param  Exception   参数  
 25      * @date 2018-06-04
 26      * 
 27      * @throws  
 28      * @return List<UserPojo>   返回类型
 29      * 
 30      */
 31     public static List<UserPojo> getUserPojo() throws Exception{
 32         
 33         List<UserPojo> list = new ArrayList<UserPojo>();
 34         UserPojo up1 = new UserPojo(91, "小明", "xiaoming1", "2018、03、21");
 35         UserPojo up2 = new UserPojo(100, "安妮", "anni", "2018-03-22");
 36         UserPojo up3 = new UserPojo(93, "dinosaurs", "dinosaurs", "2018年03月02日");
 37         list.add(up1);
 38         list.add(up2);
 39         list.add(up3);
 40         return list;
 41     }
 42     
 43     /**
 44      * 
 45      * @Title: main  
 46      * @Description: 生成Excel并输出到指定位置
 47      * @param args
 48      * @param Exception   参数  
 49      * @return void   返回类型
 50      * @date 2018-06-04
 51      * 
 52      */
 53     public static void main(String[] args) throws Exception {
 54         /*
 55          * 第一步:------------------------
 56          */
 57         //创建一个Webbook,对应着一个Excel文件
 58         @SuppressWarnings("resource") //去掉警告(也可以定义一个static的全局静态)
 59         HSSFWorkbook web = new HSSFWorkbook();
 60         
 61         /*
 62          * 第二步:------------------------
 63          */
 64         //在webbook中添加一个sheet,对应Excel文件中的sheet (在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称)
 65         HSSFSheet sheet = web.createSheet("2018年度5月人员表");
 66         
 67         // 用于格式化单元格的数据
 68         //HSSFDataFormat format = web.createDataFormat();
 69         
 70         /*
 71          * 第三步:------------------------
 72          */
 73         //在sheet中添加表头:且添加到第0行
 74         HSSFRow row = sheet.createRow(0);
 75         
 76         /*
 77          * 第四步:创建单元格, 添加表头,且让表头居中
 78          */
 79         HSSFCellStyle style = web.createCellStyle();
 80         //style.setDataFormat(format.getFormat("@"));
 81         
 82         //居中格式
 83         style.setAlignment(HorizontalAlignment.CENTER);
 84         
 85         
 86         HSSFCell cell = row.createCell((short) 0);//创建列 处于第0行的第0列
 87         cell.setCellValue("人员编号"); //设置value值
 88         sheet.setColumnWidth(0, 20*200);//设置列宽
 89         cell.setCellStyle(style);//让其居中
 90 
 91         
 92         cell = row.createCell((short) 1);    
 93         cell.setCellValue("人员姓名");
 94         sheet.setColumnWidth(1, 20*200);//设置列宽
 95         cell.setCellStyle(style);
 96 
 97         
 98         cell = row.createCell((short) 2);
 99         cell.setCellValue("人员别称");
100         sheet.setColumnWidth(2, 20*200);//设置列宽
101         cell.setCellStyle(style);
102 
103         
104         cell = row.createCell((short) 3);
105         cell.setCellValue("注册时间");
106         sheet.setColumnWidth(3, 20*200);//设置列宽
107         cell.setCellStyle(style);
108         
109         /*
110          * 第五步:--------------------
111          */
112         //写入实体数据(此处为测试数据)
113         List<UserPojo> list = ExcelUtil.getUserPojo();
114         
115         for (int i = 0; i < list.size(); i++) {
116             //一组数据,新增一行
117             row = sheet.createRow((int) i + 1);
118             UserPojo up = list.get(i);
119             
120             //接下来。。。将值放进去(重复第四步)
121             row.createCell((short) 0).setCellValue(up.getUid());
122             row.createCell((short) 1).setCellValue(up.getUname());
123             row.createCell((short) 2).setCellValue(up.getUpass());
124             row.createCell((short) 3).setCellValue(up.getUdate());
125             row.getCell(0).setCellStyle(style); // 循环每一行的第0列
126             row.getCell(1).setCellStyle(style); // 循环每一行的第1列
127             row.getCell(2).setCellStyle(style); // 循环每一行的第2列
128             row.getCell(3).setCellStyle(style); // 循环每一行的第3列
129 
130         }
131         
132         /*
133          * 第六步:将新生成的文件通过IO存在电脑的指定位置
134          */
135         try {
136             //创建一个输出流
137             FileOutputStream fos = new FileOutputStream("D:/Test.xls");
138             //写入文件
139             web.write(fos);
140             //将缓冲区的内容写入文件
141             fos.flush();
142             //关闭流,这里想说,如果上述写入数据之后,直接关闭(fos.close())也是可以的,Java虚拟机将会把缓冲区的数据写入文件,但是不推荐
143             fos.close();
144         } catch (Exception e) {
145              e.printStackTrace();    
146         }
147     }
148 }

下面是我的效果图:

 到此入门篇结束,给自己一个记忆。若是各位有看到不合适的地方,请联系本人。。。

 后面会发一个升级版,主要想做一个Java生成Excel文档方面的总结;

 本文参考了博文:https://blog.csdn.net/u014621859/article/details/54944059

版权声明:本文为博主原创文章,未经博主允许不得转载

文章地址: http://www.cnblogs.com/hotspring/

猜你喜欢

转载自www.cnblogs.com/hotspring/p/9140930.html
今日推荐