|
|
|
@ -0,0 +1,478 @@
|
|
|
|
|
package com.tz.platform.common.core.tools;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Font;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.Color;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
public class ExcelUtil {
|
|
|
|
|
private String templatePath ;
|
|
|
|
|
private String outPath;
|
|
|
|
|
|
|
|
|
|
private FileInputStream is;
|
|
|
|
|
private XSSFWorkbook excel ;
|
|
|
|
|
//获取第一个sheet
|
|
|
|
|
private XSSFSheet sheet0 ;
|
|
|
|
|
|
|
|
|
|
private Font defaultFont;
|
|
|
|
|
|
|
|
|
|
private CellStyle cellStyle;
|
|
|
|
|
|
|
|
|
|
private CellStyle numStyle ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ExcelUtil(){
|
|
|
|
|
initNumStyle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExcelUtil(String template,String out) throws Exception{
|
|
|
|
|
this.templatePath = template;
|
|
|
|
|
is = new FileInputStream(templatePath);
|
|
|
|
|
excel = new XSSFWorkbook(is);
|
|
|
|
|
defaultFont = excel.createFont();
|
|
|
|
|
((XSSFFont) defaultFont).setColor(new XSSFColor(new Color(0,0,0)));
|
|
|
|
|
cellStyle = excel.createCellStyle();
|
|
|
|
|
cellStyle.setFont(defaultFont);
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
initNumStyle();
|
|
|
|
|
//获取第一个sheet
|
|
|
|
|
sheet0 = excel.getSheetAt(0);
|
|
|
|
|
outPath = out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExcelUtil(InputStream fileInputStream) throws Exception{
|
|
|
|
|
excel = new XSSFWorkbook(fileInputStream);
|
|
|
|
|
sheet0 = excel.getSheetAt(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getLastRowNum(){
|
|
|
|
|
return this.sheet0.getLastRowNum();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initNumStyle(){
|
|
|
|
|
if(excel!=null){
|
|
|
|
|
numStyle = excel.createCellStyle();
|
|
|
|
|
XSSFDataFormat df = excel.createDataFormat();//此处设置数据格式
|
|
|
|
|
numStyle.setDataFormat(df.getFormat("#,#0.000")); //小数点后保留两位,可以写contentStyle.setDataFormat(df.getFormat("#,#0.00"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExcelUtil(String template,String out,int sheetIndex) throws Exception{
|
|
|
|
|
this.templatePath = template;
|
|
|
|
|
|
|
|
|
|
is = new FileInputStream(templatePath);
|
|
|
|
|
|
|
|
|
|
excel = new XSSFWorkbook(is);
|
|
|
|
|
|
|
|
|
|
//获取第一个sheet
|
|
|
|
|
sheet0 = excel.getSheetAt(sheetIndex);
|
|
|
|
|
outPath = out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void CreateExcel(java.util.List datas, ExcelHeader[] headers, String fileName, String sheetName){
|
|
|
|
|
if(excel == null){
|
|
|
|
|
excel = new XSSFWorkbook();
|
|
|
|
|
}
|
|
|
|
|
XSSFWorkbook workbook =excel;
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
int rowNum = 0;
|
|
|
|
|
|
|
|
|
|
// System.out.println("Creating excel");
|
|
|
|
|
|
|
|
|
|
int colNum = 0;
|
|
|
|
|
|
|
|
|
|
Row rowHeader = sheet.createRow(0);
|
|
|
|
|
|
|
|
|
|
for(ExcelHeader header :headers){
|
|
|
|
|
Cell cell = rowHeader.createCell(colNum++);
|
|
|
|
|
cell.setCellValue(header.getAliax());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rowNum=1;
|
|
|
|
|
colNum=0;
|
|
|
|
|
appendDataToExcel(sheet,datas,rowNum,colNum,headers);
|
|
|
|
|
|
|
|
|
|
String filePath = fileName;
|
|
|
|
|
outPath =filePath;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Row getRow(int rowNo){
|
|
|
|
|
Row row = sheet0.getRow(rowNo);
|
|
|
|
|
if(row==null){
|
|
|
|
|
row = sheet0.createRow(rowNo);
|
|
|
|
|
}
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Row getRow(XSSFSheet sheet,int rowNo){
|
|
|
|
|
Row row = sheet.getRow(rowNo);
|
|
|
|
|
if(row==null){
|
|
|
|
|
row = sheet.createRow(rowNo);
|
|
|
|
|
}
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setRichValue(String location,String value,int start,int end){
|
|
|
|
|
Cell cell = get("A21");
|
|
|
|
|
System.out.println(cell.getStringCellValue());
|
|
|
|
|
Font font = excel.createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
((XSSFFont) font).setColor(new XSSFColor(new Color(0,0,0)));
|
|
|
|
|
|
|
|
|
|
XSSFRichTextString richTextString1 = new XSSFRichTextString(value);
|
|
|
|
|
richTextString1.applyFont(start,end,font);
|
|
|
|
|
|
|
|
|
|
cell.setCellValue(richTextString1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Cell getCell(Row row, int cNo){
|
|
|
|
|
Cell cell = row.getCell(cNo);
|
|
|
|
|
if(cell == null ) {
|
|
|
|
|
cell = row.createCell(cNo);
|
|
|
|
|
}
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Cell getCell(int rowNo,int colNo){
|
|
|
|
|
Row row = getRow(rowNo);
|
|
|
|
|
return getCell(row,colNo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setValue(String location,Object value){
|
|
|
|
|
Cell cell = get(location);
|
|
|
|
|
setCellValue(cell,value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setValue(String location,Object value,String formula){
|
|
|
|
|
Cell cell = get(location);
|
|
|
|
|
cell.setCellValue(formula);
|
|
|
|
|
setCellValue(cell,value);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFormula(String location,String formula){
|
|
|
|
|
Cell cell = get(location);
|
|
|
|
|
cell.setCellFormula(formula);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Cell get(String location){
|
|
|
|
|
Pattern pattern = Pattern.compile("([A-Z]+)(\\d+)");
|
|
|
|
|
Matcher matcher = pattern.matcher(location.toUpperCase());
|
|
|
|
|
if(matcher.matches()){
|
|
|
|
|
String y = matcher.group(1);
|
|
|
|
|
String x = matcher.group(2);
|
|
|
|
|
int rowNum = coverToXIndex(x);
|
|
|
|
|
int colNum = covertToYIndex(y);
|
|
|
|
|
Row row = getRow(rowNum);
|
|
|
|
|
return getCell(row,colNum);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int coverToXIndex(String x){
|
|
|
|
|
return Integer.parseInt(x)-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int covertToYIndex(String y){
|
|
|
|
|
int result = 0;
|
|
|
|
|
for(int i = y.length()-1;i>=0;i--){
|
|
|
|
|
char c = y.charAt(i);
|
|
|
|
|
int power = ((y.length()-1)-i);
|
|
|
|
|
|
|
|
|
|
if(power>0){
|
|
|
|
|
result +=(c-'A'+1)*Math.pow(26,power);
|
|
|
|
|
}else{
|
|
|
|
|
result +=(c-'A');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void appendDataToExcel(XSSFSheet sheet, java.util.List datas, int rowNo, int colNo, ExcelHeader[] headers){
|
|
|
|
|
if(sheet==null){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
for(int r = 0;r<datas.size();r++){
|
|
|
|
|
Row row = getRow(sheet,rowNo+r);
|
|
|
|
|
|
|
|
|
|
Object t = datas.get(r);
|
|
|
|
|
Class tClass = t.getClass();
|
|
|
|
|
Field[] fields =tClass.getDeclaredFields();
|
|
|
|
|
if(headers!=null){
|
|
|
|
|
for(int c=0;c<headers.length;c++){
|
|
|
|
|
Cell cell =getCell(row,c+colNo);
|
|
|
|
|
if(cell == null){
|
|
|
|
|
cell = row.createCell(c+colNo);
|
|
|
|
|
}
|
|
|
|
|
ExcelHeader header = headers[c];
|
|
|
|
|
Field f = Arrays.stream(fields).filter(field -> field.getName().equals(header.name)).findFirst().orElse(null);
|
|
|
|
|
f.setAccessible(true);
|
|
|
|
|
Object v = f.get(t);
|
|
|
|
|
setCellValue(cell, v);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
for(int c = 0;c<fields.length;c++){
|
|
|
|
|
Cell cell = getCell(row,c+colNo);
|
|
|
|
|
Field f = fields[c];
|
|
|
|
|
f.setAccessible(true);
|
|
|
|
|
Object v = f.get(t);
|
|
|
|
|
setCellValue(cell, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}catch (Exception ex){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void appendDataToExcel(int sheetIndex, java.util.List datas, int rowNo, int colNo, ExcelHeader[] headers){
|
|
|
|
|
XSSFSheet sheet = excel.getSheetAt(sheetIndex);
|
|
|
|
|
if(sheet == null ) {return ;}
|
|
|
|
|
appendDataToExcel(sheet,datas,rowNo,colNo,headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void appendDataToExcel(List datas, int rowNo, int colNo, ExcelHeader[] headers){
|
|
|
|
|
appendDataToExcel(sheet0,datas,rowNo,colNo,headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setValue(int rowNo,int columNo,Object value){
|
|
|
|
|
Row row =getRow(rowNo);
|
|
|
|
|
Cell cell = getCell(row,columNo);
|
|
|
|
|
|
|
|
|
|
// if(isMerged(rowNo,columNo)){
|
|
|
|
|
// System.out.println( getMergedRegionValue(rowNo,columNo));
|
|
|
|
|
// setCellValue(cell,value);
|
|
|
|
|
// }else{
|
|
|
|
|
setCellValue(cell,value);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isMerged(int row,int column){
|
|
|
|
|
int sheetMergeCount = sheet0.getNumMergedRegions();
|
|
|
|
|
for (int i = 0; i < sheetMergeCount; i++) {
|
|
|
|
|
CellRangeAddress range = sheet0.getMergedRegion(i);
|
|
|
|
|
int firstColumn = range.getFirstColumn();
|
|
|
|
|
int lastColumn = range.getLastColumn();
|
|
|
|
|
int firstRow = range.getFirstRow();
|
|
|
|
|
int lastRow = range.getLastRow();
|
|
|
|
|
if(row == firstRow && row == lastRow){
|
|
|
|
|
if(column >= firstColumn && column <= lastColumn){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getMergedRegionValue(int row , int column){
|
|
|
|
|
int sheetMergeCount = sheet0.getNumMergedRegions();
|
|
|
|
|
|
|
|
|
|
for(int i = 0 ; i < sheetMergeCount ; i++){
|
|
|
|
|
CellRangeAddress ca = sheet0.getMergedRegion(i);
|
|
|
|
|
int firstColumn = ca.getFirstColumn();
|
|
|
|
|
int lastColumn = ca.getLastColumn();
|
|
|
|
|
int firstRow = ca.getFirstRow();
|
|
|
|
|
int lastRow = ca.getLastRow();
|
|
|
|
|
|
|
|
|
|
if(row >= firstRow && row <= lastRow){
|
|
|
|
|
|
|
|
|
|
if(column >= firstColumn && column <= lastColumn){
|
|
|
|
|
Row fRow = sheet0.getRow(firstRow);
|
|
|
|
|
Cell fCell = fRow.getCell(firstColumn);
|
|
|
|
|
return getCellValue(fCell) ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取单元格的值
|
|
|
|
|
* @param cell
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public String getCellValue(Cell cell){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(cell == null) {return "";}
|
|
|
|
|
|
|
|
|
|
if(cell.getCellType() == Cell.CELL_TYPE_STRING){
|
|
|
|
|
|
|
|
|
|
return cell.getStringCellValue();
|
|
|
|
|
|
|
|
|
|
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
|
|
|
|
|
|
|
|
|
|
return String.valueOf(cell.getBooleanCellValue());
|
|
|
|
|
|
|
|
|
|
}else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
|
|
|
|
|
|
|
|
|
|
return cell.getCellFormula() ;
|
|
|
|
|
|
|
|
|
|
}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
|
|
|
|
|
String dateTime = getDateTime(cell);
|
|
|
|
|
if(StringUtils.isEmpty(dateTime)){
|
|
|
|
|
return String.valueOf(cell.getNumericCellValue());
|
|
|
|
|
}
|
|
|
|
|
return dateTime;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getDateTime(Cell cell){
|
|
|
|
|
|
|
|
|
|
String result = "";
|
|
|
|
|
//1、判断是否是数值格式
|
|
|
|
|
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
|
|
|
|
|
short format = cell.getCellStyle().getDataFormat();
|
|
|
|
|
SimpleDateFormat sdf = null;
|
|
|
|
|
if(format == 14 || format == 31 || format == 57 || format == 58){
|
|
|
|
|
//日期
|
|
|
|
|
sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
}else if (format == 20 || format == 32) {
|
|
|
|
|
//时间
|
|
|
|
|
sdf = new SimpleDateFormat("HH:mm");
|
|
|
|
|
}else if(format == 176){
|
|
|
|
|
sdf =new SimpleDateFormat("yyyy/MM/dd");
|
|
|
|
|
}else{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
double value = cell.getNumericCellValue();
|
|
|
|
|
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
|
|
|
|
|
result = sdf.format(date);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean save(){
|
|
|
|
|
if(WriteExcel(excel,outPath)){
|
|
|
|
|
excel = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean WriteExcel(XSSFWorkbook workbook,String filePath){
|
|
|
|
|
try {
|
|
|
|
|
if(filePath==null) {return false;}
|
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(filePath);
|
|
|
|
|
|
|
|
|
|
workbook.write(outputStream);
|
|
|
|
|
workbook.close();
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCellValue(Cell cell , Object value){
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
if(value instanceof String){
|
|
|
|
|
cell.setCellValue((String) value);
|
|
|
|
|
}else if(value instanceof Integer){
|
|
|
|
|
cell.setCellValue((Integer)value);
|
|
|
|
|
}else if(value instanceof Float){
|
|
|
|
|
cell.setCellValue((Float) value);
|
|
|
|
|
}else if(value instanceof Double){
|
|
|
|
|
cell.setCellValue((Double)value);
|
|
|
|
|
}else if(value instanceof BigDecimal){
|
|
|
|
|
BigDecimal tmp = ((BigDecimal) value).abs();
|
|
|
|
|
if(numStyle==null){
|
|
|
|
|
initNumStyle();
|
|
|
|
|
}
|
|
|
|
|
cell.setCellStyle(numStyle);
|
|
|
|
|
cell.setCellValue(tmp.doubleValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ExcelHeader{
|
|
|
|
|
String name;
|
|
|
|
|
String aliax;
|
|
|
|
|
ParserValue parserValue;
|
|
|
|
|
|
|
|
|
|
public ExcelHeader(String name,String aliax,ParserValue... pv){
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.aliax = aliax;
|
|
|
|
|
if(pv!=null&&pv.length>0){
|
|
|
|
|
this.parserValue = pv[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列名
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public String getName() {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 别名
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public String getAliax() {
|
|
|
|
|
return aliax;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAliax(String aliax) {
|
|
|
|
|
this.aliax = aliax;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 值解析
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public ParserValue getParserValue() {
|
|
|
|
|
return parserValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParserValue(ParserValue parserValue) {
|
|
|
|
|
this.parserValue = parserValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@FunctionalInterface
|
|
|
|
|
public interface ParserValue{
|
|
|
|
|
String parser(Object object);
|
|
|
|
|
}
|
|
|
|
|
}
|