背景
分享一个导出excel样式模版
开始
工具类
public class ExcelUtil {
public Timestamp formatTimeStr(String timeStr) {
try {
SimpleDateFormat format;
if (timeStr.contains("年")) {
format = new SimpleDateFormat("yyyy年MM月dd日");
} else if (timeStr.contains("/")) {
format = new SimpleDateFormat("yyyy/MM/dd");
} else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
return new Timestamp(format.parse(timeStr).getTime());
} catch (Exception e) {
return null;
}
}
public Integer formatNumberStr(String numberStr) {
try {
boolean matches = Pattern.matches("^[0-9]{1,4}$", numberStr);
if (matches) {
return Double.valueOf(numberStr).intValue();
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
public double formatPriceStr(String numberStr) {
try {
boolean matches = Pattern.matches("(^[1-9]{1}[0-9]{0,3}$)|(^[0-9]{1}[0-9]{0,3}\\.[0-9]{0,2}$)", numberStr);
if (matches) {
return Double.valueOf(numberStr);
} else {
return -1;
}
} catch (Exception e) {
return -1;
}
}
public HorizontalCellStyleStrategy getCellStyle() {
WriteCellStyle headCellStyle = new WriteCellStyle();
headCellStyle.setWrapped(true);
headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headCellStyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index)