?1.常用場景
(1)將用戶數(shù)據(jù)導(dǎo)出為excel表格(導(dǎo)出數(shù)據(jù))
(2)將Excel表中的數(shù)據(jù)導(dǎo)出到網(wǎng)站數(shù)據(jù)庫中
操作Excel最常用的就是Apache的POI和阿里巴巴的easyExcel。
2.官網(wǎng)地址
Apache POI的官網(wǎng):https://poi./
easyExcel的官網(wǎng):https://github.com/alibaba/easyexcel
3.poi和easyExecel的區(qū)別:
以下是我在官網(wǎng)截的圖:

?
?
?
?4.Excel中的對象
? ? 在java中萬物皆對象,Excel也不例外,Excel中的對象有:
? ? 工作簿:Workbook(是一個接口)
? ? (使用時需要導(dǎo)入Workbook:import org.apache.poi.ss.usermodel.Workbook;)
? ?Workbook的三個實現(xiàn)類:
(1)HSSFWorkbook:HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴展名是.xls; (2)XSSFWorkbook:是操作Excel2007的版本,擴展名是.xlsx; (3)SXSSFworkbook:是XSSFWorkbook的升級版,當(dāng)數(shù)據(jù)量超出65536條后,在使用HSSFWorkbook或XSSFWorkbook,程序會報OutOfMemoryError:Javaheap space;內(nèi)存溢出錯誤。這時應(yīng)該用SXSSFworkbook。
工作表:Sheet (使用時需要導(dǎo)入Sheet:import org.apache.poi.ss.usermodel.Sheet;)
? ? 行:Row? ? ?(使用時需要導(dǎo)入Row:import org.apache.poi.ss.usermodel.Row;)
? ? 列:Cell??(使用時需要導(dǎo)入Cell??:import org.apache.poi.ss.usermodel.Cell;)
?5.03版Excel表的生成代碼:
package com.lqz.controller;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author:liqinzhen
* @Date:2020/6/9
* @Description:
*/
public class ExcelWriteTest {
String path = "E:\\java\\project\\springmvc-ajax-json";
@Test
public void test() throws IOException {
//1.創(chuàng)建一個工作簿
Workbook workbook = new HSSFWorkbook();
//2.創(chuàng)建一個工作表,因為工作表在工作簿中,所以用工作簿創(chuàng)建工作表
Sheet sheet = workbook.createSheet("學(xué)生表");
//3.創(chuàng)建一個 行
Row row1 = sheet.createRow(0);
//4.創(chuàng)建一個單元格
Cell cell11 = row1.createCell(0);
//給單元格設(shè)置值
cell11.setCellValue("陳咬金");;
//創(chuàng)建第二個單元格
Cell cell12 = row1.createCell(1);
cell12.setCellValue("1021");
//創(chuàng)建第二行
Row row2 = sheet.createRow(1);
//創(chuàng)建第二行的第一個單元格
Cell cell21 = row2.createCell(0);
cell21.setCellValue("羋月");
//創(chuàng)建第二行的第二個單元格
Cell cell22 = row2.createCell(1);
cell22.setCellValue("1023");
//生成一張表(io流),03版的Excel使用.xls結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(path "學(xué)生表03.xls");
//輸出
workbook.write(fileOutputStream);
//關(guān)閉流
fileOutputStream.close();
System.out.println("學(xué)生表03.xls表生成完畢");
}
}
6.07版的Excel生成代碼:
//(1)導(dǎo)入少量數(shù)據(jù) package com.lqz.controller;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author:liqinzhen
* @Date:2020/6/9
* @Description:
*/
public class ExcelWriteTest {
String path = "E:\\java\\project\\springmvc-ajax-json";
@Test
public void test() throws IOException {
//1.創(chuàng)建一個工作簿
Workbook workbook = new XSSFWorkbook();
//2.創(chuàng)建一個工作表,因為工作表在工作簿中,所以用工作簿創(chuàng)建工作表
Sheet sheet = workbook.createSheet("學(xué)生表");
//3.創(chuàng)建一個 行
Row row1 = sheet.createRow(0);
//4.創(chuàng)建一個單元格
Cell cell11 = row1.createCell(0);
//給單元格設(shè)置值
cell11.setCellValue("陳咬金");;
//創(chuàng)建第二個單元格
Cell cell12 = row1.createCell(1);
cell12.setCellValue("1021");
//創(chuàng)建第二行
Row row2 = sheet.createRow(1);
//創(chuàng)建第二行的第一個單元格
Cell cell21 = row2.createCell(0);
cell21.setCellValue("羋月");
//創(chuàng)建第二行的第二個單元格
Cell cell22 = row2.createCell(1);
cell22.setCellValue("1023");
//生成一張表(io流),03版的Excel使用.xls結(jié)尾
FileOutputStream fileOutputStream = new FileOutputStream(path "學(xué)生表07.xls");
//輸出
workbook.write(fileOutputStream);
//關(guān)閉流
fileOutputStream.close();
System.out.println("學(xué)生表07.xls表生成完畢");
}
}
//(2)批量數(shù)據(jù)導(dǎo)入
@Test
public void test02() throws IOException {
String path = "E:\\java\\project\\springmvc-ajax-json";
//1.創(chuàng)建一個工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//2.創(chuàng)建一個工作表
HSSFSheet sheet = workbook.createSheet();
for (int rowNum = 0;rowNum<65536;rowNum ){
//3.創(chuàng)建行
Row row = sheet.createRow(rowNum);
for (int cellNum = 0;cellNum <5;cellNum ){
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("寫入完畢");
FileOutputStream fileOutputStream = new FileOutputStream(path "工作表03.xls");
workbook.write(fileOutputStream);
//關(guān)閉流
fileOutputStream.close();
}
//(3)讀取Excel文件 @Test
public void readExcelTest() throws IOException {
//獲取文件流
FileInputStream fileInputStream = new FileInputStream(path "學(xué)生表03.xls");
//創(chuàng)建一個工作簿
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
//得到表
Sheet sheet = workbook.getSheetAt(0);
//得到行
Row row = sheet.getRow(0);
//得到列
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
fileInputStream.close();
}
//讀取不同數(shù)據(jù)類型 @Test
public void testCellType() throws IOException {
//獲取文件流
FileInputStream fileInputStream = new FileInputStream(path "學(xué)生表03.xls");
//創(chuàng)建一個工作簿
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet sheet = workbook.getSheetAt(0);
//獲取標(biāo)題,表的第一行
Row rowTile = sheet.getRow(0);
//如果第一行不為空
if (rowTile != null) {
//讀取第一行列的數(shù)量
int numberOfCells = rowTile.getPhysicalNumberOfCells();
//循環(huán)遍歷每一列
for (int i = 0; i < numberOfCells; i ) {
//獲得列
Cell cell = rowTile.getCell(i);
//如果列不為空
if (cell != null) {
//獲取列的類型
CellType cellType = cell.getCellType();
String stringCellValue = cell.getStringCellValue();
System.out.print(stringCellValue "|");
}
}
}
System.out.println();
//獲取行內(nèi)容,行的數(shù)量
int numberOfRows = sheet.getPhysicalNumberOfRows();
//循環(huán)獲取每一行
for (int i = 1; i < numberOfRows; i ) {
//獲得行
Row row = sheet.getRow(i);
if (row != null) {
//獲取列
int numberOfCells1 = rowTile.getPhysicalNumberOfCells();
for (int j = 0; j < numberOfCells1; j ) {
System.out.print("[" (j 1) (numberOfCells1 1) "]");
Cell cell = rowTile.getCell(j);
//匹配列的數(shù)據(jù)類型
if (cell != null) {
CellType cellType = cell.getCellType();
String cellValue = "";
switch (cellType) {
case STRING:
System.out.println("[STRING]");
cellValue = cell.getStringCellValue();
break;
case BOOLEAN:
System.out.println("[BOOLEAN]");
cellValue = String.valueOf(cell.getStringCellValue());
break;
case BLANK://空
System.out.println("[BOOLEAN]");
break;
case NUMERIC://空
System.out.println("[NUMERIC]");//數(shù)字(普通數(shù)字,日期)
if (HSSFDateUtil.isCellDateFormatted(cell)) {
System.out.println("[日期]");
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString("yyyy-MM-dd");
} else {
System.out.println("轉(zhuǎn)換為字符串輸出");
cell.setCellType(STRING);
cellValue = cell.toString();
}
break;
case ERROR://空
System.out.println("[數(shù)據(jù)類型錯誤]");
break;
}
System.out.println(cellValue);
}
}
}
}
//關(guān)閉流
fileInputStream.close();
}
?

?
來源:https://www./content-4-708351.html
|