在實際項目中可能出現(xiàn)將數(shù)據(jù)生成Excel表格,然后導(dǎo)入到本地?;蛘呤褂肊xcel模板將數(shù)據(jù)導(dǎo)出,這個或許在項目中比較常用。
認(rèn)識Excel表格

image.png
使用POI創(chuàng)建Excel
通過Apache POI文檔可以清楚的了解到如何去創(chuàng)建一個Excel表格。
* Excel 2003: HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell, etc.(對于2003)
* Excel 2007: XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell, etc.(對于2007)
1.HSSFWorkbook : 相當(dāng)于一個excel文件
Workbook wb = new HSSFWorkbook();
2.HSSFSheet:一張excel表,excel左下角的sheet0,sheet1..
Sheet sheet1 = wb.createSheet("new sheet");//參數(shù)new sheet是工作表的表名
3.HSSFRow:一張表格中的某一行
Row row = sheet.createRow((short)0);//參數(shù)表示創(chuàng)建第幾行,索引從0開始
4.創(chuàng)建一個單元格
HSSFCell cell = row.createCell(0);//參數(shù)表示創(chuàng)建第幾列,索引從0開始
到這里就可以新建一個簡單的Excel表格了。
寫入數(shù)據(jù)到Excel并導(dǎo)出
private static void createExcel(ArrayList<String> list) {
//獲取當(dāng)前時間
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//設(shè)置日期格式
String date = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間,也可使用當(dāng)前時間戳
//存儲路徑--獲取桌面位置
FileSystemView view = FileSystemView.getFileSystemView();
File directory = view.getHomeDirectory();
System.out.println(directory);
//存儲Excel的路徑
String path = directory+"\\"+date+".xlsx";
System.out.println(path);
try {
//定義一個Excel表格
XSSFWorkbook wb = new XSSFWorkbook(); //創(chuàng)建工作薄
XSSFSheet sheet = wb.createSheet("sheet1"); //創(chuàng)建工作表
XSSFRow row = sheet.createRow(0); //行
XSSFCell cell; //單元格
//添加表頭數(shù)據(jù)
for (int i = 0; i < list.size(); i++) {
//從前端接受到的參數(shù)封裝成list集合,然后遍歷下標(biāo)從而取出對應(yīng)的值
String value = list.get(i);
//將取到的值依次寫到Excel的第一行的cell中
row.createCell(i).setCellValue(value);
}
//輸出流,下載時候的位置
// FileWriter outputStream1 = new FileWriter(path);
FileOutputStream outputStream = new FileOutputStream(path);
wb.write(outputStream);
outputStream.flush();
outputStream.close();
System.out.println("寫入成功");
} catch (Exception e) {
System.out.println("寫入失敗");
e.printStackTrace();
}
}
讀出Excel表格
/**
* 從指定路徑讀取Excel文件,返回類型為List<Map<String,String>>
*
* @param path
* @throws IOException
*/
private static ArrayList<Map<String, String>> readExcel(String path) throws Exception {
ArrayList<Map<String, String>> mapList = new ArrayList<>();
File file = new File(path);
//判斷文件是否存在
if (file.isFile() && file.exists()) {
System.out.println(file.getPath());
//獲取文件的后綴名 \\ .是特殊字符
String[] split = file.getName().split("\\.");
System.out.println(split[1]);
Workbook wb;
//根據(jù)文件后綴(xls/xlsx)進(jìn)行判斷
if ("xls".equals(split[1])) {
// //獲取文件流對象
FileInputStream inputStream = new FileInputStream(file);
wb = new HSSFWorkbook(inputStream);
}else if ("xlsx".equals(split[1])){
wb = new XSSFWorkbook(file);
}else {
System.out.println("文件類型錯誤");
return null;
}
//開始解析
Sheet sheet = wb.getSheetAt(0);
//第一行是列名,所以從第二行開始遍歷
int firstRowNum = sheet.getFirstRowNum() + 1;
int lastRowNum = sheet.getLastRowNum();
//遍歷行
for (int rIndex = firstRowNum; rIndex <= lastRowNum; rIndex++) {
Map map =new HashMap();
//獲取當(dāng)前行的內(nèi)容
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellNum = row.getFirstCellNum();
int lastCellNum = row.getLastCellNum();
for (int cIndex = firstCellNum; cIndex < lastCellNum; cIndex++) {
row.getCell(cIndex).setCellType(Cell.CELL_TYPE_STRING);
//獲取單元格的值
String value = row.getCell(cIndex).getStringCellValue();
System.out.println(value);
//獲取此單元格對應(yīng)第一行的值
String key = sheet.getRow(0).getCell(cIndex).getStringCellValue();
System.out.println(key);
//第一行中的作為鍵,第n行的作為值
map.put(key, value);
System.out.println(map);
}
}
mapList.add(map);
System.out.println("讀取成功");
System.out.println(mapList);
}
}
return mapList;
}
這就使用POI對Excel的基本操作,還有更多的復(fù)雜的操作請參考官方文檔。
可能需要添加的包
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>