POI-生成Excel文件

  1 import java.io.FileOutputStream;
  2 import java.util.ArrayList;
  3 import java.util.List;
  4 
  5 import org.apache.poi.hssf.usermodel.HSSFCell;
  6 import org.apache.poi.hssf.usermodel.HSSFRow;
  7 import org.apache.poi.hssf.usermodel.HSSFSheet;
  8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9 import org.poi.pojo.User;
 10 
 11 /**
 12  * POI导出excel Created by Ay on 2016/6/14. <dependency>
 13  * <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId>
 14  * <version>3.14</version> </dependency>
 15  */
 16 public class PoiToExcel {
 17 
 18     public static void main(String[] args) {
 19 
 20         List<String> arrayList = new ArrayList<String>();
 21         arrayList.add("编号");
 22         arrayList.add("姓名");
 23         arrayList.add("年龄");
 24         arrayList.add("性别");
 25         arrayList.add("手机号码");
 26         arrayList.add("居住地址");
 27         arrayList.add("工作地点");
 28         List<User> arrayList1 = new ArrayList<User>();
 29 
 30         for (int i = 0; i < 65535; i++) {
 31             User user = new User(i, "梁家强", 23 + i, "男", 18888888888L, "甘肃平凉", "陕西西安");
 32             arrayList1.add(user);
 33         }
 34 
 35         // 新建excel报表
 36         HSSFWorkbook excel = new HSSFWorkbook();
 37         // 添加一个sheet,名字叫"测试表"
 38         for (int q = 0; q < 8; q++) {
 39             HSSFSheet hssfSheet = excel.createSheet(q + "");
 40             HSSFRow hssfRow = hssfSheet.createRow(0);
 41             for (int j = 0; j < arrayList.size(); j++) {
 42                 // 设置列宽 第一个参数是索引,从零开始
 43                 hssfSheet.setColumnWidth(j, 20 * 256);
 44                 // 创建列,
 45                 HSSFCell hssfCell = hssfRow.createCell(j);
 46                 // 往列里增加内容
 47                 hssfCell.setCellValue(arrayList.get(j));
 48 
 49             }
 50 
 51             for (int i = 0; i < arrayList1.size(); i++) {
 52                 HSSFRow hssfRow1 = hssfSheet.createRow(i + 1);
 53 
 54                 for (int k = 0; k < arrayList.size(); k++) {
 55                     HSSFCell hssfCell1 = hssfRow1.createCell(k);
 56                     switch (k) {
 57                     case 0:
 58                         hssfCell1.setCellValue(arrayList1.get(i).getId());
 59                         break;
 60                     case 1:
 61                         hssfCell1.setCellValue(arrayList1.get(i).getName());
 62                         break;
 63                     case 2:
 64                         hssfCell1.setCellValue(arrayList1.get(i).getAge());
 65                         break;
 66                     case 3:
 67                         hssfCell1.setCellValue(arrayList1.get(i).getSex());
 68                         break;
 69                     case 4:
 70                         hssfCell1.setCellValue(arrayList1.get(i).getPhoneNum());
 71                         break;
 72                     case 5:
 73                         hssfCell1.setCellValue(arrayList1.get(i).getHomeAddress());
 74                         break;
 75                     case 6:
 76                         hssfCell1.setCellValue(arrayList1.get(i).getCompanyAddress());
 77                         break;
 78                     }
 79 
 80                 }
 81 
 82             }
 83 
 84         }
 85 
 86         // 往excel表格创建一行,excel的行号是从0开始的
 87 
 88         // 设置行高
 89         // hssfRow.setHeight((short) (20 * 50));
 90 
 91         FileOutputStream fout = null;
 92         try {
 93             // 用流将其写到D盘
 94             fout = new FileOutputStream("F:/students.xls");
 95             excel.write(fout);
 96             fout.close();
 97         } catch (Exception e) {
 98             e.printStackTrace();
 99         }
100 
101     }
102 }

 

posted @ 2018-02-26 16:56  游园复梦  阅读(151)  评论(0编辑  收藏  举报