package com.fuyin.until;
import java.io.File;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import java.sql.*;
import java.util.*;
import com.fuyin.entity.BhyfDownload;
public class JavatoExcel {
/**
* 导出Excel表
* @param rs 数据库结果集
* @param filePath 要保存的路径,文件名为 fileName.xls
* @param sheetName 工作簿名称 工作簿名称,本方法目前只支持导出一个Excel工作簿
* @param columnName 列名,类型为Vector
*/
public void WriteExcel(List list,HashMap map ,String filePath, String sheetName, Vector columnName) {
WritableWorkbook workbook = null;
WritableSheet sheet = null;
Vector alltitle = null;
Vector all = null;
try {
workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
alltitle=new Vector(); //总数标题
alltitle.add("总记录数");
alltitle.add("成功记录数");
alltitle.add("成功总金额");
this.writeCol(sheet, alltitle, 0); // 首先将列名写入
all=new Vector(); //总数
all.add(map.get("allnumber")+"");
all.add(map.get("number")+"");
all.add(map.get("money")+"");
this.writeCol(sheet, all, 1); // 将内容写入
int rowNum = 5; // 从第五行开始写入
this.writeCol(sheet, columnName, 4); // 首先将列名写入
// 将结果集写入
for(int a=0;a
Vector col = new Vector(); // 用以保存一行数据
// 将一行内容保存在col中
col.add(list.get(a).getPlat_transno());
col.add(list.get(a).getPartnerNo());
col.add(list.get(a).getTotal_fee());
col.add(list.get(a).getOut_trade_no());
col.add(list.get(a).getCurrencyType());
col.add(list.get(a).getTrade_state());
col.add(list.get(a).getTrans_type());
col.add(list.get(a).getPlatCharge());
col.add(list.get(a).getSubPlatCharge());
col.add(list.get(a).getTime_start());
// 写入Excel
this.writeCol(sheet, col, rowNum++);
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
// 关闭
workbook.write();
workbook.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
/***
* 将数组写入工作簿
* @param sheet 要写入的工作簿
* @param col 要写入的数据数组
* @param rowNum 要写入哪一行
* @throws WriteException
* @throws RowsExceededException
*/
private void writeCol(WritableSheet sheet, Vector col, int rowNum) throws RowsExceededException, WriteException {
int size = col.size(); // 获取集合大小
for(int i = 0; i < size; i++) { // 写入每一列
Label label = new Label(i, rowNum, (String) col.get(i));
sheet.addCell(label);
}
}
}
}