package com.happy.Until.Excel;

import com.happy.Model.Admin;
import com.happy.Model.Booking;
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.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

public class toExcel {

    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.valueOf(col.get(i)));
            sheet.addCell(label);
        }
    }

    public void book(List<Vector> list, 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); // 首先将列名写入第一行
            // 将结果集写入
            for (Vector col : list) {
//                col.add(list.get(i).getOrder_num());
//                col.add(list.get(i).getStatus());
//                col.add(list.get(i).getDom());
//                col.add(list.get(i).getUser_name());
//                col.add(list.get(i).getPhone());
//                col.add(list.get(i).getH_type());
//                col.add(list.get(i).getPrice());
//                col.add(list.get(i).getCreate_time());
//                col.add(list.get(i).getPay_account());
                // 写入Excel
                this.writeCol(sheet, col, rowNum++);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭
                workbook.write();
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void out(List<Object> list, 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); // 首先将列名写入第一行
            Field[] fields = list.get(0).getClass().getDeclaredFields();

            // 将结果集写入
            for (int i = 0; i < list.size(); i++) {
                Vector col = new Vector(); // 用以保存一行数据
                for (int j = 0; j < fields.length; j++) {
                    // 获取属性的名字
                    String name = fields[i].getName();
                    // 将属性的首字符大写，方便构造get，set方法
                    name = name.substring(0, 1).toUpperCase() + name.substring(1);
                    // 如果type是类类型，则前面包含"class "，后面跟类名
                    Method m = list.get(0).getClass().getMethod("get" + name);
                    String value = String.valueOf(m.invoke(list.get(0)));
                    col.add(value);
                }
                // 写入Excel
                this.writeCol(sheet, col, rowNum++);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭
                workbook.write();
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
