예제 #1
0
        public XlsxParts(Stream path)
        {
            p = path;
            z = new ZipFile();

            mainRels = new Relationships();
            xlRels   = new Relationships();

            #region workbook.xml_Content_Type

            workbookXml            = new XlsxWorkbookMetadata();
            workbookXmlPackagePart = XlsxPart.LinkToPackage("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "xl/workbook.xml");
            mainRels.Link(workbookXmlPackagePart);

            #endregion

            #region core.xml

            XDocument xdjfkd = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

            xdjfkd = XDocument.Parse(Settings.Default.CoreXml.OuterXml, LoadOptions.None);
            mainRels.Link(XlsxPart.LinkToPackage("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties", "docProps/core.xml"
                                                 ));

            using (StringWriterWithEncoding sb = new StringWriterWithEncoding(Encoding.UTF8))
            {
                var za = new XmlWriterSettings();
                za.Encoding = Encoding.UTF8;
                using (XmlWriter apo = XmlWriter.Create(sb, za))
                {
                    xdjfkd.Save(apo);
                    apo.Close();
                    z.AddEntry("/docProps/core.xml", sb.ToString());
                }
            }

            #endregion

            #region theme

            XDocument xdjfkdd = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

            xdjfkdd = XDocument.Parse(Settings.Default.ThemeXml.OuterXml, LoadOptions.None);
            xlRels.Link(XlsxPart.LinkToPackage("http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "theme/theme1.xml"
                                               ));

            using (StringWriterWithEncoding sbs = new StringWriterWithEncoding(Encoding.UTF8))
            {
                var zas = new XmlWriterSettings();
                zas.Encoding = Encoding.UTF8;

                using (XmlWriter apos = XmlWriter.Create(sbs, zas))
                {
                    xdjfkdd.Save(apos);
                    apos.Close();
                    z.AddEntry("/xl/theme/theme1.xml", sbs.ToString());
                }
            }


            #endregion

            #region styles

            XDocument xdjfkdsa = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

            xdjfkdsa = XDocument.Parse(Settings.Default.StylesXml.OuterXml, LoadOptions.None);
            xlRels.Link(XlsxPart.LinkToPackage("http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "styles.xml"
                                               ));

            using (StringWriterWithEncoding sasb = new StringWriterWithEncoding(Encoding.UTF8))
            {
                var zaaa = new XmlWriterSettings();
                zaaa.Encoding = Encoding.UTF8;

                using (XmlWriter apso = XmlWriter.Create(sasb, zaaa))
                {
                    xdjfkdsa.Save(apso);
                    apso.Close();
                    z.AddEntry("/xl/styles.xml", sasb.ToString());
                }
            }

            #endregion

            #region sharedstrings

            string jf    = Path.GetTempFileName();
            var    strm1 = new FileStream(jf, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            z.AddEntry("/xl/sharedStrings.xml", strm1);
            sharedStrings = new XlsxSharedStringsXml(strm1, jf);

            xlRels.Link(XlsxPart.LinkToPackage("http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings", "sharedStrings.xml"));

            #endregion
        }
예제 #2
0
        internal XlsxWorksheet(string sheetName, string relationshipId, DataRowCollection inputData
                               , XlsxSharedStringsXml sharedStringReference, string packageFileName)
        {
            PackageFileName = packageFileName;

            string jf = Path.GetTempFileName();

            FileAssociatedWithOutputStream = jf;

            OutputStream = new FileStream(jf, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

            SharedStringReference = sharedStringReference;

            wx = XmlWriter.Create(OutputStream);

            RelationshipId = relationshipId;
            Sheetname      = sheetName;

            wx.WriteStartDocument(true);

            wx.WriteStartElement("worksheet", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");

            wx.WriteAttributeString("xmlns", "r", null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            wx.WriteAttributeString("xmlns", "mc", null, "http://schemas.openxmlformats.org/markup-compatibility/2006");
            wx.WriteAttributeString("mc", "Ignorable", null, "x14ac");
            wx.WriteAttributeString("xmlns", "x14ac", null, "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("dimension");
            wx.WriteAttributeString("ref", "A1");
            wx.WriteEndElement();
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("sheetViews");
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("sheetView");
            wx.WriteAttributeString("workbookViewId", "0");
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("pane");
            wx.WriteAttributeString("ySplit", "1");
            wx.WriteAttributeString("topLeftCell", "A2");
            wx.WriteAttributeString("activePane", "bottomLeft");
            wx.WriteAttributeString("state", "frozen");
            wx.WriteEndElement();

            wx.WriteStartElement("selection");
            wx.WriteAttributeString("pane", "bottomLeft");
            wx.WriteAttributeString("activeCell", "A2");
            wx.WriteAttributeString("sqref", "A2");
            wx.WriteEndElement();
            //</sheetView>
            wx.WriteEndElement();
            wx.WriteWhitespace(Environment.NewLine);
            //</sheetViews>
            wx.WriteEndElement();
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("sheetFormatPr");
            wx.WriteAttributeString("defaultRowHeight", "15");
            wx.WriteAttributeString("x14ac", "dyDescent", null, "0.25");
            wx.WriteEndElement();
            wx.WriteWhitespace(Environment.NewLine);

            wx.WriteStartElement("sheetData");
            wx.WriteWhitespace(Environment.NewLine);

            // write row hdr

            wx.WriteStartElement("row");
            wx.WriteWhitespace(Environment.NewLine);
            foreach (DataRow rows in inputData)
            {
                // FIXME don't hardcode 100
                // FIXME the call to overpunch happens twice, could just happen once with appropriate reutnr value
                Writeval(rows["ColumnName"].ToString(), StaticFunctions.ResolveDataType(rows["ColumnName"].ToString()));
            }
            // write row close

            wx.WriteEndElement();
            wx.WriteWhitespace(Environment.NewLine);
        }