This commit is contained in:
zhuo
2020-09-06 17:29:52 +08:00
parent 029d9b0ef0
commit 486cc02088
61 changed files with 714 additions and 282 deletions

View File

@@ -0,0 +1,91 @@
package net.lab1024.smartadmin.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
/**
* @author zhuoda
*/
public class SmartFileUtil extends FileUtils {
public static boolean isXmlFile(File file) {
return "xml".equalsIgnoreCase(getFileExtension(file.getName()));
}
/**
* 文件后缀名
*
* @param fullName
* @return
*/
public static String getFileExtension(String fullName) {
String fileName = new File(fullName).getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
}
/**
* 不带后缀名的文件名
*
* @param file
* @return
*/
public static String getNameWithoutExtension(String file) {
String fileName = new File(file).getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
}
public static boolean isFileExist(String filePath) {
File file = new File(filePath);
return file.exists();
}
/**
* 验证文件是否存在,如果不存在则抛出异常
*
* @param filePath
* @throws IOException
*/
public static void isFileExistThrowException(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
}
public static BufferedReader newBufferedReader(File file, Charset charset) throws FileNotFoundException {
return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
}
public static BufferedWriter newBufferedWriter(File file, Charset charset) throws FileNotFoundException {
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
}
public static boolean createParentDirs(File file) throws IOException {
File parent = file.getCanonicalFile().getParentFile();
if (parent == null) {
return false;
}
return parent.mkdirs();
}
public static boolean createNotExistParentDirFile(File file) throws IOException {
boolean createParentDirsRes = createParentDirs(file);
if (!createParentDirsRes) {
throw new IOException("cannot create parent Directory of " + file.getName());
}
return file.createNewFile();
}
}

View File

@@ -1,6 +1,6 @@
package net.lab1024.smartadmin.util;
import net.lab1024.smartadmin.module.business.login.domain.RequestTokenBO;
import net.lab1024.smartadmin.module.system.login.domain.RequestTokenBO;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

View File

@@ -0,0 +1,83 @@
package net.lab1024.smartadmin.util.excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* zhuoda
*/
public class SmartExcel {
List<SmartSheet> sheetList = new ArrayList<SmartSheet>();;
public SmartExcel(String fileName) {
org.apache.poi.ss.usermodel.Workbook workbook = null;
try {
workbook = fileName.endsWith(".xls") ? new HSSFWorkbook(new FileInputStream(fileName)) : new XSSFWorkbook(new FileInputStream(fileName));
int numberOfSheets = workbook.getNumberOfSheets();
for (int index = 0; index < numberOfSheets; index++) {
addSheet(new SmartSheet(workbook.getSheetAt(index)));
}
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
}
workbook = null;
}
}
}
public SmartExcel(InputStream ins, SmartExcelFileType fileType) {
org.apache.poi.ss.usermodel.Workbook workbook = null;
try {
workbook = fileType == SmartExcelFileType.XLS ? new HSSFWorkbook(ins) : new XSSFWorkbook(ins);
int numberOfSheets = workbook.getNumberOfSheets();
for (int index = 0; index < numberOfSheets; index++) {
addSheet(new SmartSheet(workbook.getSheetAt(index)));
}
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
}
workbook = null;
}
}
}
final protected void addSheet(SmartSheet sheet) {
this.sheetList.add(sheet);
}
final protected void addSheetList(Collection<SmartSheet> sheets) {
this.sheetList.addAll(sheets);
}
final public List<SmartSheet> getSheetList() {
return sheetList;
}
final public SmartSheet getSheet(String sheetName) {
for (SmartSheet sheet : sheetList) {
if (sheet.getName().equals(sheetName)) {
return sheet;
}
}
return null;
}
}

View File

@@ -0,0 +1,10 @@
package net.lab1024.smartadmin.util.excel;
/**
* @author zhuoda
*/
public enum SmartExcelFileType {
XLS,
XLSX
}

View File

