大概流程分7步:
1.創建工作簿 -->
2.創建sheet表 -->
3.創建row行(建議使用循環) -->
4.用row行逐一創建單元格(建議使用循環) -->
5.單元格內填充自己的數據並設置樣式 -->
6.其他設置(合並單元格、凍結行列、設置列寬等) -->
7.輸出excel文件 -->
下面開始流程:
提前准備好依賴:
<!--操作excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
1.創建工作簿-->
//創建一個工作薄 SXSSFWorkbook workbook = new SXSSFWorkbook();
2.創建sheet表-->
//創建一個sheet SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(); //設置sheet頁長度限制,-1則不限制長度 sheet.setRandomAccessWindowSize(-1);
3.創建row行-->
//創建第1行,從0開始算 Row row0 = sheet.createRow(0); //創建第2行 Row row1 = sheet.createRow(1); //創建第3行 Row row2 = sheet.createRow(2); //創建第4行 Row row3 = sheet.createRow(3); //以此類推,建議根據List內容使用循環創建
4.循環創建cell單元格-->
//在剛剛創建的第1行 “row0” 的基礎上創建4個單元格並且設置內容,這里手動創建來演示 //實際中使用中應該使用循環來進行創建 Cell cell0 = row0.createCell(0); cell0.setCellValue("我是單元格的內容1"); Cell cell1 = row0.createCell(1); cell1.setCellValue("我是單元格的內容2"); Cell cell2 = row0.createCell(2); cell2.setCellValue("我是單元格的內容3"); Cell cell3 = row0.createCell(3); cell3.setCellValue("我是單元格的內容4");
5.寫入自己的數據、樣式、字體 等設置信息、、、
//表中要幾種樣式,或幾種字體,提前創建好,設置單元格的時候就可以直接拿來用了
//創建樣式:
CellStyle cellStyle = workbook.createCellStyle();
//水平居中 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //上下居中 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); //設置字體 Font font1 = workbook.createFont(); font1.setFontName("黑體"); font1.setFontHeightInPoints((short) 16); //字體大小
XSSFFont font2 = wb.createFont();
font2.setFontName(“仿宋_GB2312”);
font2.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);
//將設置好的字體放入剛創建的樣式中
cellStyle.setFont(font1);
//使用樣式: 在需要設置的單元格中設置cell.setCellStyle(里面放剛創建的樣式);
6.合並想合並的單元格、凍結首行等、、、
//合並單元格的方法(根據自己需要,每次合並都需要分別設置四個屬性) //sheet.addMergedRegion(new CellRangeAddress(起始行號, 結束行號, 其實列號, 結束列號)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 5));
//凍結第一行
sheet.createFreezePane(0,1,0,1);
//分別設置列寬,參數1:列號 參數2:寬度值
sheet.setColumnWidth(0,3000);
sheet.setColumnWidth(1,3000);
sheet.setColumnWidth(2,3000);
sheet.setColumnWidth(3,5000);
7.用輸出流輸出excel文件
//輸出文件
//創建文件輸出流
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\new5\\autoMation\\resource\\test.xlsx"));
//用最開始創建的工作簿.write進行文件寫出 workbook.write(fileOutputStream);
下面是完整的簡單流程:
准備一個實體類,方便輸出:
public class People { public String name; public String sex; public String age; public String phone; public People() { } public People(String name, String sex, String age, String phone) { this.name = name; this.sex = sex; this.age = age; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
生成excel的代碼:
public class OutExcel {
public static void main(String[] args) throws IOException, IllegalAccessException {
ArrayList<People> dataList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
People people = new People("姓名" + i, i + "", "男", "12345678911");
dataList.add(people);
doExceloneThread(dataList);
}
}
public static void doExceloneThread(ArrayList<People> dataList) throws IllegalAccessException, IOException {
//創建一個工作薄
SXSSFWorkbook workbook = new SXSSFWorkbook();
//創建一個sheet
SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet();
//設置sheet頁長度限制
sheet.setRandomAccessWindowSize(-1);
//-------------------------------------------------------
//表中要幾種樣式,或幾種字體,提前創建好,設置單元格的時候就可以直接設置了
//創建樣式
CellStyle cellStyle = workbook.createCellStyle();
//水平居中
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//上下居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
//設置邊框
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下邊框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左邊框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上邊框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右邊框
//設置字體
Font font = workbook.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16); //字體大小
//將字體設置放入樣式中
cellStyle.setFont(font);
//創建第一行.后面需要合並某些單元格
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("哦哦哦");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("");
cell1.setCellStyle(cellStyle);
Cell cell2 = row0.createCell(2);
cell2.setCellValue("哈哈哈");
cell2.setCellStyle(cellStyle);
Cell cell3 = row0.createCell(3);
cell3.setCellValue("哈哈哈");
cell3.setCellStyle(cellStyle);
//-------------------------------------------
//遍歷集合中的數據
//也可以采用自己的方法,總之就是將數據放到一個一個的cell(單元格)中,
Iterator<People> iterator = dataList.iterator();
//反射
Field[] fields = People.class.getDeclaredFields();
Integer currentRow = 1;
CellStyle cellStyle2 = workbook.createCellStyle();
//上下左右居中
cellStyle2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
cellStyle2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
cellStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下邊框
cellStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左邊框
cellStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);//上邊框
cellStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);//右邊框
while (iterator.hasNext()) {
People people = iterator.next();
Row row = sheet.createRow(currentRow);
for (int i = 0; i < fields.length; i++) {
Cell cell = row.createCell(i);
if (currentRow == 1){
cell.setCellValue(fields[i].getName());
}else{
cell.setCellValue(String.valueOf(fields[i].get(people)));
}
cell.setCellStyle(cellStyle2);
}
currentRow++;
}
//合並單元格的方法
//sheet.addMergedRegion(new CellRangeAddress(起始行號, 結束行號, 其實列號, 結束列號));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 3));
//凍結第一行
sheet.createFreezePane(0,1,0,1);
//設置列寬,參數1:列號 參數2:寬度值
sheet.setColumnWidth(0,3000);
sheet.setColumnWidth(1,3000);
sheet.setColumnWidth(2,3000);
sheet.setColumnWidth(3,5000);
//輸出文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\new5\\autoMation\\resource\\test.xlsx"));
workbook.write(fileOutputStream);
}
}
