package com.happy.Until; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import java.io.File; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Vector; public class DBtoExcel { /** * 导出Excel表 * * @param rs * 数据库结果集 * @param filePath * 要保存的路径,文件名为 fileName.xls * @param sheetName * 工作簿名称 工作簿名称,本方法目前只支持导出一个Excel工作簿 * @param columnName * 列名,类型为Vector */ public void WriteExcel(ResultSet rs, String filePath, String sheetName, Vector columnName) { WritableWorkbook workbook = null; WritableSheet sheet = null; int rowNum = 1; // 从第2行开始写入 try { workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件 sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿 this.writeCol(sheet, columnName, 0); // 首先将列名写入第一行 // 将结果集写入 while (rs.next()) { Vector col = new Vector(); // 用以保存一行数据 for (int i = 1; i <= columnName.size(); i++) { // 将一行内容保存在col中 col.add(rs.getString(i)); } // 写入Excel this.writeCol(sheet, col, rowNum++); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭 workbook.write(); workbook.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } } } public void WriteExcel2(ResultSet rs, String filePath, String sheetName, Vector columnName, String time1, String time2) { WritableWorkbook workbook = null; WritableSheet sheet = null; int rowNum = 2; // 从第一行开始写入 try { workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件 sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿 this.writeCol(sheet, columnName, 1); // 首先将列名写入 // 写入时间 Vector col2 = new Vector(); // 用以保存一行数据 col2.add("统计时间-:"+time1+"到"+time2); this.writeCol(sheet, col2, 0); // 将结果集写入 while (rs.next()) { Vector col = new Vector(); // 用以保存一行数据 for (int i = 1; i <= columnName.size(); i++) { // 将一行内容保存在col中 col.add(rs.getString(i)); } // 写入Excel this.writeCol2(sheet, col, rowNum++); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭 workbook.write(); workbook.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } } } public void WriteExcel4(ArrayList aa, String filePath, String sheetName, Vector columnName, String time1, String time2) { WritableWorkbook workbook = null; WritableSheet sheet = null; int rowNum = 2; // 从第一行开始写入 try { workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件 sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿 this.writeCol(sheet, columnName, 1); // 首先将列名写入 // 写入时间 Vector col2 = new Vector(); // 用以保存一行数据 col2.add("统计时间-:"+time1+"到"+time2); this.writeCol(sheet, col2, 0); // 将结果集写入 for (int i=0; i> aa, ArrayList al, ArrayList al2, ArrayList al_1, String filePath, String sheetName, Vector columnName, String time1, String time2, String place) { WritableWorkbook workbook = null; WritableSheet sheet = null; int rowNum = 3; // 从第四行开始写入具体数据 try { workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件 sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿 this.writeCol(sheet, columnName, 1); // 首先第二行将列名写入 // 写入时间 Vector col2 = new Vector(); // 用以保存一行数据 col2.add("统计时间-:"+time1+"到"+time2); col2.add(" "); col2.add(" "); col2.add(" "); col2.add(" "); col2.add("订单号:"+place); this.writeCol(sheet, col2, 0); // 第一行写入时间、订单号 // 将结果集写入 for (int i=0; i