@@ -0,0 +1,32 @@
package net.lab1024.smartadmin.util.excel;
/**
* @author zhuoda
* @Date 2020/8/10
*/
import net.lab1024.smartadmin.util.SmartFileUtil;
import java.io.*;
public class SmartExcelReader {
public static SmartExcel openExcel(String filePath) throws IOException {
SmartFileUtil.isFileExistThrowException(filePath);
return new SmartExcel(new File(filePath).getCanonicalPath());
}
public static SmartExcel openExcel(File file) throws IOException {
return new SmartExcel(file.getCanonicalPath());
}
public static SmartExcel openExcel(InputStream ins, SmartExcelFileType fileType) throws IOException {
return new SmartExcel(ins, fileType);
}
public static void main(String[] args) throws Exception {
SmartExcel smartExcel = openExcel(new FileInputStream(new File("F:/privilege.xlsx")), SmartExcelFileType.XLSX);
System.out.println(smartExcel.getSheetList());
}
}

View File

@@ -0,0 +1,113 @@
package net.lab1024.smartadmin.util.excel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* @author zhuoda
*/
public class SmartSheet {
final String name;
private final int rowCount;
private final int columnCount;
private final String[][] datas;
public SmartSheet(org.apache.poi.ss.usermodel.Sheet sheet) {
this.name = sheet.getSheetName();
this.rowCount = sheet.getLastRowNum() + 1;
// 初始化基本数据
int maxColumnCount = 0;
this.datas = new String[rowCount][];
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
int _columnCount = row.getLastCellNum() + 1;
this.datas[rowIndex] = new String[_columnCount];
for (int colIndex = 0; colIndex < _columnCount; colIndex++) {
this.datas[rowIndex][colIndex] = getCellContents(row.getCell(colIndex));
}
if (maxColumnCount < _columnCount) {
maxColumnCount = _columnCount;
}
}
this.columnCount = maxColumnCount;
// 根据单元格合并情况,填充内容
for (int index = 0; index < sheet.getNumMergedRegions(); index++) {
CellRangeAddress mergedRegion = sheet.getMergedRegion(index);
String upperLeftData = this.datas[mergedRegion.getFirstRow()][mergedRegion.getFirstColumn()];
for (int rowIndex = mergedRegion.getFirstRow(); rowIndex <= mergedRegion.getLastRow(); rowIndex++) {
String[] _rowDatas = this.datas[rowIndex];
if (_rowDatas == null) {
this.datas[rowIndex] = new String[mergedRegion.getLastColumn() + 1];
} else if (_rowDatas.length < mergedRegion.getLastColumn() + 1) {
String[] newStrArray = new String[mergedRegion.getLastColumn() + 1];
System.arraycopy(_rowDatas, 0, newStrArray, 0, _rowDatas.length);
this.datas[rowIndex] = newStrArray;
}
for (int colIndex = mergedRegion.getFirstColumn(); colIndex <= mergedRegion.getLastColumn(); colIndex++) {
this.datas[rowIndex][colIndex] = upperLeftData;
}
}
}
}
private String getCellContents(Cell cell) {
if (cell == null) {
return null;
}
return getCellContents(cell.getCellType(), cell);
}
private String getCellContents(CellType type, Cell cell) {
switch (type) {
case BLANK:
return "";
case NUMERIC:
return cell.getStringCellValue();
case STRING:
return cell.getStringCellValue();
case FORMULA:
return getCellContents(cell.getCachedFormulaResultType(), cell);
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case ERROR:
default:
throw new IllegalArgumentException(String.format("unsupported cell type:%d, col:%d, row:%d, sheet:%s", cell.getCellType(), cell.getColumnIndex(),
cell.getRowIndex(), getName()));
}
}
public int getRowCount() {
return rowCount;
}
public int getColumnCount() {
return columnCount;
}
public String getValue(int rowIndex, int columnIndex) {
if (rowIndex < 0 || rowIndex >= datas.length) {
return "";
}
if (columnIndex < 0 || datas[rowIndex] == null || columnIndex >= datas[rowIndex].length) {
return "";
}
String value = datas[rowIndex][columnIndex];
return value == null ? "": value;
}
public String getName() {
return name;
}
}