POI简单初识 Demo (资源来自网络本人属于收藏总结)

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.ArrayList;
 7 import java.util.ArrayList;
 8 import java.util.Date;
 9 import java.util.List;
10 import org.apache.poi.xssf.usermodel.XSSFRow;
11 import org.apache.poi.xssf.usermodel.XSSFSheet;
12 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
13 
14 public class testPOI {
15 
16     /**
17      * @param args
18      * @throws IOException 
19      */
20     public static void main(String[] args) throws IOException {
21         // TODO Auto-generated method stub
22          SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMDDhhmmss");
23          String now = dateFormat.format(new Date());
24          System.out.println(now);
25          String basePath = "C:\Users\hzxx\Desktop";    //文件位置
26          String exportFileName = "\数据_"+now+".xlsx";//文件名字
27          String[] cellTitle = {"序号","姓名","学号","性别","入学日期"};
28             //需要导出的数据
29             List<String[]> dataList = new ArrayList<String[]>();
30             dataList.add(new String[]{"东邪","17232401001","男","2015年9月"});
31             dataList.add(new String[]{"西毒","17232401002","女","2016年9月"});
32             dataList.add(new String[]{"南帝","17232401003","男","2017年9月"});
33             dataList.add(new String[]{"北丐","17232401004","男","2015年9月"});
34             dataList.add(new String[]{"中神通","17232401005","女","2017年9月"});
35          // 声明一个工作薄
36             XSSFWorkbook workBook = null;
37             workBook = new XSSFWorkbook();
38             // 生成一个表格
39             XSSFSheet sheet = workBook.createSheet();
40             workBook.setSheetName(0,"学生信息");
41             // 创建表格标题行 第一行
42             XSSFRow titleRow = sheet.createRow(0);
43             for(int i=0;i<cellTitle.length;i++){
44                 titleRow.createCell(i).setCellValue(cellTitle[i]);
45             }
46             //插入需导出的数据
47             for(int i=0;i<dataList.size();i++){
48                 XSSFRow row = sheet.createRow(i+1);
49                 row.createCell(0).setCellValue(i+1);
50                 row.createCell(1).setCellValue(dataList.get(i)[0]);
51                 row.createCell(2).setCellValue(dataList.get(i)[1]);
52                 row.createCell(3).setCellValue(dataList.get(i)[2]);
53                 row.createCell(4).setCellValue(dataList.get(i)[3]);
54             }
55             File  file = new File(basePath+exportFileName);
56             //文件输出流
57             FileOutputStream outStream = new FileOutputStream(file);
58             workBook.write(outStream);
59             outStream.flush();
60             outStream.close();
61             System.out.println("导出2007文件成功!文件导出路径:--"+basePath+exportFileName);
62     }
63 
64 }

输出效果

原文地址:https://www.cnblogs.com/zhbx/p/8550184.html