1.导入jar包jxl.jar
2.日志model
package com.yinlianhang.log4j.javabean;
public class MyLog {
public String status;//状态
public String operator;//操作人
public String actions;//动作
public String time;//时间
public String remarks;//备注
public MyLog(String status, String operator, String actions, String time,
String remarks) {
super();
this.status = status;
this.operator = operator;
this.actions = actions;
this.time = time;
this.remarks = remarks;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getActions() {
return actions;
}
public void setActions(String actions) {
this.actions = actions;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
3.日志管理类
package com.yinlianhang.log4j.javabean;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class LogToExel {
public static Workbook rwb;
public static WritableWorkbook wwb;
/**
* 记录日志,每天一张一只表,每月一个exel文件
*
* @param myLog
* 日志对象
*/
public static void addLog(MyLog myLog) {
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLUE);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
WritableSheet ws = getWritableSheet();// 拿到可写入日志的工作簿
if (null != ws) {
// 判断单元格的类型,做出相应的转换
try {
int rows = ws.getRows();// 得到行数
Label labelCF1 = new Label(0, rows, myLog.getStatus(), wcfFC);
Label labelCF2 = new Label(1, rows, myLog.getOperator(), wcfFC);
Label labelCF3 = new Label(2, rows, myLog.getActions(), wcfFC);
Label labelCF4 = new Label(3, rows, myLog.getTime(), wcfFC);
Label labelCF5 = new Label(4, rows, myLog.getRemarks(), wcfFC);
ws.addCell(labelCF1);
ws.addCell(labelCF2);
ws.addCell(labelCF3);
ws.addCell(labelCF4);
ws.addCell(labelCF5);
wwb.write();
wwb.close();
if (null != rwb) {
rwb.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 获取可插入数据的工作簿
*
* @return
*/
public static WritableSheet getWritableSheet() {
File f = new File("D:/" + getMonthString() + ".xls");
try {
if (f.exists()) {// 文件存在,可以直接写
rwb = Workbook.getWorkbook(f);
wwb = Workbook.createWorkbook(f, rwb);
WritableSheet sheet = wwb.getSheet(getDayString());
if (null != sheet) {
return sheet;
} else {
return wwb.createSheet(getDayString(), wwb
.getNumberOfSheets());
}
} else {// 文件不存在,需要新建
wwb = Workbook.createWorkbook(new File("D:/" + getMonthString()+ ".xls"));
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
WritableSheet sheet = wwb.createSheet(getDayString(), 0);//注意:如果新建表格而没有初始化的话,会格式与打开方式类型不一致的错误
Label labelCF1 = new Label(0, 0, "状态", wcfFC);
Label labelCF2 = new Label(1, 0, "操作人", wcfFC);
Label labelCF3 = new Label(2, 0, "动作", wcfFC);
Label labelCF4 = new Label(3, 0, "时间", wcfFC);
Label labelCF5 = new Label(4, 0, "备注", wcfFC);
try {
sheet.addCell(labelCF1);
sheet.addCell(labelCF2);
sheet.addCell(labelCF3);
sheet.addCell(labelCF4);
sheet.addCell(labelCF5);
return sheet;
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 获取某年某月某日的时间字符串
*
* @return
*/
public static String getDayString() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日");
return f.format(cal.getTime());
}
/**
* 获取某年某月的时间字符串
*
* @return
*/
public static String getMonthString() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月");
return f.format(cal.getTime());
}
}
4.测试
LogToExel.addLog(new MyLog("成功","测试者","测试记录数据",new Date().toString(),"这是个测试数据"));