當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。