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

📄 gnumericwriter.java

📁 非常实用的java描述的excel组件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* =============================================================
 * JWorkbook : a free Java library for writing spreadsheet files
 * =============================================================
 *
 * Project Info:  http://www.jfree.org/jworkbook/index.html;
 * Project Lead:  David Gilbert (david.gilbert@object-refinery.com);
 *
 * (C) Copyright 2001-2003, by Object Refinery Limited.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -------------------
 * GnumericWriter.java
 * -------------------
 * (C) Copyright 2001, 2003, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id$
 *
 * Changes:
 * --------
 * 05-Nov-2001 : Version 1 (DG);
 * 11-Feb-2002 : Implemented GnumericWriter.java and XLWriter.java (DG);
 * 07-Jul-2003 : Changed GPL --> LGPL, changed package name, updated company name (DG);
 *
 */

package org.jfree.workbook.io;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;

import org.jfree.workbook.Border;
import org.jfree.workbook.Cell;
import org.jfree.workbook.Cells;
import org.jfree.workbook.ColumnAttributes;
import org.jfree.workbook.ColumnAttributesManager;
import org.jfree.workbook.Comment;
import org.jfree.workbook.FontStyle;
import org.jfree.workbook.JWorkbook;
import org.jfree.workbook.Margins;
import org.jfree.workbook.Name;
import org.jfree.workbook.NamesManager;
import org.jfree.workbook.PrintInformation;
import org.jfree.workbook.Row;
import org.jfree.workbook.RowAttributes;
import org.jfree.workbook.RowAttributesManager;
import org.jfree.workbook.Selection;
import org.jfree.workbook.Selections;
import org.jfree.workbook.Solver;
import org.jfree.workbook.Style;
import org.jfree.workbook.StyleRegion;
import org.jfree.workbook.Styles;
import org.jfree.workbook.Summary;
import org.jfree.workbook.Worksheet;

/**
 * A class that knows how to write a JWorkbook object to the Gnumeric XML file format.
 */
public class GnumericWriter {

    /** Two spaces for indenting. */
    private static final String INDENT = "  ";

    /** The workbook. */
    protected JWorkbook workbook;

    /**
     * Creates a new writer for the specified workbook.
     * 
     * @param workbook  the workbook.
     */
    public GnumericWriter(JWorkbook workbook) {
        this.workbook = workbook;
    }

    /**
     * Saves the workbook in Gnumeric XML format.  We don't gzip the output yet, but that will
     * be simple to add.
     * 
     * @param filename  the name of the file.
     * 
     * @throws IOException if there is an I/O problem.
     */
    public void saveWorkbook(String filename) throws IOException {

        PrintStream out = new PrintStream(
                              new BufferedOutputStream(
                                  new FileOutputStream(filename)
                              )
                          );
        out.println("<?xml version=\"1.0\"?>");
        writeWorkbook(out);
        out.close();

    }

    /**
     * Writes the workbook element to the specified stream.
     * 
     * @param out  the output stream.
     */
    private void writeWorkbook(PrintStream out) {

        out.println("<gmr:Workbook xmlns:gmr=\"http://www.gnome.org/gnumeric/v7\">");

        writeAttributes(out, INDENT);
        writeSummary(out, INDENT);
        writeWorkbookNames(out, INDENT);
        writeGeometry(out, INDENT);

        out.println(INDENT + "<gmr:Sheets>");

        Iterator iterator = this.workbook.getWorksheetsIterator();
        while (iterator.hasNext()) {

            Worksheet sheet = (Worksheet) iterator.next();
            writeWorksheet(sheet, out, INDENT + INDENT);

        }
        out.println(INDENT + "</gmr:Sheets>");

        writeUIData(out, INDENT);

        out.println("</gmr:Workbook>");

    }

    /**
     * Writes the attributes element to the specified stream.
     * 
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeAttributes(PrintStream out, String indent) {

        String nextIndent = indent + INDENT;

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

        String horizontal = (this.workbook.isHorizontalScrollBarVisible() ? "TRUE" : "FALSE");
        writeAttribute(out, "WorkbookView::show_horizontal_scrollbar", 4, horizontal, nextIndent);

        String vertical = (this.workbook.isVerticalScrollBarVisible() ? "TRUE" : "FALSE");
        writeAttribute(out, "WorkbookView::show_vertical_scrollbar", 4, vertical, nextIndent);

        String tabs = (this.workbook.isNotebookTabsVisible() ? "TRUE" : "FALSE");
        writeAttribute(out, "WorkbookView::show_notebook_tabs", 4, tabs, nextIndent);

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

    }

    /**
     * Writes one attribute element to the stream.
     * 
     * @param out  the output stream.
     * @param name  the attribute name.
     * @param type  the attribute type.
     * @param value  the attribute value.
     * @param indent  the indentation.
     */
    private void writeAttribute(PrintStream out,
                                String name, int type, String value, String indent) {

        out.println(indent + "<gmr:Attribute>");
        out.println(indent + INDENT + "<gmr:name>" + name + "</gmr:name>");
        out.println(indent + INDENT + "<gmr:type>" + type + "</gmr:type>");
        out.println(indent + INDENT + "<gmr:value>" + value + "</gmr:value>");
        out.println(indent + "</gmr:Attribute>");

    }

