很简单,这有代码:代码里面数据库访问对象自己写,用到第三方架包:

poi-2.5-final-20040302.jar;

poi-contrib-2.5-final-20040302.jar

poi-scratchpad-2.5-final-20040302.jar

自己去网上下吧

//处理类
public class CountExcel {
/**
* 数据库操作类,自己写吧。。。
*/
private OperData jdbc /*自己写*/;
/**
* 创建excel,返回excel的存放地址
*
* @param title
* 标题
* @param sql
* 查询语句
* @param path
* excel的存放路径(物理地址)
* @param titlename
* 报表的名字
* @return 路径
*/
public String createExcelForAssess(String[] title, String sql, String path,
String titlename) throws Exception {
GregorianCalendar c = new GregorianCalendar();
StringBuffer excelUrl = new StringBuffer();
java.util.Date now = c.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
String strNow = format.format(now);
excelUrl.append(path).append(File.separator).append("te_").append(
strNow).append(".xls");
// excel的表头
Vector vcCol = new Vector();
if (title == null || title.length < 1)
return "";
for (int i = 0; i < title.length; i++) {
vcCol.add(title[i]);
}
int[] align = new int[vcCol.size()];
int[] num = new int[vcCol.size()];
for (int i = 0; i < vcCol.size(); i++) {
align[i] = 2;
num[i] = 0;
}
Vector vc = getqueryrest(sql);
ExcelReport excel = new ExcelReport();
excel.setExcelFile(excelUrl.toString());
excel.setCols(vcCol.size());
excel.createCaption(titlename);
excel.createColumnCaption(vcCol);
excel.createBody(vc, align, num);
excel.createPage(5);
excel.createFile();
return excelUrl.toString();
}
/**
* 查询结果集
*
* @param sql传入查询的sql语句
* @return Vector
* @throws SQLException
*/
public Vector getqueryrest(String sql) throws SQLException {
Vector vc = new Vector();
//数据库查询,返回的list里面存的是数据的pojo对象
//List list = jdbc.getQueryResult(sql);
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
String[] info = (String[]) list.get(i);
for (int j = 0; j < info.length; j++) {
vc.add(info[j]);
}
}
}
return vc;
}
}
//工具类
public class ExcelReport{
/**
* EXCEL文件工作区
*/
private HSSFWorkbook wb;
/**
* EXCEL文件SHEET
*/
private HSSFSheet sheet;
/**
* EXCEL文件存放的目录路径
*/
private String excelFile = "";
/**
* 记录当前行数
*/
private int rownum = 0;
/**
* 记录总共列数
*/
private int cols = 0;
/**
* 记录每列的宽度
*/
private int[] length;
/**
* 记录是否已经设定字段数
*/
private boolean flag = false;
/**
* 设置EXCEL文件存放的目录路径,在生成标题,列头前设定
*/
public void setExcelFile(String excelFile){
this.excelFile = excelFile;
}
/**
* 设置EXCEL表的列数,在生成标题,列头前设定
*/
public void setCols(int cols){
this.cols = cols;
if(flag){
if(length.length < cols){
length = getLength(length);
}
}else{
length = new int[cols];
}
flag = true;
}
/**
* 第二次设定字段数,保存第一张表格每列的长度
*/
private int[] getLength(int[] arr){
int[] temp = new int[cols];
for(int i=0;i
temp[i] = arr[i];
}
return temp;
}
/**
* 初始化EXCEL报表类
*/
public ExcelReport(){
wb = new HSSFWorkbook();
sheet = wb.createSheet("new sheet");
}
/**
* 生成标题
* @param caption 标题头
*/
public void createCaption(String caption){
//生成标题行
HSSFRow row = sheet.createRow((short)rownum);
//生成标题单元格
HSSFCell cell = row.createCell((short)0);
//生成标题单元格样式
HSSFCellStyle style = wb.createCellStyle();
//设定标题字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)14);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
cell.setCellValue(caption);
cell.setCellStyle(style);
for(int i=1;i
cell = row.createCell((short)i);
cell.setCellStyle(style);
cell.setCellValue("");
}
//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));
//当前行自增
rownum++;
}
/**
* 生成列头
* @param vc 列头内容
*/
public void createColumnCaption(Vector vc){
//生成列头行
HSSFRow row = sheet.createRow((short)rownum);
//生成了列头格式
HSSFCellStyle style = wb.createCellStyle();
//设定列头字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);
//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//生成列头单元格
HSSFCell cell;
for(int i=0;i
//生成标题单元格
cell = row.createCell((short)i);
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
if(vc.get(i) != null){
cell.setCellValue(vc.get(i).toString());
//记录列头的长度
if(vc.get(i).toString().getBytes().length > length[i]){
length[i] = vc.get(i).toString().getBytes().length;
}
}else{
cell.setCellValue("");
}
cell.setCellStyle(style);
}
rownum++;
}
/**
* 生成合并的列头
* @param vc 列头
* @param colnum 每列要合并的列数,并且所有要合并的列数之和等于总表格列数
*/
public void mergeColumnCaption(Vector vc,int[] colnum){
//生成标题行
HSSFRow row = sheet.createRow((short)rownum);
//生成标题单元格样式
HSSFCellStyle style = wb.createCellStyle();
//设定标题字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);
//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
int pos = 0;
HSSFCell cell;
for(int i=0;i
pos = pos + colnum[i];
cell = row.createCell((short)(pos-colnum[i]));
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
cell.setCellStyle(style);
if(vc.get(i) == null){
cell.setCellValue("");
}else{
cell.setCellValue(vc.get(i).toString());
}
for(int j=1;j
cell = row.createCell((short)(pos-colnum[i]+j));
cell.setCellStyle(style);
cell.setCellValue("");
}
//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)(pos-colnum[i]),rownum,(short)(pos-1)));
}
//当前行自增
rownum++;
}
/**
* 合并行
* @param startrow 起始行
* @param endrow 终止行
* @param column 列数
*/
public void mergeRowCaption(int startrow,int endrow,int column){
sheet.addMergedRegion(new Region(startrow,(short)column,endrow,(short)column));
}
/**
* 生成表格主体
* @param vc 表格内容
* @param align 每列的对齐方式,1为左,2为中,3为右,数组长度要等于表格列数
* @param num 每列的数据类型,0为字符串,1为数字
*/
public void createBody(Vector vc,int[] align,int[] num){
int rows = vc.size() / cols;
//设定表格字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFRow row;
HSSFCell cell;
//设定数据格式
HSSFDataFormat df = wb.createDataFormat();
//生成了左对齐表格格式
HSSFCellStyle styleLeft = wb.createCellStyle();
styleLeft.setFont(font);
styleLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleLeft.setBottomBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleLeft.setLeftBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleLeft.setRightBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleLeft.setTopBorderColor(HSSFColor.BLACK.index);
styleLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
styleLeft.setDataFormat(df.getFormat("##################.00"));
//生成居中对齐表格格式
HSSFCellStyle styleCenter = wb.createCellStyle();
styleCenter.setFont(font);
styleCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleCenter.setBottomBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleCenter.setLeftBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleCenter.setRightBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleCenter.setTopBorderColor(HSSFColor.BLACK.index);
styleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleCenter.setDataFormat(df.getFormat("##################.00"));
//生成右对齐表格格式
HSSFCellStyle styleRight = wb.createCellStyle();
styleRight.setFont(font);
styleRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleRight.setBottomBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleRight.setLeftBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleRight.setRightBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleRight.setTopBorderColor(HSSFColor.BLACK.index);
styleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
styleRight.setDataFormat(df.getFormat("##################.00"));
for (int i = 0;i < rows; i++){
// 创建新行
row = sheet.createRow((short)(rownum));
for (int j = 0;j < cols; j++){
// 创建一个单元格
cell = row.createCell((short)j);
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置cell字符类型的值
if(vc.get(i*cols+j) == null){
cell.setCellValue("");
}else{
if(num[j] == 0){
cell.setCellValue(vc.get(i*cols+j).toString());
}else{
cell.setCellValue(Double.parseDouble(vc.get(i*cols+j).toString()));
}
//记录每列的长度
if(vc.get(i*cols+j).toString().getBytes().length > length[j]){
length[j] = vc.get(i*cols+j).toString().getBytes().length;
}
}
//设定对齐方式
if(align[j] == 1){
cell.setCellStyle(styleLeft);
}
if(align[j] == 2){
cell.setCellStyle(styleCenter);
}
if(align[j] == 3){
cell.setCellStyle(styleRight);
}
}
rownum++;
}
}
/**
* 生成统计结果行
* @param stat 统计结果
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createStat(String stat,int align){
//生成统计结果行
HSSFRow row = sheet.createRow((short)rownum);
//生成统计结果格
HSSFCell cell = row.createCell((short)0);
//生成统计结果单元格样式
HSSFCellStyle style = wb.createCellStyle();
//设定统计结果字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
//设定对齐方式
if(align == 1){
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
}
if(align == 2){
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
}
if(align == 3){
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
}
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
cell.setCellValue(stat);
cell.setCellStyle(style);
for(int i=1;i
cell = row.createCell((short)i);
cell.setCellStyle(style);
cell.setCellValue("");
}
//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));
//当前行自增
rownum++;
}
/**
* 设置页眉
* @param header 页眉内容
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createHeader(String header,int align){
HSSFHeader head = sheet.getHeader();
if(align == 1){
head.setLeft(header);
}
if(align == 2){
head.setCenter(header);
}
if(align == 3){
head.setRight(header);
}
}
/**
* 设置页脚
* @param footer 页脚内容
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createFooter(String footer,int align){
HSSFFooter foot = sheet.getFooter();
if(align == 1){
foot.setLeft(footer);
}
if(align == 2){
foot.setCenter(footer);
}
if(align == 3){
foot.setRight(footer);
}
}
/**
* 设定页脚的页面值
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createPage(int align){
HSSFFooter foot = sheet.getFooter();
if(align == 1){
foot.setLeft("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
if(align == 2){
foot.setCenter("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
if(align == 3){
foot.setRight("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
}
/**
* 生成EXCEL文件
*/
public void createFile() throws Exception{
for(int i=0;i
sheet.setColumnWidth((short)i,(short)(length[i]*2*200));
}
FileOutputStream fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
fileOut.close();
}
}