Exemple #1
0
        /**
         * Writes os the workbook data as XML, with formatting information
         */
        private void writeFormattedXML(TextWriter os)
        {
            try
            {
                //OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
                //BufferedWriter bw = new BufferedWriter(osw);

                os.Write("<?xml version=\"1.0\" ?>");
                os.WriteLine();
                os.Write("<!DOCTYPE workbook SYSTEM \"formatworkbook.dtd\">");
                os.WriteLine();
                os.WriteLine();
                os.Write("<workbook>");
                os.WriteLine();
                for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++)
                {
                    Sheet s = workbook.getSheet(sheet);

                    os.Write("  <sheet>");
                    os.WriteLine();
                    os.Write("    <name><![CDATA[" + s.getName() + "]]></name>");
                    os.WriteLine();

                    Cell[]     row    = null;
                    CellFormat format = null;
                    Font       font   = null;

                    for (int i = 0; i < s.getRows(); i++)
                    {
                        os.Write("    <row number=\"" + i + "\">");
                        os.WriteLine();
                        row = s.getRow(i);

                        for (int j = 0; j < row.Length; j++)
                        {
                            // Remember that empty cells can contain format information
                            if ((row[j].getType() != CellType.EMPTY) ||
                                (row[j].getCellFormat() != null))
                            {
                                format = row[j].getCellFormat();
                                os.Write("      <col number=\"" + j + "\">");
                                os.WriteLine();
                                os.Write("        <data>");
                                os.Write("<![CDATA[" + row[j].getContents() + "]]>");
                                os.Write("</data>");
                                os.WriteLine();

                                if (row[j].getCellFormat() != null)
                                {
                                    os.Write("        <format wrap=\"" + format.getWrap() + "\"");
                                    os.WriteLine();
                                    os.Write("                align=\"" +
                                             format.getAlignment().getDescription() + "\"");
                                    os.WriteLine();
                                    os.Write("                valign=\"" +
                                             format.getVerticalAlignment().getDescription() + "\"");
                                    os.WriteLine();
                                    os.Write("                orientation=\"" +
                                             format.getOrientation().getDescription() + "\"");
                                    os.Write(">");
                                    os.WriteLine();

                                    // The font information
                                    font = format.getFont();
                                    os.Write("          <font name=\"" + font.getName() + "\"");
                                    os.WriteLine();
                                    os.Write("                point_size=\"" +
                                             font.getPointSize() + "\"");
                                    os.WriteLine();
                                    os.Write("                bold_weight=\"" +
                                             font.getBoldWeight() + "\"");
                                    os.WriteLine();
                                    os.Write("                italic=\"" + font.isItalic() + "\"");
                                    os.WriteLine();
                                    os.Write("                underline=\"" +
                                             font.getUnderlineStyle().getDescription() + "\"");
                                    os.WriteLine();
                                    os.Write("                colour=\"" +
                                             font.getColour().getDescription() + "\"");
                                    os.WriteLine();
                                    os.Write("                script=\"" +
                                             font.getScriptStyle().getDescription() + "\"");
                                    os.Write(" />");
                                    os.WriteLine();


                                    // The cell background information
                                    if (format.getBackgroundColour() != Colour.DEFAULT_BACKGROUND ||
                                        format.getPattern() != Pattern.NONE)
                                    {
                                        os.Write("          <background colour=\"" +
                                                 format.getBackgroundColour().getDescription() + "\"");
                                        os.WriteLine();
                                        os.Write("                      pattern=\"" +
                                                 format.getPattern().getDescription() + "\"");
                                        os.Write(" />");
                                        os.WriteLine();
                                    }


                                    // The cell border, if it has one
                                    if (format.getBorder(Border.TOP) != BorderLineStyle.NONE ||
                                        format.getBorder(Border.BOTTOM) != BorderLineStyle.NONE ||
                                        format.getBorder(Border.LEFT) != BorderLineStyle.NONE ||
                                        format.getBorder(Border.RIGHT) != BorderLineStyle.NONE)
                                    {
                                        os.Write("          <border top=\"" +
                                                 format.getBorder(Border.TOP).getDescription() + "\"");
                                        os.WriteLine();
                                        os.Write("                  bottom=\"" +
                                                 format.getBorder(Border.BOTTOM).getDescription() +
                                                 "\"");
                                        os.WriteLine();
                                        os.Write("                  left=\"" +
                                                 format.getBorder(Border.LEFT).getDescription() + "\"");
                                        os.WriteLine();
                                        os.Write("                  right=\"" +
                                                 format.getBorder(Border.RIGHT).getDescription() + "\"");
                                        os.Write(" />");
                                        os.WriteLine();
                                    }

                                    // The cell number/date format
                                    if (format.getFormat().getFormatString().Length != 0)
                                    {
                                        os.Write("          <format_string string=\"");
                                        os.Write(format.getFormat().getFormatString());
                                        os.Write("\" />");
                                        os.WriteLine();
                                    }

                                    os.Write("        </format>");
                                    os.WriteLine();
                                }

                                os.Write("      </col>");
                                os.WriteLine();
                            }
                        }
                        os.Write("    </row>");
                        os.WriteLine();
                    }
                    os.Write("  </sheet>");
                    os.WriteLine();
                }

                os.Write("</workbook>");
                os.WriteLine();

                os.Flush();
                //bw.close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }