package com.youle;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class parseExcel {
static HSSFWorkbook readWorkBook;
public static int compare_date(String DATE1, String DATE2,String DATE3) {
DateFormat df = new SimpleDateFormat("hh:mm:ss");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
Date dt3 = df.parse(DATE3);
if(dt1.getTime() > dt3.getTime()){
return 0;
}
if (dt1.getTime() > dt2.getTime()) {
//System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
//System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
public void readExcel(String xlsFilePath) throws IOException{
FileInputStream fIn = new FileInputStream(xlsFilePath);
readWorkBook = new HSSFWorkbook(fIn);
HSSFSheet readSheet = readWorkBook.getSheetAt(0);
HSSFRow readRow = null;
HSSFCell readCell = null;
for(int i= 1;i<= readSheet.getLastRowNum();i++){
readRow = readSheet.getRow(i);
for(short j=0;j<readRow.getLastCellNum();j++){
readCell = readRow.getCell(j);
if(getCellValue(readCell) != null && !getCellValue(readCell).equals("")){
if(Pattern.matches("^[0-9].*",getCellValue(readCell))){
String cvalue = getCellValue(readCell).replaceAll(" ","=");
String t[] = getCellValue(readCell).split(" ");
int b = 0;
if(t != null && t.length >= 1){
b = compare_date(t[1],"9:16:00","18:00:00");
if(b == 1){
setStyle(readCell);
System.out.println("第:" +"i="+i+",j="+j+"单元:"+t[1]);
}
}
//int b = compare_date(getCellValue(readCell),"9:16","18:00:00");
//System.out.println("第:" +"i="+i+",j="+j+"单元:"+","+getCellValue(readCell));
}
}
}
}
FileOutputStream out = null;
try{
out = new FileOutputStream("D:/work_space/bb.xls");
readWorkBook.write(out);
}catch (Exception e){
}
}
public static void setStyle(HSSFCell cell){
HSSFCellStyle cellStyle = readWorkBook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.RED.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
}
private String getCellValue(Cell cell) {
String value = null;
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
// cell.getCellFormula();
try {
value = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
value = String.valueOf(cell.getRichStringCellValue());
} finally {
break;
}
case Cell.CELL_TYPE_NUMERIC:
value = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
value = String.valueOf(cell.getRichStringCellValue());
break;
}
}
return value;
}
public static void main(String[] arg) throws IOException{
parseExcel pe = new parseExcel();
pe.readExcel("D:/work_space/23.xls");
//System.out.println(System.getProperties());
}
}