Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > Excel xml 2003


Автор: Telepyz 14.8.2013, 08:25
Доброго времени суток.

Хочу написать класс для создания документа Excel xml 2003

Базовая структура файла:
Код

<?xml version="1.0"?>
<?mso-application progid='Excel.Sheet'?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  </DocumentProperties>
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
    <AllowPNG />
  </OfficeDocumentSettings>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>5000</WindowHeight>
    <WindowWidth>10000</WindowWidth>
    <WindowTopX>0</WindowTopX>
    <WindowTopY>0</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
      <Borders />
      <Font ss:FontName="Times New Roman" x:CharSet="204" x:Family="Roman" ss:Size="12" ss:Color="#000000" />
      <Interior />
      <NumberFormat />
      <Protection />
    </Style>
  </Styles>
  <Worksheet ss:Name="Test">
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="15.75">
    </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <Selected />
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </Worksheet>
</Workbook>



Код класса
Код

    public class Excel
    {
        #region BaseXml
        string xml = "<?xml version='1.0'?>" +
                     "<?mso-application progid='Excel.Sheet'?>" +
                     "<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>" +
                     "<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>" +
                     "<Title></Title>"+
                     "</DocumentProperties>" +
                     "<OfficeDocumentSettings xmlns='urn:schemas-microsoft-com:office:office'>" +
                     "<AllowPNG/>" +
                     "</OfficeDocumentSettings>" +
                     "<ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>" +
                     "<WindowHeight>5000</WindowHeight>" +
                     "<WindowWidth>10000</WindowWidth>" +
                     "<WindowTopX>0</WindowTopX>" +
                     "<WindowTopY>0</WindowTopY>" +
                     "<ProtectStructure>False</ProtectStructure>" +
                     "<ProtectWindows>False</ProtectWindows>" +
                     "</ExcelWorkbook>" +
                     "<Styles>" +
                     "<Style ss:ID='Default' ss:Name='Normal'>" +
                     "<Alignment ss:Vertical='Bottom'/>" +
                     "<Borders/>" +
                     "<Font ss:FontName='Times New Roman' x:CharSet='204' x:Family='Roman' ss:Size='12' ss:Color='#000000'/>" +
                     "<Interior/>" +
                     "<NumberFormat/>" +
                     "<Protection/>" +
                     "</Style>" +
                     "</Styles>" +
                     "<Worksheet ss:Name='Test'>" +
                     "<Table ss:ExpandedColumnCount='1' ss:ExpandedRowCount='1' x:FullColumns='1' x:FullRows='1' ss:DefaultColumnWidth='54' ss:DefaultRowHeight='15.75'>" +
                     "</Table>" +
                     "<WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>" +
                     "<Selected/>" +
                     "<ProtectObjects>False</ProtectObjects>" +
                     "<ProtectScenarios>False</ProtectScenarios>" +
                     "</WorksheetOptions>" +
                     "</Worksheet>" +
                     "</Workbook>";
        #endregion

        #region Propertys
        /// <summary>
        /// Название
        /// </summary>
        public string Title
        {
            get
            {
                XmlNode node = Data.DocumentElement.GetElementsByTagName("Title").Item(0);
                return node.InnerText;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    XmlNode node = Data.DocumentElement.GetElementsByTagName("Title").Item(0);
                    if (node != null)
                    {
                        node.InnerText = value;
                    }
                    else
                    {
                        node = Data.DocumentElement.GetElementsByTagName("DocumentProperties").Item(0);

                        XmlNode title = Data.CreateElement("Title");
                        title.InnerText = value;
                        node.AppendChild(title);
                    }
                }
            }
        }
        /// <summary>
        /// Тема
        /// </summary>
        public string Subject
        {
            get
            {
                XmlNode node = Data.DocumentElement.GetElementsByTagName("Subject").Item(0);
                return node.InnerText;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    XmlNode node = Data.DocumentElement.GetElementsByTagName("Subject").Item(0);
                    if (node != null)
                    {
                        node.InnerText = value;
                    }
                    else
                    {
                        node = Data.DocumentElement.GetElementsByTagName("DocumentProperties").Item(0);

                        XmlNode subject = Data.CreateElement("Subject");
                        subject.InnerText = value;
                        node.AppendChild(subject);
                    }
                }
            }
        }

        #endregion

        private XmlDocument Data = new XmlDocument();
        public Excel()
        {
            Data.LoadXml(xml);
        }
        /// <summary>
        /// Сохранение сгенерированного документа в файл
        /// </summary>
        /// <param name="FileName">Имя файла</param>
        public void Save(string FileName = "Excel.xml")
        {
            Data.Save(FileName);
        }
    }


Свойства Title и Subject пишутся в секцию DocumentProperties, проблема в том что после генерации документа секция получается такого вида
Код

  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Title xmlns="">Title</Title>
    <Subject xmlns="">Subject</Subject>
  </DocumentProperties>

и Excel не видит этих свойств а должна быть
Код

  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Title>Title</Title>
    <Subject>Subject</Subject>
  </DocumentProperties>


Если изначально в шаблоне прописать поля Title и Subject то все нормально

Помогите разобраться в чем дело

Проект прилагается на Studio 2010

Автор: diadiavova 14.8.2013, 18:26
Вместо
Код

                      XmlNode title = Data.CreateElement("Title");
пиши
Код

                      XmlNode title = Data.CreateElement("Title", "urn:schemas-microsoft-com:office:office");
Со вторым элементом то же самое.

Автор: Telepyz 15.8.2013, 10:53
Спасибо огромное!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)