第一個版本,基礎(chǔ)版
package org.ctzk.jcpc.utils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.ctzk.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* com.lzctzk.guidepost.util
* excel操作工具類
* @author luozhen
* @version V1.0
* @date 2020-2-1 17:56
* @description
*/
public class ExcelUtilOld {
private static final Logger log = LoggerFactory.getLogger(ExcelUtilOld.class);
private static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 獲取xlsx文件內(nèi)容某個sheet(從0開始)的內(nèi)容食呻,以二維數(shù)組形式返回
*
* @param fileAbsolutePath xlsx文件的絕對路徑
* @return xlsx文件的文本內(nèi)容
*/
public static List<List<String>> getExcelData(String fileAbsolutePath) {
/*
* workbook:工作簿,就是整個Excel文檔
* sheet:工作表
* row:行
* cell:單元格
*/
File file = new File(fileAbsolutePath);
InputStream inputStream = null;
//默認從第一個sheet開始讀取
int bookIndex = 0;
try {
//文件
inputStream = new FileInputStream(file);
Workbook book = null;
if (fileAbsolutePath.endsWith(".xlsx")) {
book = new XSSFWorkbook(inputStream);
} else if (fileAbsolutePath.endsWith(".xls")) {
book = new HSSFWorkbook(inputStream);
}
if (bookIndex >= book.getNumberOfSheets()) {
log.error("獲取表格數(shù)據(jù)錯誤: bookIndex={}伶唯,sheet的下標超出范圍 ", bookIndex);
return null;
}
//sheet從0開始
Sheet sheet = book.getSheetAt(bookIndex);
//取得最后一行的行號
int rowNum = sheet.getLastRowNum() + 1;
//每行的最后一個單元格位置(默認選取第一行的長度)
int cellNum = sheet.getRow(0).getLastCellNum();
//創(chuàng)建二維數(shù)組保存所有讀取到的行列數(shù)據(jù)疙教,外層存行數(shù)據(jù)聋溜,內(nèi)層存單元格數(shù)據(jù)
List<List<String>> lists = new ArrayList<List<String>>();
//設(shè)置讀取開始的行數(shù)推正,從0(第一行)開始
//第三層
int startIndex = 2;
//開始進行讀取
for (int rowIndex = startIndex; rowIndex < rowNum; rowIndex++) {
//行
Row row = sheet.getRow(rowIndex);
if (null != row) {
List<String> cellList = new ArrayList<String>();
for (int cellIndex = 0; cellIndex < cellNum; cellIndex++) {
Cell cell = row.getCell(cellIndex);
cellList.add(cellValueFormat(cell));
}
lists.add(cellList);
}
}
return lists;
} catch (FileNotFoundException fe) {
log.error("獲取表格數(shù)據(jù)錯誤: FileNotFoundException", fe);
} catch (IOException ie) {
log.error("獲取表格數(shù)據(jù)錯誤: IOException", ie);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (Exception e) {
log.error("獲取表格數(shù)據(jù)關(guān)閉IO流異常", e);
}
}
}
return null;
}
/**
* 表格數(shù)據(jù)格式化
* @param cell
* @return
*/
private static String cellValueFormat(Cell cell){
String cellValue = null;
if(null != cell){
switch (cell.getCellType()){
case _NONE:
break;
//數(shù)值型
case NUMERIC:
//判斷是日期類型
if (HSSFDateUtil.isCellDateFormatted(cell)) {
//獲取成DATE類型
Date dt = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
cellValue = dateformat.format(dt);
}else{
//轉(zhuǎn)化電話號碼和身份證號碼為字符串
DecimalFormat df = new DecimalFormat("#");
cellValue = String.valueOf(df.format(cell.getNumericCellValue()));
}
break;
//字符串型
case STRING:
cellValue = cell.getStringCellValue();
break;
//空值
case BLANK:
break;
//布爾型
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
//公式型
case FORMULA:
cellValue = cell.getCellFormula();
break;
//錯誤
case ERROR:
break;
default:
}
}
return cellValue;
}
/**
* 保存上傳的文件
* @param multipartFile
* @return
* @throws Exception
*/
public static String keepExcel(MultipartFile multipartFile)throws Exception{
//文件參數(shù)整理
//原始文件名
String myFileName = multipartFile.getOriginalFilename();
//文件大小
String fileSize = multipartFile.getSize() + "";
//文件名
String fileNme = myFileName.substring(0, myFileName.lastIndexOf("."));
//獲取文件的后綴名
String fileType = myFileName.substring(myFileName.lastIndexOf(".") + 1);
//存儲文件名(加上時間是為了保證文件的唯一名稱)
String saveName = fileNme + "-" + DateUtil.getStringAllDate();
//存儲文件全名稱(壓縮圖片文件名)
String saveFullName = saveName + "." + fileType;
//創(chuàng)建存儲文件夾
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String date = sdf.format(new Date());
//存儲文件夾路徑
//String fileFolder = "E:/Collection/files/" + date;
String fileFolder = "/var/files/" + date;
//相對存儲路徑
String fileFpfName = fileFolder + "/" + saveFullName;
File folder = new File(fileFolder);
if (!folder.exists()) {
folder.mkdirs();
}
//創(chuàng)建文件
String filePath = "";
filePath = fileFolder + "/" + saveFullName;
File diskFile = new File(filePath);
//寫入文件
multipartFile.transferTo(diskFile);
//創(chuàng)建文件 不用檢查文件是否存在葛圃,UUID能保證文件名的不重復性
diskFile.createNewFile();
return fileFpfName;
}
}