main
黄海 2 years ago
parent ca29780314
commit 61d135fcc9

@ -516,6 +516,8 @@ public class CollectController extends Controller {
//打开EXCEL进行检查
InputStream is = new FileInputStream(f2);
XSSFWorkbook wb = new XSSFWorkbook(is);
//遍历每个Sheet注册好的信息对用户上传的数据表进行检查
boolean flag = true;
List<Integer> blankSheet = new ArrayList<>();
@ -528,7 +530,7 @@ public class CollectController extends Controller {
Record r = cm.getSheetConfig(upload_excel_filename_finish, i);
int data_start_row = r.getInt("data_start_row");
//恢复背景色
PoiUtil.resetStyle(wb, sheet, data_start_row);
//PoiUtil.resetStyle(wb, sheet, data_start_row);
//数据有效行数
int lastRowNum = sheet.getLastRowNum();
@ -565,11 +567,17 @@ public class CollectController extends Controller {
}
}
}
// int sheet_index = 1;
// int row_index = 3;
// int col_index = 7;
// XSSFCell cell2 = wb.getSheetAt(sheet_index).getRow(row_index).getCell(col_index);
// Object value2 = PoiUtil.getValue(cell2);
// System.out.println(value2);
// 检查数据类型是不是和规定的不兼容
for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
Object obj = PoiUtil.getValue(cell);
if (column_type.equals("Integer") && !CommonUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
PoiUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
@ -578,10 +586,13 @@ public class CollectController extends Controller {
PoiUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
} else if (column_type.equals("Date") && !CommonUtil.isDate(obj.toString())) {//要求是日期,实际不是日期
PoiUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
}
else if (column_type.equals("Date")) {//要求是日期,实际不是日期
if (!CommonUtil.isDate(obj.toString())) {
PoiUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false;
}
}
}
}

@ -9,6 +9,7 @@ import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
@ -102,7 +103,7 @@ public class PoiUtil {
* @param sheet
*/
public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) {
// 遍历所有行和单元格,移除批注
// 遍历所有行和单元格,恢复颜色
for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) {
for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) {
Cell cell = sheet.getRow(row).getCell(col);
@ -176,32 +177,68 @@ public class PoiUtil {
* @param cell
* @return
*/
public static Object getValue(XSSFCell cell) {
//获取单元格各类型值,返回字符串类型
public static String getValue(Cell cell) {
//判断是否为null或空串
if (cell == null || cell.toString().trim().equals("")) {
return "";
}
String cellValue = "";
switch (cell.getCellType()) {
case CellType.STRING:
String cellValueString = cell.getStringCellValue();
return cellValueString;
case CellType.NUMERIC:
//日期格式
case CellType.NUMERIC: // 数字
short format = cell.getCellStyle().getDataFormat();
if (DateUtil.isCellDateFormatted(cell)) {
Date dateValue = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(dateValue);
return formattedDate;
SimpleDateFormat sdf = null;
//System.out.println("cell.getCellStyle().getDataFormat()="+cell.getCellStyle().getDataFormat());
if (format == 20 || format == 32) {
sdf = new SimpleDateFormat("HH:mm");
} else if (format == 14 || format == 31 || format == 57 || format == 58) {
// 处理自定义日期格式m月d日(通过判断单元格的格式id解决id的值是58)
sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
cellValue = sdf.format(date);
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
try {
cellValue = sdf.format(cell.getDateCellValue());// 日期
} catch (Exception e) {
try {
throw new Exception("exception on get date data !".concat(e.toString()));
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
sdf = null;
}
} else {
BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString可以防止获取到科学计数值
}
double cellValueNumeric = cell.getNumericCellValue();
return cellValueNumeric;
case CellType.BOOLEAN:
boolean cellValueBoolean = cell.getBooleanCellValue();
return cellValueBoolean;
case CellType.BLANK:
cellValueString = cell.getStringCellValue();
return cellValueString;
// 其他类型的处理
break;
case CellType.STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case CellType.BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
;
break;
case CellType.FORMULA: // 公式
cellValue = cell.getCellFormula();
break;
case CellType.BLANK: // 空值
cellValue = "";
break;
case CellType.ERROR: // 故障
cellValue = "ERROR VALUE";
break;
default:
cellValueString = cell.getStringCellValue();
return cellValueString;
cellValue = "UNKNOW VALUE";
break;
}
return cellValue;
}
/**
@ -346,19 +383,30 @@ public class PoiUtil {
}
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(is);
setIntegerStyle(wb, 0, 2, 0, 20000);
// InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx");
// XSSFWorkbook wb = new XSSFWorkbook(is);
//
// setIntegerStyle(wb, 0, 2, 0, 20000);
//
// setFloatStyle(wb, 0, 3, 0, 20000);
//
// setDateStyle(wb, 0, 4, 0, 20000);
// //保存
// FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx");
// wb.write(fileOut);
// //关闭Excel
// wb.close();
setFloatStyle(wb, 0, 3, 0, 20000);
setDateStyle(wb,0,4,0,20000);
//保存
FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx");
wb.write(fileOut);
//关闭Excel
wb.close();
String f2 = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\4.xlsx";
InputStream is = new FileInputStream(f2);
XSSFWorkbook wb = new XSSFWorkbook(is);
int sheet_index = 1;
int row_index = 3;
int col_index = 7;
XSSFCell cell = wb.getSheetAt(sheet_index).getRow(row_index).getCell(col_index);
Object value = getValue(cell);
System.out.println(value);
}
}

Binary file not shown.
Loading…
Cancel
Save