|
@@ -0,0 +1,415 @@
|
|
|
|
+package com.jeeplus.common.utils.excel.utils;
|
|
|
|
+
|
|
|
|
+import com.jeeplus.common.utils.StringUtils;
|
|
|
|
+import com.jeeplus.common.utils.excel.entity.CellModel;
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+public class ExcelUtilInvoice {
|
|
|
|
+
|
|
|
|
+ public static Map<String, List<CellModel>> generateExcelHeader() {
|
|
|
|
+ Map<String, List<CellModel>> headerMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 第一行表头
|
|
|
|
+ List<CellModel> firstRow = new ArrayList<>();
|
|
|
|
+ firstRow.add(new CellModel("月份", 0, 1, 0, 0));
|
|
|
|
+ firstRow.add(new CellModel("合计收入", 0, 1, 1, 1));
|
|
|
|
+ firstRow.add(new CellModel("一部", 0, 0, 2, 3));
|
|
|
|
+ firstRow.add(new CellModel("二部", 0, 1, 4, 4));
|
|
|
|
+ firstRow.add(new CellModel("三部", 0, 1, 5, 5));
|
|
|
|
+ firstRow.add(new CellModel("四部", 0, 1, 6, 6));
|
|
|
|
+ firstRow.add(new CellModel("五部", 0, 0, 7, 10));
|
|
|
|
+ firstRow.add(new CellModel("招标部", 0, 1, 11, 11));
|
|
|
|
+ firstRow.add(new CellModel("盐城分公司", 0, 1, 12, 12));
|
|
|
|
+ firstRow.add(new CellModel("综合", 0, 1, 13, 13));
|
|
|
|
+
|
|
|
|
+ // 第二行表头
|
|
|
|
+ List<CellModel> secondRow = new ArrayList<>();
|
|
|
|
+ secondRow.add(new CellModel("本部", 1, 1, 2, 2));
|
|
|
|
+ secondRow.add(new CellModel("苏州", 1, 1, 3, 3));
|
|
|
|
+ secondRow.add(new CellModel("王晓青", 1, 1, 7, 7));
|
|
|
|
+ secondRow.add(new CellModel("孙兴华", 1, 1, 8, 8));
|
|
|
|
+ secondRow.add(new CellModel("王小国", 1, 1, 9, 9));
|
|
|
|
+ secondRow.add(new CellModel("刘万前", 1, 1, 10, 10));
|
|
|
|
+
|
|
|
|
+ headerMap.put("0", firstRow);
|
|
|
|
+ headerMap.put("1", secondRow);
|
|
|
|
+
|
|
|
|
+ return headerMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static Font createFont(Workbook workbook) {
|
|
|
|
+ Font font = workbook.createFont();
|
|
|
|
+ font.setFontName("宋体");
|
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
|
+ return font;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static CellStyle createHeaderStyle(Workbook workbook) {
|
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
|
+ style.setFont(createFont(workbook));
|
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ style.setBorderTop(BorderStyle.MEDIUM);
|
|
|
|
+ style.setBorderBottom(BorderStyle.MEDIUM);
|
|
|
|
+ style.setBorderLeft(BorderStyle.MEDIUM);
|
|
|
|
+ style.setBorderRight(BorderStyle.MEDIUM);
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static CellStyle createDataStyle(Workbook workbook) {
|
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
|
+ style.setFont(createFont(workbook));
|
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static CellStyle createNumberStyle(Workbook workbook) {
|
|
|
|
+ CellStyle style = createDataStyle(workbook);
|
|
|
|
+ DataFormat format = workbook.createDataFormat();
|
|
|
|
+ style.setDataFormat(format.getFormat("0.00")); // 设置数字格式
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static CellStyle createTitleStyle(Workbook workbook) {
|
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
|
+ Font font = createFont(workbook);
|
|
|
|
+ font.setBold(true);
|
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
|
+ style.setFont(font);
|
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static CellStyle createFillDataStyle(Workbook workbook) {
|
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
|
+ style.setFont(createFont(workbook));
|
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void createExcelSheet(Workbook workbook, Sheet sheet, Map<String, List<CellModel>> headerMap, int headerSize, List<Map<String, String>> dataList,String year,String title) {
|
|
|
|
+ CellStyle headerStyle = createHeaderStyle(workbook);
|
|
|
|
+ CellStyle dataStyle = createDataStyle(workbook);
|
|
|
|
+ CellStyle numberStyle = createNumberStyle(workbook);
|
|
|
|
+ CellStyle titleStyle = createTitleStyle(workbook);
|
|
|
|
+ CellStyle fillStyle = createFillDataStyle(workbook); // 这个样式没有边框
|
|
|
|
+
|
|
|
|
+ int titleMergeCols = 14;
|
|
|
|
+ List<String> fillData = Arrays.asList("年度:", year + "年");
|
|
|
|
+
|
|
|
|
+ // 创建标题行
|
|
|
|
+ Row titleRow = sheet.createRow(0);
|
|
|
|
+ titleRow.setHeightInPoints(30); // 设置标题行高
|
|
|
|
+ Cell titleCell = titleRow.createCell(0);
|
|
|
|
+ titleCell.setCellValue(title);
|
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, titleMergeCols - 1));
|
|
|
|
+
|
|
|
|
+ // 创建填写数据行
|
|
|
|
+ Row fillRow = sheet.createRow(1);
|
|
|
|
+ fillRow.setHeightInPoints(25); // 设置填写数据行高
|
|
|
|
+ for (int i = 0; i < fillData.size(); i++) {
|
|
|
|
+ Cell cell = fillRow.createCell(i);
|
|
|
|
+ cell.setCellValue(fillData.get(i));
|
|
|
|
+ cell.setCellStyle(fillStyle);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建表头
|
|
|
|
+ for (int rowIndex = 0; rowIndex < headerSize; rowIndex++) {
|
|
|
|
+ List<CellModel> cellModelList = headerMap.get(String.valueOf(rowIndex));
|
|
|
|
+
|
|
|
|
+ if (cellModelList != null) {
|
|
|
|
+ for (CellModel cellModel : cellModelList) {
|
|
|
|
+ // 合并单元格
|
|
|
|
+ if (cellModel.getStartRow() != cellModel.getEndRow() || cellModel.getStartColumn() != cellModel.getEndColumn()) {
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(
|
|
|
|
+ cellModel.getStartRow() + 2, cellModel.getEndRow() + 2,
|
|
|
|
+ cellModel.getStartColumn(), cellModel.getEndColumn()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建单元格并设置样式
|
|
|
|
+ for (int r = cellModel.getStartRow() + 2; r <= cellModel.getEndRow() + 2; r++) {
|
|
|
|
+ Row mergeRow = sheet.getRow(r) != null ? sheet.getRow(r) : sheet.createRow(r);
|
|
|
|
+ mergeRow.setHeightInPoints(20); // 设置表头行高
|
|
|
|
+ for (int c = cellModel.getStartColumn(); c <= cellModel.getEndColumn(); c++) {
|
|
|
|
+ Cell cell = mergeRow.createCell(c);
|
|
|
|
+ cell.setCellValue(cellModel.getCellName());
|
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 填充数据
|
|
|
|
+ for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) {
|
|
|
|
+ Row row = sheet.createRow(rowIndex + headerSize + 2);
|
|
|
|
+ row.setHeightInPoints(18); // 设置数据行高
|
|
|
|
+ Map<String, String> data = dataList.get(rowIndex);
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
|
|
+ Cell cell = row.createCell(Integer.parseInt(entry.getKey()));
|
|
|
|
+ if (StringUtils.isBlank(entry.getValue())) {
|
|
|
|
+ cell.setCellValue(Double.parseDouble("0"));
|
|
|
|
+ } else if (isNumeric(entry.getValue())) {
|
|
|
|
+ //浮点值
|
|
|
|
+ cell.setCellValue(Double.parseDouble(entry.getValue()));
|
|
|
|
+ } else {
|
|
|
|
+ //非浮点值
|
|
|
|
+ cell.setCellValue(entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ cell.setCellStyle(numberStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 自适应列宽并增加额外宽度
|
|
|
|
+ for (int i = 0; i < 14; i++) {
|
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
|
+ int currentWidth = sheet.getColumnWidth(i);
|
|
|
|
+ sheet.setColumnWidth(i, currentWidth + 1000);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ try (Workbook workbook = new XSSFWorkbook()) {
|
|
|
|
+ Map<String, List<CellModel>> headerMap = generateExcelHeader();
|
|
|
|
+ int headerSize = headerMap.size();
|
|
|
|
+
|
|
|
|
+ // 示例数据
|
|
|
|
+ List<Map<String, String>> dataList = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
|
+ Map<String, String> data = new HashMap<>();
|
|
|
|
+ for (int j = 0; j < 14; j++) {
|
|
|
|
+ data.put(String.valueOf(j), String.valueOf((i + 1) * (j + 1) * 10));
|
|
|
|
+ }
|
|
|
|
+ dataList.add(data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ CellStyle headerStyle = createHeaderStyle(workbook);
|
|
|
|
+ CellStyle dataStyle = createDataStyle(workbook);
|
|
|
|
+ CellStyle numberStyle = createNumberStyle(workbook);
|
|
|
|
+ CellStyle titleStyle = createTitleStyle(workbook);
|
|
|
|
+ CellStyle fillStyle = createFillDataStyle(workbook); // 这个样式没有边框
|
|
|
|
+
|
|
|
|
+ String title = "兴光项目公司开票汇总表";
|
|
|
|
+ int titleMergeCols = 14;
|
|
|
|
+ List<String> fillData = Arrays.asList("年份:", "2024");
|
|
|
|
+
|
|
|
|
+ // 创建 Excel 表格
|
|
|
|
+ Sheet sheet = workbook.createSheet("测试表单");
|
|
|
|
+
|
|
|
|
+ // 创建标题行
|
|
|
|
+ Row titleRow = sheet.createRow(0);
|
|
|
|
+ titleRow.setHeightInPoints(30); // 设置标题行高
|
|
|
|
+ Cell titleCell = titleRow.createCell(0);
|
|
|
|
+ titleCell.setCellValue(title);
|
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, titleMergeCols - 1));
|
|
|
|
+
|
|
|
|
+ // 创建填写数据行
|
|
|
|
+ Row fillRow = sheet.createRow(1);
|
|
|
|
+ fillRow.setHeightInPoints(25); // 设置填写数据行高
|
|
|
|
+ for (int i = 0; i < fillData.size(); i++) {
|
|
|
|
+ Cell cell = fillRow.createCell(i);
|
|
|
|
+ cell.setCellValue(fillData.get(i));
|
|
|
|
+ cell.setCellStyle(fillStyle);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建表头
|
|
|
|
+ for (int rowIndex = 0; rowIndex < headerSize; rowIndex++) {
|
|
|
|
+ List<CellModel> cellModelList = headerMap.get(String.valueOf(rowIndex));
|
|
|
|
+
|
|
|
|
+ if (cellModelList != null) {
|
|
|
|
+ for (CellModel cellModel : cellModelList) {
|
|
|
|
+ // 合并单元格
|
|
|
|
+ if (cellModel.getStartRow() != cellModel.getEndRow() || cellModel.getStartColumn() != cellModel.getEndColumn()) {
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(
|
|
|
|
+ cellModel.getStartRow() + 2, cellModel.getEndRow() + 2,
|
|
|
|
+ cellModel.getStartColumn(), cellModel.getEndColumn()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建单元格并设置样式
|
|
|
|
+ for (int r = cellModel.getStartRow() + 2; r <= cellModel.getEndRow() + 2; r++) {
|
|
|
|
+ Row mergeRow = sheet.getRow(r) != null ? sheet.getRow(r) : sheet.createRow(r);
|
|
|
|
+ mergeRow.setHeightInPoints(20); // 设置表头行高
|
|
|
|
+ for (int c = cellModel.getStartColumn(); c <= cellModel.getEndColumn(); c++) {
|
|
|
|
+ Cell cell = mergeRow.createCell(c);
|
|
|
|
+ cell.setCellValue(cellModel.getCellName());
|
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 填充数据
|
|
|
|
+ for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) {
|
|
|
|
+ Row row = sheet.createRow(rowIndex + headerSize + 2);
|
|
|
|
+ row.setHeightInPoints(18); // 设置数据行高
|
|
|
|
+ Map<String, String> data = dataList.get(rowIndex);
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
|
|
+ Cell cell = row.createCell(Integer.parseInt(entry.getKey()));
|
|
|
|
+ cell.setCellValue(Double.parseDouble(entry.getValue()));
|
|
|
|
+ cell.setCellStyle(numberStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 自适应列宽并增加额外宽度
|
|
|
|
+ for (int i = 0; i < 14; i++) {
|
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
|
+ int currentWidth = sheet.getColumnWidth(i);
|
|
|
|
+ sheet.setColumnWidth(i, currentWidth + 1000);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (OutputStream os = new FileOutputStream("D:\\复杂表头示例.xlsx")) {
|
|
|
|
+ workbook.write(os);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void exportInvoiceDataInfoByYear(List<Map<String, String>> dataList) {
|
|
|
|
+ try (Workbook workbook = new XSSFWorkbook()) {
|
|
|
|
+ Map<String, List<CellModel>> headerMap = generateExcelHeader();
|
|
|
|
+ int headerSize = headerMap.size();
|
|
|
|
+
|
|
|
|
+ CellStyle headerStyle = createHeaderStyle(workbook);
|
|
|
|
+ CellStyle dataStyle = createDataStyle(workbook);
|
|
|
|
+ CellStyle numberStyle = createNumberStyle(workbook);
|
|
|
|
+ CellStyle titleStyle = createTitleStyle(workbook);
|
|
|
|
+ CellStyle fillStyle = createFillDataStyle(workbook); // 这个样式没有边框
|
|
|
|
+
|
|
|
|
+ String title = "兴光项目公司开票汇总表";
|
|
|
|
+ int titleMergeCols = 14;
|
|
|
|
+ List<String> fillData = Arrays.asList("年份:", "2024年");
|
|
|
|
+
|
|
|
|
+ // 创建 Excel 表格
|
|
|
|
+ Sheet sheet = workbook.createSheet("测试表单");
|
|
|
|
+
|
|
|
|
+ // 创建标题行
|
|
|
|
+ Row titleRow = sheet.createRow(0);
|
|
|
|
+ titleRow.setHeightInPoints(30); // 设置标题行高
|
|
|
|
+ Cell titleCell = titleRow.createCell(0);
|
|
|
|
+ titleCell.setCellValue(title);
|
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, titleMergeCols - 1));
|
|
|
|
+
|
|
|
|
+ // 创建填写数据行
|
|
|
|
+ Row fillRow = sheet.createRow(1);
|
|
|
|
+ fillRow.setHeightInPoints(25); // 设置填写数据行高
|
|
|
|
+ for (int i = 0; i < fillData.size(); i++) {
|
|
|
|
+ Cell cell = fillRow.createCell(i);
|
|
|
|
+ cell.setCellValue(fillData.get(i));
|
|
|
|
+ cell.setCellStyle(fillStyle);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建表头
|
|
|
|
+ for (int rowIndex = 0; rowIndex < headerSize; rowIndex++) {
|
|
|
|
+ List<CellModel> cellModelList = headerMap.get(String.valueOf(rowIndex));
|
|
|
|
+
|
|
|
|
+ if (cellModelList != null) {
|
|
|
|
+ for (CellModel cellModel : cellModelList) {
|
|
|
|
+ // 合并单元格
|
|
|
|
+ if (cellModel.getStartRow() != cellModel.getEndRow() || cellModel.getStartColumn() != cellModel.getEndColumn()) {
|
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(
|
|
|
|
+ cellModel.getStartRow() + 2, cellModel.getEndRow() + 2,
|
|
|
|
+ cellModel.getStartColumn(), cellModel.getEndColumn()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建单元格并设置样式
|
|
|
|
+ for (int r = cellModel.getStartRow() + 2; r <= cellModel.getEndRow() + 2; r++) {
|
|
|
|
+ Row mergeRow = sheet.getRow(r) != null ? sheet.getRow(r) : sheet.createRow(r);
|
|
|
|
+ mergeRow.setHeightInPoints(20); // 设置表头行高
|
|
|
|
+ for (int c = cellModel.getStartColumn(); c <= cellModel.getEndColumn(); c++) {
|
|
|
|
+ Cell cell = mergeRow.createCell(c);
|
|
|
|
+ cell.setCellValue(cellModel.getCellName());
|
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 填充数据
|
|
|
|
+ for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) {
|
|
|
|
+ Row row = sheet.createRow(rowIndex + headerSize + 2);
|
|
|
|
+ row.setHeightInPoints(18); // 设置数据行高
|
|
|
|
+ Map<String, String> data = dataList.get(rowIndex);
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
|
|
+ Cell cell = row.createCell(Integer.parseInt(entry.getKey()));
|
|
|
|
+ if(StringUtils.isBlank(entry.getValue())){
|
|
|
|
+ cell.setCellValue(Double.parseDouble("0"));
|
|
|
|
+ }else if(isNumeric(entry.getValue())){
|
|
|
|
+ //浮点值
|
|
|
|
+ cell.setCellValue(Double.parseDouble(entry.getValue()));
|
|
|
|
+ }else {
|
|
|
|
+ //非浮点值
|
|
|
|
+ cell.setCellValue(entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ cell.setCellStyle(numberStyle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 自适应列宽并增加额外宽度
|
|
|
|
+ for (int i = 0; i < 14; i++) {
|
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
|
+ int currentWidth = sheet.getColumnWidth(i);
|
|
|
|
+ sheet.setColumnWidth(i, currentWidth + 1000);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (OutputStream os = new FileOutputStream("D:\\复杂表头示例.xlsx")) {
|
|
|
|
+ workbook.write(os);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static boolean isNumeric(String str) {
|
|
|
|
+ if (str == null || str.isEmpty()) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String regex = "[-+]?\\d*\\.?\\d+";
|
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
|
+ Matcher matcher = pattern.matcher(str);
|
|
|
|
+
|
|
|
|
+ if (matcher.matches()) {
|
|
|
|
+ try {
|
|
|
|
+ Double.parseDouble(str);
|
|
|
|
+ return true;
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|