⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnumericwriter.java

📁 非常实用的java描述的excel组件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            writeRow(row, out, indent + INDENT);
        }
        out.println(indent + "</gmr:Cells>");

    }

    /**
     * Writes a solver element to the stream.
     * 
     * @param solver  the solver.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeSolver(Solver solver, PrintStream out, String indent) {

        out.println(indent + "<gmr:Solver TargetCol=\"" + solver.getTargetColumn()
                           + "\" TargetRow=\"" + solver.getTargetRow()
                           + "\" ProblemType=\"" + solver.getProblemType()
                           + "\" Inputs=\"" + solver.getInputs() + "\"/>");
    }

    /**
     * Writes a names element in the Gnumeric format.
     * 
     * @param names  the names manager.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeWorksheetNames(NamesManager names, PrintStream out, String indent) {

        Iterator iterator = names.getNamesIterator();
        while (iterator.hasNext()) {
            Name current = (Name) iterator.next();
            out.println(indent + "<gmr:Name>");
            out.println(indent + INDENT + "<gmr:name>" + current.getName() + "</gmr:name>");
            out.println(indent + INDENT + "<gmr:value>" + current.getValue() + "</gmr:value>");
            out.println(indent + "</gmr:Name>");
        }

    }

    /**
     * Writes the margins element to the stream.
     * 
     * @param margins  the margins.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeMargins(Margins margins, PrintStream out, String indent) {

        out.println(indent + "<gmr:Margins>");

        String nextIndent = indent + INDENT;
        writeMargin(out, "top", margins.getTopPts(), nextIndent);
        writeMargin(out, "bottom", margins.getBottomPts(), nextIndent);
        writeMargin(out, "left", margins.getLeftPts(), nextIndent);
        writeMargin(out, "right", margins.getRightPts(), nextIndent);
        writeMargin(out, "header", margins.getHeaderPts(), nextIndent);
        writeMargin(out, "footer", margins.getFooterPts(), nextIndent);

        out.println(indent + "</gmr:Margins>");

    }

    /**
     * Writes a single margin element to the stream.
     * 
     * @param out  the output stream.
     * @param tag  the tag name.
     * @param pts  the points.
     * @param indent  the indentation.
     */
    private void writeMargin(PrintStream out, String tag, double pts, String indent) {
        out.println(indent + "<gmr:" + tag + " Points=\"" + pts + "\" PrefUnit=\"cm\"/>");
    }

    /**
     * Writes a style border element to the stream.
     * 
     * @param style  the style.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeBorderStyle(Border style, PrintStream out, String indent) {

        String nextIndent = indent + INDENT;

        out.println(indent + "<gmr:StyleBorder>");

        out.println(nextIndent + "<gmr:Top Style=\"" + style.getTop() + "\" Color=\"0:0:0\"/>");
        out.println(nextIndent + "<gmr:Bottom Style=\"" + style.getBottom() 
                               + "\" Color=\"0:0:0\"/>");
        out.println(nextIndent + "<gmr:Left Style=\"" + style.getLeft() + "\" Color=\"0:0:0\"/>");
        out.println(nextIndent + "<gmr:Right Style=\"" + style.getRight() + "\" Color=\"0:0:0\"/>");
        out.println(nextIndent + "<gmr:Diagonal Style=\"" + style.getDiagonal() 
                               + "\" Color=\"0:0:0\"/>");
        out.println(nextIndent + "<gmr:Rev-Diagonal Style=\"" + style.getReverseDiagonal()
                                                              + "\" Color=\"0:0:0\"/>");

        out.println(indent + "</gmr:StyleBorder>");

    }

    /**
     * Writes a date cell.
     * 
     * @param cell  the cell.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeDateCell(Cell cell, PrintStream out, String indent) {

        out.println(indent + "<gmr:Cell Col=\""
                           + cell.getColumn() + "\" Row=\""
                           + cell.getRow() + "\" ValueType=\"40\" ValueFormat=\"d-mmm-yyyy\">");
        writeCellContent(cell, out, indent + INDENT);
        out.println(indent + "</gmr:Cell>");

    }

    /**
     * Write the cell element.  So far, it will work for labels, numbers and expressions.  Of
     * course, the intention is to ensure that it works for all types.
     * 
     * @param cell  the cell.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeCell2(Cell cell, PrintStream out, String indent) {

        out.println(indent + "<gmr:Cell Col=\"" + cell.getColumn()
                           + "\" Row=\"" + cell.getRow() + "\">");
        writeCellContent(cell, out, indent + INDENT);
        out.println(indent + "</gmr:Cell>");

    }

    /**
     * Writes a style element to the stream.
     * 
     * @param style  the style.
     * @param out  the stream.
     * @param indent  the indentation.
     */
    public void writeStyle(Style style, PrintStream out, String indent) {

        out.println(indent + "<gmr:Style "
                           + "HAlign=\"" + style.getHorizontalAlignment()
                           + "\" VAlign=\"" + style.getVerticalAlignment()
                           + "\" WrapText=\"" + (style.isWrapText() ? "1" : "0")
                           + "\" Orient=\"" + style.getOrientation()
                           + "\" Shade=\"" + style.getShade()
                           + "\" Indent=\"" + style.getCellIndent()
                           + "\" Fore=\"" + style.getForegroundColor().toString()
                           + "\" Back=\"" + style.getBackgroundColor().toString()
                           + "\" PatternColor=\"" + style.getPatternColor().toString()
                           + "\" Format=\"" + style.getFormat()
                           + "\">");

        String nextIndent = indent + INDENT;
        writeFontStyle(style.getFont(), out, nextIndent);
        writeBorderStyle(style.getBorder(), out, nextIndent);

        out.println(indent + "</gmr:Style>");

    }

    /**
     * Write the cell element.  So far, it will work for labels, numbers and expressions.  Of
     * course, the intention is to ensure that it works for all types.
     * 
     * @param cell  the cell.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeCell(Cell cell, PrintStream out, String indent) {

        int type = cell.getType();

        if (type == 0) {
            out.println(indent + "<gmr:Cell Col=\"" + cell.getColumn() + "\" Row=\""
                                                    + cell.getRow() + "\" ExprID=\"1\">");
            writeCellContent(cell, out, indent + INDENT);
            out.println(indent + "</gmr:Cell>");
        }
        else if (type == 30) {
            out.println(indent + "<gmr:Cell Col=\"" + cell.getColumn() + "\" Row=\""
                                                    + cell.getRow() + "\" ValueType=\"30\">");
            writeCellContent(cell, out, indent + INDENT);
            out.println(indent + "</gmr:Cell>");
        }
        else if (type == 40) {
            out.println(indent + "<gmr:Cell Col=\"" + cell.getColumn() + "\" Row=\""
                                                    + cell.getRow() + "\" ValueType=\"40\">");
            writeCellContent(cell, out, indent + INDENT);
            out.println(indent + "</gmr:Cell>");
        }
        else if (type == 60) {
            out.println(indent + "<gmr:Cell Col=\"" + cell.getColumn() + "\" Row=\""
                                                    + cell.getRow() + "\" ValueType=\"60\">");
            writeCellContent(cell, out, indent + INDENT);
            out.println(indent + "</gmr:Cell>");
        }

    }

    /**
     * Write the cell contents.
     * 
     * @param cell  the cell.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    protected void writeCellContent(Cell cell, PrintStream out, String indent) {

        out.println(indent + "<gmr:Content>" + cell.getContent() + "</gmr:Content>");

    }

    /**
     * Writes a style region element to the stream.
     * 
     * @param region  the style region.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeStyleRegion(StyleRegion region, PrintStream out, String indent) {

        out.println(indent + "<gmr:StyleRegion "
                           + "startCol=\"" + region.getStartColumn()
                           + "\" startRow=\"" + region.getStartRow()
                           + "\" endCol=\"" + region.getEndColumn()
                           + "\" endRow=\"" + region.getEndRow()
                           + "\">");

        writeStyle(region.getStyle(), out, indent + INDENT);

        out.println(indent + "</gmr:StyleRegion>");

    }
    /**
     * Writes a row element to the stream.
     * 
     * @param row  the row.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeRow(Row row, PrintStream out, String indent) {

        Iterator iterator = row.getCellsIterator();
        while (iterator.hasNext()) {
            Cell cell = (Cell) iterator.next();
            int type = cell.getType();
            if (type == Cell.EXPRESSION_TYPE) {
                writeCell(cell, out, indent);
            }
            else if (type == Cell.DATE_TYPE) {
                writeDateCell(cell, out, indent);
            }
            else if (type == Cell.VALUE_TYPE) {
                writeCell(cell, out, indent);
            }
            else if (type == Cell.LABEL_TYPE) {
                writeCell(cell, out, indent);
            }
        }

    }

    /**
     * Writes a style font element to the stream.
     * 
     * @param font  the font style.
     * @param out  the stream for output.
     * @param indent  the indentation.
     */
    public void writeFontStyle(FontStyle font, PrintStream out, String indent) {

        String boldText = (font.isBold() ? "1" : "0");
        String italicText = (font.isItalic() ? "1" : "0");
        String underlineText = (font.isUnderline() ? "1" : "0");
        String strikethroughText = (font.isStrikethrough() ? "1" : "0");

        out.println(indent + "<gmr:Font Unit=\"" + font.getSize() + "\" Bold=\"" + boldText
                                                 + "\" Italic=\"" + italicText
                                                 + "\" Underline=\"" + underlineText
                                                 + "\" StrikeThrough=\"" + strikethroughText
                                                 + "\">" + font.getName() + "</gmr:Font>");

    }

    /**
     * Writes the columns element to the specified stream in the Gnumeric format.
     * 
     * @param attributes  the column attributes.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeColInfo(ColumnAttributes attributes, PrintStream out, String indent) {

        out.println(indent + "<gmr:ColInfo "
                           + "No=\"" + attributes.getStartColumn() + "\" "
                           + "Unit=\"" + attributes.getWidth() + "\" "
                           + "MarginA=\"" + attributes.getMarginA() + "\" "
                           + "MarginB=\"" + attributes.getMarginB() + "\" "
                           + "HardSize=\"" + (attributes.isHardSize() ? "1" : "0") + "\" "
                           + "Hidden=\"" + (attributes.isHidden() ? "1" : "0") + "\" "
                           + "Count=\"" 
                           + (attributes.getEndColumn() - attributes.getStartColumn() + 1)
                           + "\"/>");

    }

    /**
     * Writes the rows element to the specified stream in the Gnumeric format.
     * 
     * @param attributes  the row attributes.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeRowInfo(RowAttributes attributes, PrintStream out, String indent) {

        out.println(indent + "<gmr:RowInfo No=\"" + attributes.getStartRow() + "\" "
                           + "Unit=\"" + attributes.getHeight() + "\" "
                           + "MarginA=\"" + attributes.getMarginA() + "\" "
                           + "MarginB=\"" + attributes.getMarginB() + "\" "
                           + "HardSize=\"" + (attributes.isHardSize() ? "1" : "0") + "\" "
                           + "Hidden=\"" + (attributes.isHidden() ? "1" : "0") + "\" "
                           + "Count=\"" + (attributes.getEndRow() - attributes.getStartRow() + 1)
                           + "\"/>");

    }

    /**
     * Writes a comment element in the Gnumeric file format.
     * 
     * @param comment  the comment.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeComment(Comment comment, PrintStream out, String indent) {

        String cellReference = Worksheet.cellReference(comment.getRow(), comment.getColumn());
        out.println(indent + "<gmr:CellComment Author=\"" + comment.getAuthor()
                           + "\" Text=\"" + comment.getText()
                           + "\" ObjectBound=\"" + cellReference
                           + "\" ObjectOffset=\"" + "0 0 0 0"
                           + "\" ObjectAnchorType=\"" + "33 32 33 32"
                           + "\"/>");

    }

    /**
     * Writes a selection element to the stream.
     * 
     * @param selection  the selection.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeSelection(Selection selection, PrintStream out, String indent) {

        out.println(indent + "<gmr:Selection "
                           + "startCol=\"" + selection.getStartColumn() + "\" "
                           + "startRow=\"" + selection.getStartRow() + "\" "
                           + "endCol=\"" + selection.getEndColumn() + "\" "
                           + "endRow=\"" + selection.getEndRow()
                           + "\"/>");

    }

}

⌨️ 快捷键说明

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