    /**
     * Writes the workbook summary element.
     * 
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeSummary(PrintStream out, String indent) {

        Summary summary = this.workbook.getSummary();

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

        String nextIndent = indent + INDENT;

        if (summary.getTitle() != null) {
            writeSummaryItem(out, "title", summary.getTitle(), nextIndent);
        }

        if (summary.getKeywords() != null) {
            writeSummaryItem(out, "keywords", summary.getKeywords(), nextIndent);
        }

        if (summary.getComments() != null) {
            writeSummaryItem(out, "comments", summary.getComments(), nextIndent);
        }

        if (summary.getCategory() != null) {
            writeSummaryItem(out, "category", summary.getCategory(), nextIndent);
        }

        if (summary.getManager() != null) {
            writeSummaryItem(out, "manager", summary.getManager(), nextIndent);
        }

        if (summary.getApplication() != null) {
            writeSummaryItem(out, "application", summary.getApplication(), nextIndent);
        }

        if (summary.getAuthor() != null) {
            writeSummaryItem(out, "author", summary.getAuthor(), nextIndent);
        }

        if (summary.getCompany() != null) {
            writeSummaryItem(out, "company", summary.getCompany(), nextIndent);
        }

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

    }

    /**
     * Writes one summary item element to the stream.
     * 
     * @param out  The output stream.
     * @param name  The item name.
     * @param value  The item value.
     * @param indent  The indentation.
     */
    private void writeSummaryItem(PrintStream out,
                                  String name, String value, String indent) {

        out.println(indent + "<gmr:Item>");
        out.println(indent + INDENT + "<gmr:name>" + name + "</gmr:name>");
        out.println(indent + INDENT + "<gmr:val-string>" + value + "</gmr:val-string>");
        out.println(indent + "</gmr:Item>");

    }

    /**
     * Write the names for the workbook.  Currently not supported, so we write out an empty names
     * element.
     * 
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeWorkbookNames(PrintStream out, String indent) {

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

    }

    /**
     * Write the "geometry" for the workbook.
     * 
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeGeometry(PrintStream out, String indent) {

        out.println(indent + "<gmr:Geometry Width=\"" + this.workbook.getGeometryWidth()
                           + "\" Height=\"" + this.workbook.getGeometryHeight() + "\"/>");

    }

    /**
     * Write the "UIData" for the workbook.
     * 
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeUIData(PrintStream out, String indent) {
        out.println(indent + "<gmr:UIData SelectedTab=\"0\"/>");
    }


    /**
     * Writes a worksheet element in the Gnumeric format to a stream.
     * 
     * @param worksheet  the worksheet.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    public void writeWorksheet(Worksheet worksheet, PrintStream out, String indent) {

        boolean displayFormulae = worksheet.getDisplayFormulae();
        boolean hideZeros = worksheet.getHideZeros();
        boolean isGridVisible = worksheet.isGridVisible();
        boolean isColumnHeaderVisible = worksheet.isColumnHeaderVisible();
        boolean isRowHeaderVisible = worksheet.isRowHeaderVisible();

        out.println(indent + "<gmr:Sheet DisplayFormulas=\"" + (displayFormulae ? "true" : "false")
                           + "\" HideZero=\"" + (hideZeros ? "true" : "false")
                           + "\" HideGrid=\"" + (isGridVisible ? "false" : "true")
                           + "\" HideColHeader=\"" + (isColumnHeaderVisible ? "false" : "true")
                           + "\" HideRowHeader=\"" + (isRowHeaderVisible ? "false" : "true")
                           + "\">");

        String nextIndent = indent + INDENT;

        writeName(worksheet, out, nextIndent);
        writeMaxCol(worksheet, out, nextIndent);
        writeMaxRow(worksheet, out, nextIndent);
        writeZoom(worksheet, out, nextIndent);

        out.println(nextIndent + "<gmr:Names>");
        writeWorksheetNames(worksheet.getNamesManager(), out, nextIndent + INDENT);
        out.println(nextIndent + "</gmr:Names>");

        PrintInformation printInfo = worksheet.getPrintInformation();
        writePrintInformation(printInfo, out, nextIndent);

        Styles styles = worksheet.getStyles();
        writeStyles(styles, out, nextIndent);

        ColumnAttributesManager columnAttributes = worksheet.getColumnAttributesManager();
        writeColumns(columnAttributes, out, nextIndent);

        RowAttributesManager rowAttributes = worksheet.getRowAttributesManager();
        writeRows(rowAttributes, out, nextIndent);

        Selections selections = worksheet.getSelections();
        writeSelections(selections, out, nextIndent);

        writeObjects(worksheet, out, nextIndent);

        Cells cells = worksheet.getCells();
        writeCells(cells, out, nextIndent);

        Solver solver = worksheet.getSolver();
        writeSolver(solver, out, nextIndent);

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

    }

    /**
     * Writes the name element.
     * 
     * @param worksheet  the worksheet.
     * @param out  the output stream.
     * @param indent  the indentation.
     */
    private void writeName(Worksheet worksheet, PrintStream out, String indent) {

        out.println(indent + "<gmr:Name>" + worksheet.getName() + "</gmr:Name>");

    }

    /**
     * Writes the maximum column element.
     * 
     * @param worksheet  the worksheet.
     * @param out  the output stream.
     * @param indent  the indentation.

⌨️ 快捷键说明

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