package cn.cerc.mis.excel.output;

import cn.cerc.core.DataSet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class DataSetFile {
    private ExcelTemplate template;
    private DataSet dataSet;
    private String fileName;

    public DataSetFile(DataSet dataSet) {
        this.dataSet = dataSet;
    }

    public void save() throws IOException, WriteException {
        save(this.fileName);
    }

    public void save(String fileName) throws IOException, WriteException {
        setFileName(fileName);
        OutputStream os = new FileOutputStream(fileName);
        ExcelTemplate template = this.getTemplate();
        template.setFileName(fileName);
        template.setDataSet(dataSet);
        if (template.getColumns().size() == 0) {
            for (String field : dataSet.getFieldDefs()) {
                StringColumn column = new StringColumn();
                column.setCode(field);
                column.setName(field);
                template.addColumn(column);
            }
        }
        // 创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        // 创建新的一页
        WritableSheet sheet = workbook.createSheet("Sheet1", 0);
        template.output(sheet);
        // 把创建的内容写入到输出流中，并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }

    public ExcelTemplate getTemplate() {
        if (template == null) {
            template = new ExcelTemplate();
            template.setColumns(new ArrayList<>());
        }
        return template;
    }

    public void setTemplate(ExcelTemplate template) {
        this.template = template;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

@@ -28,16 +28,135 @@
 utput;%0A%0A
+import java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream;%0Aimport java.util.ArrayList;%0A%0A
 import c
@@ -302,127 +302,8 @@
 n;%0A%0A
-import java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream;%0Aimport java.util.ArrayList;%0A%0A
 publ
@@ -995,16 +995,28 @@
 ldDefs()
+.getFields()
 ) %7B%0A    

Update DataSetFile.java