main
黄海 2 years ago
parent ca29780314
commit 61d135fcc9

@ -516,6 +516,8 @@ public class CollectController extends Controller {
//打开EXCEL进行检查 //打开EXCEL进行检查
InputStream is = new FileInputStream(f2); InputStream is = new FileInputStream(f2);
XSSFWorkbook wb = new XSSFWorkbook(is); XSSFWorkbook wb = new XSSFWorkbook(is);
//遍历每个Sheet注册好的信息对用户上传的数据表进行检查 //遍历每个Sheet注册好的信息对用户上传的数据表进行检查
boolean flag = true; boolean flag = true;
List<Integer> blankSheet = new ArrayList<>(); List<Integer> blankSheet = new ArrayList<>();
@ -528,7 +530,7 @@ public class CollectController extends Controller {
Record r = cm.getSheetConfig(upload_excel_filename_finish, i); Record r = cm.getSheetConfig(upload_excel_filename_finish, i);
int data_start_row = r.getInt("data_start_row"); 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(); 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++) {//行 for (int j = data_start_row; j <= lastRowNum; j++) {//行
XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx); XSSFCell cell = sheet.getRow(j).getCell(excel_column_idx);
Object obj = PoiUtil.getValue(cell); Object obj = PoiUtil.getValue(cell);
if (column_type.equals("Integer") && !CommonUtil.isInteger(obj.toString())) {//要求整数,实际不是整数 if (column_type.equals("Integer") && !CommonUtil.isInteger(obj.toString())) {//要求整数,实际不是整数
PoiUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!"); PoiUtil.addComment(wb, cell, "要求是整数,实际数据类型不是整数!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
@ -578,10 +586,13 @@ public class CollectController extends Controller {
PoiUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!"); PoiUtil.addComment(wb, cell, "要求是小数,实际数据类型不是小数!");
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex());
flag = false; flag = false;
} else if (column_type.equals("Date") && !CommonUtil.isDate(obj.toString())) {//要求是日期,实际不是日期 }
PoiUtil.addComment(wb, cell, "要求是日期格式,实际数据类型不是日期格式!"); else if (column_type.equals("Date")) {//要求是日期,实际不是日期
PoiUtil.fillColor(wb, cell, IndexedColors.YELLOW.getIndex()); if (!CommonUtil.isDate(obj.toString())) {
flag = false; 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 org.apache.poi.xssf.usermodel.*;
import java.io.*; import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -102,7 +103,7 @@ public class PoiUtil {
* @param sheet * @param sheet
*/ */
public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) { public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) {
// 遍历所有行和单元格,移除批注 // 遍历所有行和单元格,恢复颜色
for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) { for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) {
for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) { for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) {
Cell cell = sheet.getRow(row).getCell(col); Cell cell = sheet.getRow(row).getCell(col);
@ -176,32 +177,68 @@ public class PoiUtil {
* @param cell * @param cell
* @return * @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()) { switch (cell.getCellType()) {
case CellType.STRING: case CellType.NUMERIC: // 数字
String cellValueString = cell.getStringCellValue(); short format = cell.getCellStyle().getDataFormat();
return cellValueString;
case CellType.NUMERIC:
//日期格式
if (DateUtil.isCellDateFormatted(cell)) { if (DateUtil.isCellDateFormatted(cell)) {
Date dateValue = cell.getDateCellValue(); SimpleDateFormat sdf = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //System.out.println("cell.getCellStyle().getDataFormat()="+cell.getCellStyle().getDataFormat());
String formattedDate = sdf.format(dateValue); if (format == 20 || format == 32) {
return formattedDate; 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(); break;
return cellValueNumeric; case CellType.STRING: // 字符串
case CellType.BOOLEAN: cellValue = cell.getStringCellValue();
boolean cellValueBoolean = cell.getBooleanCellValue(); break;
return cellValueBoolean; case CellType.BOOLEAN: // Boolean
case CellType.BLANK: cellValue = cell.getBooleanCellValue() + "";
cellValueString = cell.getStringCellValue(); ;
return cellValueString; break;
// 其他类型的处理 case CellType.FORMULA: // 公式
cellValue = cell.getCellFormula();
break;
case CellType.BLANK: // 空值
cellValue = "";
break;
case CellType.ERROR: // 故障
cellValue = "ERROR VALUE";
break;
default: default:
cellValueString = cell.getStringCellValue(); cellValue = "UNKNOW VALUE";
return cellValueString; break;
} }
return cellValue;
} }
/** /**
@ -346,19 +383,30 @@ public class PoiUtil {
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx"); // InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(is); // XSSFWorkbook wb = new XSSFWorkbook(is);
//
setIntegerStyle(wb, 0, 2, 0, 20000); // 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); String f2 = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\4.xlsx";
InputStream is = new FileInputStream(f2);
setDateStyle(wb,0,4,0,20000); XSSFWorkbook wb = new XSSFWorkbook(is);
//保存 int sheet_index = 1;
FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx"); int row_index = 3;
wb.write(fileOut); int col_index = 7;
//关闭Excel XSSFCell cell = wb.getSheetAt(sheet_index).getRow(row_index).getCell(col_index);
wb.close(); Object value = getValue(cell);
System.out.println(value);
} }
} }

Binary file not shown.
Loading…
Cancel
Save