一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格示例代碼

Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格示例代碼

2020-06-26 16:22daisy JAVA教程

最近工作中遇到一個(gè)需求,是需要導(dǎo)出數(shù)據(jù)到Excel表格里,所以寫個(gè)Demo測(cè)試一下,還是比較簡(jiǎn)單的,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧。

介紹

Jakarta POI 是一套用于訪問微軟格式文檔的Java API。Jakarta POI有很多組件組成,其中有用于操作Excel格式文件的HSSF和用于操作Word的HWPF,在各種組件中目前只有用于操作Excel的HSSF相對(duì)成熟。官方主頁http://poi.apache.org/index.html,API文檔http://poi.apache.org/apidocs/index.html

實(shí)現(xiàn)

已經(jīng)在代碼中加入了完整的注釋。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
public class ExcelOperate {
 
  public static void main(String[] args) {
    // 創(chuàng)建Excel表格
    createExcel(getStudent());
 
    // 讀取Excel表格
    List<Student> list = readExcel();
    System.out.println(list.toString());
  }
 
  /**
   * 初始化數(shù)據(jù)
   *
   * @return 數(shù)據(jù)
   */
  private static List<Student> getStudent() {
    List<Student> list = new ArrayList<Student>();
    Student student1 = new Student("小明", 8, "二年級(jí)");
    Student student2 = new Student("小光", 9, "三年級(jí)");
    Student student3 = new Student("小花", 10, "四年級(jí)");
    list.add(student1);
    list.add(student2);
    list.add(student3);
    return list;
  }
 
  /**
   * 創(chuàng)建Excel
   *
   * @param list
   *      數(shù)據(jù)
   */
  private static void createExcel(List<Student> list) {
    // 創(chuàng)建一個(gè)Excel文件
    HSSFWorkbook workbook = new HSSFWorkbook();
    // 創(chuàng)建一個(gè)工作表
    HSSFSheet sheet = workbook.createSheet("學(xué)生表一");
    // 添加表頭行
    HSSFRow hssfRow = sheet.createRow(0);
    // 設(shè)置單元格格式居中
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 
    // 添加表頭內(nèi)容
    HSSFCell headCell = hssfRow.createCell(0);
    headCell.setCellValue("姓名");
    headCell.setCellStyle(cellStyle);
 
    headCell = hssfRow.createCell(1);
    headCell.setCellValue("年齡");
    headCell.setCellStyle(cellStyle);
 
    headCell = hssfRow.createCell(2);
    headCell.setCellValue("年級(jí)");
    headCell.setCellStyle(cellStyle);
 
    // 添加數(shù)據(jù)內(nèi)容
    for (int i = 0; i < list.size(); i++) {
      hssfRow = sheet.createRow((int) i + 1);
      Student student = list.get(i);
 
      // 創(chuàng)建單元格,并設(shè)置值
      HSSFCell cell = hssfRow.createCell(0);
      cell.setCellValue(student.getName());
      cell.setCellStyle(cellStyle);
 
      cell = hssfRow.createCell(1);
      cell.setCellValue(student.getAge());
      cell.setCellStyle(cellStyle);
 
      cell = hssfRow.createCell(2);
      cell.setCellValue(student.getGrade());
      cell.setCellStyle(cellStyle);
    }
 
    // 保存Excel文件
    try {
      OutputStream outputStream = new FileOutputStream("D:/students.xls");
      workbook.write(outputStream);
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * 讀取Excel
   *
   * @return 數(shù)據(jù)集合
   */
  private static List<Student> readExcel() {
    List<Student> list = new ArrayList<Student>();
    HSSFWorkbook workbook = null;
 
    try {
      // 讀取Excel文件
      InputStream inputStream = new FileInputStream("D:/students.xls");
      workbook = new HSSFWorkbook(inputStream);
      inputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
 
    // 循環(huán)工作表
    for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
      HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
      if (hssfSheet == null) {
        continue;
      }
      // 循環(huán)行
      for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
        HSSFRow hssfRow = hssfSheet.getRow(rowNum);
        if (hssfRow == null) {
          continue;
        }
 
        // 將單元格中的內(nèi)容存入集合
        Student student = new Student();
 
        HSSFCell cell = hssfRow.getCell(0);
        if (cell == null) {
          continue;
        }
        student.setName(cell.getStringCellValue());
 
        cell = hssfRow.getCell(1);
        if (cell == null) {
          continue;
        }
        student.setAge((int) cell.getNumericCellValue());
 
        cell = hssfRow.getCell(2);
        if (cell == null) {
          continue;
        }
        student.setGrade(cell.getStringCellValue());
 
        list.add(student);
      }
    }
    return list;
  }
}

附上Student類的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Student {
 
  private String name;
  private int age;
  private String grade;
 
  public Student() {
  }
 
  public Student(String name, int age, String grade) {
    super();
    this.name = name;
    this.age = age;
    this.grade = grade;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public int getAge() {
    return age;
  }
 
  public void setAge(int age) {
    this.age = age;
  }
 
  public String getGrade() {
    return grade;
  }
 
  public void setGrade(String grade) {
    this.grade = grade;
  }
 
  @Override
  public String toString() {
    return "Student [name=" + name + ", age=" + age + ", grade=" + grade
        + "]";
  }
}

測(cè)試結(jié)果

導(dǎo)出的Excel表格

Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格示例代碼

students

打印讀取的Excel數(shù)據(jù)

?
1
[Student [name=小明, age=8, grade=二年級(jí)], Student [name=小光, age=9, grade=三年級(jí)], Student [name=小花, age=10, grade=四年級(jí)]]

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 97伊人久久精品亚洲午夜 | 亚洲国产成人久久综合一 | 四虎国产精品视频免费看 | 无遮挡h肉动漫高清在线 | 欧美成人免费tv在线播放 | 男男gaygays18中国 | 特黄aa级毛片免费视频播放 | 国色天香高清版 | 777午夜精品免费播放 | 免费午夜网站 | 久久电影午夜 | 海派甜心完整版在线观看 | 69日本人xxxxxxxx色 | 色综合图片| 免费视频精品一区二区三区 | 青草青草久热精品视频在线网站 | 欧美视频一二三区 | a男人的天堂久久a毛片 | 日韩国产成人资源精品视频 | 精品福利视频一区二区三区 | 特黄特黄一级片 | 99在线精品日韩一区免费国产 | 国产亚洲视频网站 | japan日韩xxxx69hd japanese在线观看 | 天天干夜夜拍 | 99热最新 | 精品国产综合 | 国产福利自产拍在线观看 | 亚洲国产精品久久卡一 | 女人爽到喷水的视频免费 | 精品无人乱码一区二区三区 | 日韩一级精品视频在线观看 | 女人把扒开给男人爽的 | 女人张开腿让男人桶视频免费大全 | 国产一区二区在线看 | 青青草成人影院 | 色漫在线观看 | 爱爱调教| 男生操女生动态图 | 我半夜摸妺妺的奶C了她软件 | 天天干夜夜拍 |