//生成Excel类
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExpExcl {
/**
* 初始化数据
* @return
*/
public List getData(){
ResultSet rs = null;
List list = new ArrayList();
Datathplyyvch data = new Datathplyyvch();
data.setAreano("地区号111");
data.setNetdotno("网点111");
Datathplyyvch data1 = new Datathplyyvch();
data1.setAreano("地区号222");
data1.setNetdotno("网点222");
list.add(data);
list.add(data1);
return list;
}
public void expExcel(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//设置文件名
SimpleDateFormat date2Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//导出日期
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);
WritableWorkbook book = Workbook.createWorkbook(new File("d:/测试"+dateStr+".xls"));
WritableSheet sheet = book.createSheet("影印日志", 0);
//标题行格式化
WritableFont wf = new WritableFont(WritableFont.TIMES,16, WritableFont.BOLD,false);
WritableCellFormat wcf = new WritableCellFormat(wf);//实例化文字格式化
wcf.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中
//打印日期行格式化
WritableFont wf2 = new WritableFont(WritableFont.TIMES,10, WritableFont.NO_BOLD,false);
WritableCellFormat wcf2 = new WritableCellFormat(wf2);//实例化文字格式化
wcf2.setAlignment(jxl.format.Alignment.LEFT);//左右居中
// wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中
//列名格式化
WritableFont wf3 = new WritableFont(WritableFont.TIMES,13, WritableFont.BOLD,false);
WritableCellFormat wcf3 = new WritableCellFormat(wf3);//实例化文字格式化
wcf2.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
// wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中
sheet.insertRow(0);
sheet.mergeCells(0, 0, 18, 0);//合并单元格
sheet.addCell(new Label(0, 0, "批量验印日志",wcf));
sheet.setRowView(0, 800);//设置高度
sheet.insertRow(1);
sheet.mergeCells(0, 1, 18, 1);
sheet.addCell(new Label(0, 1, "导出日期:"+date2Format.format(date),wcf2));
sheet.insertRow(2);
sheet.addCell(new Label(0,2,"地区号",wcf3));
sheet.setColumnView(0, 10);//设置列宽度
sheet.addCell(new Label(1,2,"网点",wcf3));
List list = this.getData();
for(int i=3;i<list.size()+3;i++){
Datathplyyvch data = (Datathplyyvch)list.get(i-3);
sheet.insertRow(i);
sheet.addCell(new Label(0, i, data.getAreano()));
sheet.addCell(new Label(1, i, data.getNetdotno()));
}
book.write();
book.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ExpExcl ee = new ExpExcl();
ee.expExcel();
}
}
//实体类
public class Datathplyyvch {
private String areano; // VARCHAR2(8) NOT NULL, --地区号
private String netdotno; //VARCHAR2(8) NOT NULL, --网点
public String getAreano() {
return areano;
}
public void setAreano(String areano) {
this.areano = areano;
}
public String getNetdotno() {
return netdotno;
}
public void setNetdotno(String netdotno) {
this.netdotno = netdotno;
}
}