opwriter.java

来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 172 行

JAVA
172
字号
package com.corba.mnq.xls;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/*
 * file format
 * -----------
 * opname,cname,ior
 * id1,value1
 * id2,value2
 */
public class OpWriter {

    public static String EOF = "EOF"; // end of file

    public static String SHEET = "OP";

    public static String XXX_NAME = "OP_XXX";

    private static common.Logger logger = common.Logger.getLogger(OpWriter.class);

    private File file;

    private WritableWorkbook book;

    private WritableSheet sheet;

    private WritableSheet xxx;

    private int currentLine = 1; // 0 was reserved by writeOp

    public boolean open(String fileName) {
        try {
            logger.setSuppressWarnings(Boolean.getBoolean("jxl.nowarnings"));
            file = new File(fileName);
            book = Workbook.createWorkbook(file);
            sheet = book.createSheet(SHEET, 0);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    
    public boolean openBoth(String fileName) {
        try {
            logger.setSuppressWarnings(Boolean.getBoolean("jxl.nowarnings"));
            file = new File(fileName);
            book = Workbook.createWorkbook(file);
            sheet = book.createSheet(SHEET, 0);
            xxx = book.createSheet(XXX_NAME, 1);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    
    public boolean openXXX(String fileName) {
        try {
            logger.setSuppressWarnings(Boolean.getBoolean("jxl.nowarnings"));
            file = new File(fileName);
            book = Workbook.createWorkbook(file);
            xxx = book.createSheet(XXX_NAME, 0);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean writeOp(String opName, String cname, String ior) {
        Label lb1 = new Label(0, 0, opName);
        Label lb2 = new Label(1, 0, cname);
        Label lb3 = new Label(2, 0, ior);

        try {
            sheet.addCell(lb1);
            sheet.addCell(lb2);
            sheet.addCell(lb3);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean writeOpXXX(String opName, String cname, String ior) {
        Label xlb1 = new Label(0, 0, opName);
        Label xlb2 = new Label(1, 0, cname);
        Label xlb3 = new Label(2, 0, ior);
        try {
            xxx.addCell(xlb1);
            xxx.addCell(xlb2);
            xxx.addCell(xlb3);

        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean writeNextPar(String id, String value) {
        Label lb1 = new Label(0, currentLine, id);
        Label lb2 = new Label(1, currentLine, value);
        try {
            sheet.addCell(lb1);
            sheet.addCell(lb2);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        currentLine++;
        return true;
    }

    public boolean writeNextParXXX(int col, int line, String value) {
        Label lb1 = new Label(col, line, value);
        try {
            xxx.addCell(lb1);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean close() {
        try {
            Label lb1 = new Label(0, currentLine, EOF);
            sheet.addCell(lb1);
            book.write();
            book.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    
    public boolean closeXXX() {
        try {
            book.write();
            book.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @param args
     */
    public static void main_(String[] args) {
        // TODO Auto-generated method stub
        OpWriter op = new OpWriter();
        op.open("test.xls");
        op.writeOp("op1", "::x::y::z::op1", "123456");
        op.writeNextPar("1.2.3.4", "199");
        op.writeNextPar("2.3.4.5", "hello");
        op.close();
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?