当前位置: 首页>>代码示例>>C#>>正文


C# ExcelWorksheet.CreateEmptyCells方法代码示例

本文整理汇总了C#中OfficeOpenXml.ExcelWorksheet.CreateEmptyCells方法的典型用法代码示例。如果您正苦于以下问题:C# ExcelWorksheet.CreateEmptyCells方法的具体用法?C# ExcelWorksheet.CreateEmptyCells怎么用?C# ExcelWorksheet.CreateEmptyCells使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OfficeOpenXml.ExcelWorksheet的用法示例。


在下文中一共展示了ExcelWorksheet.CreateEmptyCells方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Add

        /// <summary>
        /// Adds a blank worksheet with the desired name
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="rowCount">Number of rows to to create initially (for performance)</param>
        /// <param name="colCount">Number of columns to to create</param>
        /// <returns></returns>
        public ExcelWorksheet Add(string Name, int rowCount, int colCount)
        {
            // first find maximum existing sheetID
            // also check the name is unique - if not throw an error
            int sheetID = 0;
            foreach (XmlNode sheet in _worksheetsNode.ChildNodes)
            {
                XmlAttribute attr = (XmlAttribute)sheet.Attributes.GetNamedItem("sheetId");
                if (attr != null)
                {
                    int curID = int.Parse(attr.Value);
                    if (curID > sheetID)
                        sheetID = curID;
                }
                attr = (XmlAttribute)sheet.Attributes.GetNamedItem("name");
                if (attr != null)
                {
                    if (attr.Value == Name)
                        throw new Exception("Add worksheet Error: attempting to create worksheet with duplicate name");
                }
            }
            // we now have the max existing values, so add one
            sheetID++;

            // add the new worksheet to the package
            Uri uriWorksheet = new Uri("/xl/worksheets/sheet" + sheetID.ToString() + ".xml", UriKind.Relative);
            PackagePart worksheetPart = _xlPackage.Package.CreatePart(uriWorksheet, @"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", CompressionOption.Normal);

            // create the new, empty worksheet and save it to the package
            StreamWriter streamWorksheet = new StreamWriter(worksheetPart.GetStream(FileMode.Create, FileAccess.Write));
            XmlDocument worksheetXml = CreateNewWorksheet();
            worksheetXml.Save(streamWorksheet);
            streamWorksheet.Close();
            _xlPackage.Package.Flush();

            // create the relationship between the workbook and the new worksheet
            PackageRelationship rel = _xlPackage.Workbook.Part.CreateRelationship(uriWorksheet, TargetMode.Internal, ExcelPackage.schemaRelationships + "/worksheet");
            _xlPackage.Package.Flush();

            // now create the new worksheet tag and set name/SheetId attributes in the workbook.xml
            XmlElement worksheetNode = _xlPackage.Workbook.WorkbookXml.CreateElement("sheet", ExcelPackage.schemaMain);
            // create the new sheet node
            worksheetNode.SetAttribute("name", Name);
            worksheetNode.SetAttribute("sheetId", sheetID.ToString());
            // set the r:id attribute
            worksheetNode.SetAttribute("id", ExcelPackage.schemaRelationships, rel.Id);
            // insert the sheet tag with all attributes set as above
            _worksheetsNode.AppendChild(worksheetNode);

            // create a reference to the new worksheet in our collection
            ExcelWorksheet worksheet = new ExcelWorksheet(_xlPackage, rel.Id, Name, uriWorksheet, sheetID, false);
            _worksheets.Add(worksheet);
            worksheet.CreateEmptyCells(rowCount, colCount);

            return worksheet;
        }
开发者ID:jacobpovar,项目名称:ooxmlcrypto,代码行数:63,代码来源:ExcelWorksheets.cs


注:本文中的OfficeOpenXml.ExcelWorksheet.CreateEmptyCells方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。