Example #1
0
        public void importXML(string filename)
        {
            try
            {
                int col = 0;
                int row = 0;
                extraCell.view.ExtraCellTable         ect      = ((extraCell.view.MDIUI)System.Windows.Forms.Application.OpenForms[0]).activeDocument.extraCellTable;
                System.Windows.Forms.DataGridViewCell viewCell = ect.Rows[0].Cells[0];

                using (XmlReader reader = XmlReader.Create(filename))
                {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            switch (reader.Name)
                            {
                            case "col":
                                if (reader["width"] != null)
                                {
                                    ect.Columns[Convert.ToInt32(reader["id"])].Width = Convert.ToInt32(reader["width"]);
                                }
                                break;

                            case "row":
                                if (reader["height"] != null)
                                {
                                    ect.Rows[Convert.ToInt32(reader["id"])].Height = Convert.ToInt32(reader["height"]);
                                }
                                break;

                            case "cell":
                                if (reader["row"] == null || reader["col"] == null)
                                {
                                    throw new Exception("Błąd spójności struktury dokumentu");
                                }

                                col = Convert.ToInt32(reader["col"]);
                                row = Convert.ToInt32(reader["row"]);

                                setCell(col, row, new Cell(reader["formula"], reader["result"]));
                                viewCell = ect.Rows[row].Cells[col];

                                /*break;
                                 * case "style":*/
                                if (reader["format"] != null)
                                {
                                    viewCell.Style.Format = reader["format"];
                                }
                                if (reader["bgcolor"] != null && !reader["bgcolor"].Equals("0"))
                                {
                                    viewCell.Style.BackColor = Color.FromArgb(Convert.ToInt32(reader["bgcolor"]));
                                }
                                if (reader["fgcolor"] != null)
                                {
                                    viewCell.Style.ForeColor = Color.FromArgb(Convert.ToInt32(reader["fgcolor"]));
                                }
                                if (reader["align"] != null)
                                {
                                    viewCell.Style.Alignment = (DataGridViewContentAlignment)Enum.Parse(typeof(DataGridViewContentAlignment), reader["align"]);
                                }
                                break;

                            case "font":
                                FontStyle fs = new FontStyle();

                                if (Convert.ToBoolean(reader["bold"]))
                                {
                                    fs = fs | FontStyle.Bold;
                                }
                                if (Convert.ToBoolean(reader["italic"]))
                                {
                                    fs = fs | FontStyle.Italic;
                                }
                                if (Convert.ToBoolean(reader["regular"]))
                                {
                                    fs = fs | FontStyle.Regular;
                                }
                                if (Convert.ToBoolean(reader["strikeout"]))
                                {
                                    fs = fs | FontStyle.Strikeout;
                                }
                                if (Convert.ToBoolean(reader["underline"]))
                                {
                                    fs = fs | FontStyle.Underline;
                                }

                                if (reader["family"] != null && reader["size"] != null && reader["unit"] != null && reader["gdicharset"] != null)
                                {
                                    viewCell.Style.Font = new Font(
                                        reader["family"],
                                        (float)Convert.ToDouble(reader["size"]),
                                        fs,
                                        (GraphicsUnit)Enum.Parse(typeof(GraphicsUnit), reader["unit"]),
                                        Convert.ToByte(reader["gdicharset"])
                                        );
                                }
                                break;
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Nie znaleziono pliku " + filename);
            }
            catch (FileLoadException)
            {
                throw new FileLoadException("Nie udało się załadować pliku " + filename);
            }
            catch (InvalidOperationException)
            {
                throw new InvalidOperationException("To nie jest prawidłowy dokument programu eXtraCell!");
            }
        }
Example #2
0
        public bool exportXML(string filename)
        {
            try
            {
                /*WriteXml(filename, XmlWriteMode.WriteSchema);*/
                Cell cell;
                System.Windows.Forms.DataGridViewCell viewCell;
                extraCell.view.ExtraCellTable         ect = ((extraCell.view.MDIUI)System.Windows.Forms.Application.OpenForms[0]).activeDocument.extraCellTable;

                using (XmlWriter writer = XmlWriter.Create(filename))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("eXtraCellDocument");

                    for (int row = 0; row < Rows.Count; row++)
                    {
                        if (ect.Rows[row].Height != 22)
                        {
                            writer.WriteStartElement("row");
                            writer.WriteAttributeString("id", row.ToString());
                            writer.WriteAttributeString("height", ect.Rows[row].Height.ToString());
                            writer.WriteEndElement();
                        }
                    }

                    for (int col = 0; col < Columns.Count; col++)
                    {
                        if (ect.Columns[col].Width != 100)
                        {
                            writer.WriteStartElement("col");
                            writer.WriteAttributeString("id", col.ToString());
                            writer.WriteAttributeString("width", ect.Columns[col].Width.ToString());
                            writer.WriteEndElement();
                        }
                    }

                    for (int row = 0; row < Rows.Count; row++)
                    {
                        for (int col = 0; col < Columns.Count; col++)
                        {
                            cell = getCell(col, row, false);
                            if (cell != null && cell.result != null && cell.result.Trim().Length != 0)
                            {
                                viewCell = ect.Rows[row].Cells[col];

                                writer.WriteStartElement("cell");
                                writer.WriteAttributeString("col", col.ToString());
                                writer.WriteAttributeString("row", row.ToString());
                                writer.WriteAttributeString("formula", cell.formula);
                                writer.WriteAttributeString("result", cell.result);

//                                    writer.WriteStartElement("style");
                                writer.WriteAttributeString("bgcolor", viewCell.Style.BackColor.ToArgb().ToString());
                                writer.WriteAttributeString("fgcolor", viewCell.Style.ForeColor.ToArgb().ToString());
                                writer.WriteAttributeString("format", viewCell.Style.Format);
                                writer.WriteAttributeString("align", viewCell.Style.Alignment.ToString());

                                if (viewCell.Style.Font != null)
                                {
                                    writer.WriteStartElement("font");
                                    writer.WriteAttributeString("family", viewCell.Style.Font.FontFamily.Name);
                                    writer.WriteAttributeString("bold", viewCell.Style.Font.Bold.ToString());
                                    writer.WriteAttributeString("italic", viewCell.Style.Font.Italic.ToString());
                                    writer.WriteAttributeString("underline", viewCell.Style.Font.Underline.ToString());
                                    writer.WriteAttributeString("strikeout", viewCell.Style.Font.Strikeout.ToString());
                                    writer.WriteAttributeString("size", viewCell.Style.Font.Size.ToString());
                                    writer.WriteAttributeString("gdicharset", viewCell.Style.Font.GdiCharSet.ToString());
                                    writer.WriteAttributeString("unit", viewCell.Style.Font.Unit.ToString());
                                    writer.WriteEndElement();
                                }

                                //                                  writer.WriteEndElement();

                                writer.WriteEndElement();
                            }
                        }
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }

                isModified = false;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("Nie udało się zapisać pliku.", "Błąd");
                return(false);
            }
            return(true);
        }