untils
。
package com.ysd.demo1.util;
import com.ysd.demo1.entity.Teacher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ExcelUtil {
public static final short IMG_HEIGTH = 30; // 导出图片高度
public static final short IMG_WIDTH = 30; // 导出图片宽度
/**
* 判断是否office文件
* @param inputStream
* @return
*/
public static Boolean isOfficeFile(InputStream inputStream){
boolean result = false;
try {
FileMagic fileMagic = FileMagic.valueOf(inputStream);
if (Objects.equals(fileMagic,FileMagic.OLE2)||Objects.equals(fileMagic, FileMagic.OOXML)){
result = true;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 判断是否office文件
* @param file
* @return
* @throws IOException
*/
public static Boolean isOfficeFile(MultipartFile file) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(file.getInputStream());
boolean result = false;
result = isOfficeFile(bufferedInputStream);
return result;
}
/**
* 判断扩展名是否是excel扩展名
* @param extension
* @return
*/
public static Boolean checkExtension(String extension){
return Arrays.asList("xls","xlsx","XLS","XLSX").contains(extension);
}
/**
* 判断扩展名是否是excel扩展名
* @param file
* @return
*/
public static Boolean checkExtension(MultipartFile file){
String fileName = file.getOriginalFilename();
String extension = fileName.substring(fileName.lastIndexOf(".")+1);
return checkExtension(extension);
}
/**
* 自动判断文件类型
* @param file
* @return
* @throws IOException
*/
public static Workbook getWorkbookAuto(MultipartFile file) throws IOException {
/** 判断文件的类型,是2003还是2007 */
boolean isExcel2003 = true;
if (isExcel2007(file.getOriginalFilename())) {
isExcel2003 = false;
}
BufferedInputStream is = new BufferedInputStream(
file.getInputStream());
Workbook wb;
if (isExcel2003) {
wb = new HSSFWorkbook(is);
} else {
wb = new XSSFWorkbook(is);
}
return wb;
}
public static Workbook getWorkbookAuto(File file){
String fileName = file.getName().toLowerCase();
Workbook book = null;
try {
if (isExcel2007(fileName)) {
book = new XSSFWorkbook(new FileInputStream