zoukankan      html  css  js  c++  java
  • 045 Java中数据导入到excel

      程序有些参考,不过重要的是思路。

    1.导入依赖

     1 <dependencies>
     2         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
     3         <dependency>
     4             <groupId>org.apache.poi</groupId>
     5             <artifactId>poi</artifactId>
     6             <version>4.0.1</version>
     7         </dependency>
     8         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
     9         <dependency>
    10             <groupId>org.apache.poi</groupId>
    11             <artifactId>poi-ooxml</artifactId>
    12             <version>4.0.1</version>
    13         </dependency>
    14 
    15     </dependencies>

    3.程序

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

    4.结果

      

  • 相关阅读:
    Javascript函数的简单学习
    JAVA学习绘图颜色及其笔画属性设置字体显示文字
    JAVA学习路线图
    JAVA学习AWT绘图
    JAVA学习Swing章节按钮组件JButton的简单学习
    JAVA学习Swing章节JPanel和JScrollPane面板的简单学习
    JAVA学习Swing章节流布局管理器简单学习
    JAVA学习Swing绝对局部简单学习
    JAVA学习Swing章节标签JLabel中图标的使用
    JAVA学习中Swing部分JDialog对话框窗体的简单学习
  • 原文地址:https://www.cnblogs.com/juncaoit/p/10155773.html
Copyright © 2011-2022 走看看