package com.example.store.util;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExcelUtils {
private final static String excel2003L =".xls";
private final static String excel2007U =".xlsx";
public static XSSFWorkbook getWorkbook(InputStream inStr, String fileName) throws Exception{
XSSFWorkbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr);
}else{
throw new Exception("解析的文件格式有误!");
}
return wb;
}
public static Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0.00");
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
DecimalFormat df2 = new DecimalFormat("0.00");
switch (cell.getCellType().getCode()) {
case 1://字符型
value = cell.getRichStringCellValue().getString();
break;
case 0://数字型
if("General".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue());
}else{
value = df2.format(cell.getNumericCellValue());
}
break;
case 4://布尔型
value = cell.getBooleanCellValue();
break;
case 3://为空
value = "";
break;
default:
break;
}
return value;
}
public static List<List<Object>> getListByExcel(InputStream in,String fileName) throws Exception{
List<List<Object>> list = new ArrayList<>();
Workbook work = ExcelUtils.getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
sheet = work.getSheetAt(0);
if(sheet==null){
return null;
}
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if(row==null){continue;}
List<Object> li = new ArrayList<>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
if(cell==null){
li.add("");
}else{
li.add(ExcelUtils.getCellValue(cell));
}
}
list.add(li);
}
in.close();
return list;
}
public static List<List<Object>> getListByExcel(InputStream in,String fileName,int colNum) throws Exception{
List<List<Object>> list = new ArrayList<>();
Workbook work = ExcelUtils.getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
sheet = work.getSheetAt(0);
if(sheet==null){
return null;
}
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if(row==null){continue;}
List<Object> li = new ArrayList<>();
for (int y = row.getFirstCellNum(); y < colNum; y++) {
cell = row.getCell(y);
if(cell==null){
li.add("");
}else{
li.add(ExcelUtils.getCellValue(cell));
}
}
list.add(li);
}
in.close();
return list;
}
public static HashMap<String,List<List<Object>>> getAllSheetData(InputStream in, String fileName) throws Exception{
HashMap<String,List<List<Object>>> sheetMap = new HashMap<>();
List<List<Object>> list = null;
Workbook work = ExcelUtils.getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
for (int i = 0; i < work.getNumberOfSheets(); i++) {
list = new ArrayList<>();
String sheetName = work.getSheetName(i);
sheet = work.getSheetAt(i);
if(sheet==null){
return null;
}
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if(row==null){continue;}
List<Object> li = new ArrayList<>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
if(cell==null) {
li.add("");
}else {
li.add(ExcelUtils.getCellValue(cell));
}
}
if( !li.isEmpty()) {
list.add(li);
}
}
sheetMap.put(sheetName, list);
}
in.close();
return sheetMap;
}
public static List<List<String>> getFirstSheetData(InputStream in, String fileName) throws Exception{
Workbook work = ExcelUtils.getWorkbook(in,fileName);
if(null == work){
throw new Exception("Excel工作薄为空!");
}
List<List<String>> list = new ArrayList<>();
Sheet sheet = work.getSheetAt(0);
if(sheet==null){
return null;
}
Row row;
Cell cell;
String cellValue;
Row firstRow= sheet.getRow(sheet.getFirstRowNum());
int firstCellNo = firstRow.getFirstCellNum();
int lastCellNo = firstRow.getLastCellNum();
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
if(row==null){continue;}
List<String> li = new ArrayList<>();
for (int j = firstCellNo; j < lastCellNo; j++) {
cell = row.getCell(j);
cellValue = null;
if(cell!=null) {
Object obj = ExcelUtils.getCellValue(cell);
if(obj!=null){
cellValue = String.valueOf(obj);
}
}
li.add(cellValue);
}
list.add(li);
}
in.close();
return list;
}
}
@ApiOperation("下载StoreMatrix示例文件")
@ApiResponses({
@ApiResponse(code = 200, response = File.class, message = "")
})
@RequestMapping(path = "/importStoreMatrixExample", produces="application/json", method= RequestMethod.GET)
public void importStoreMatrixExample(HttpServletResponse response) throws Exception {
org.springframework.core.io.Resource resource = new ClassPathResource("template/StoreMatrixExample.xlsx");
InputStream inStr = resource.getInputStream();
String fileName = "StoreMatrixExample.xlsx";
XSSFWorkbook workbook = ExcelUtils.getWorkbook(inStr, fileName);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
throw new Exception("报表导出失败!", e);
}
byte[] content = os.toByteArray();
if (inStr != null) {
inStr.close();
}
String encodedFileName = URLEncoder.encode(fileName + ".xlsx", "utf-8").replaceAll("\\+", "%20");
response.reset();
response.setHeader("Content-disposition", "attachment;filename="+encodedFileName+"");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.flushBuffer();
workbook.write(response.getOutputStream());
}