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


C# Worksheet.AppendChild方法代码示例

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


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

示例1: ImportWorksheet

 /// <summary>
 /// Import worksheet part from one document to another
 /// </summary>
 /// <param name="sourceDoc">Source excel document</param>
 /// <param name="srcSheet">Source worksheet</param>
 /// <param name="targetDoc">Target excel document</param>
 /// <param name="tgtSheet">Position of imported worksheet</param>
 /// <returns></returns>
 protected static void ImportWorksheet(SpreadsheetDocument sourceDoc, Worksheet srcSheet, SpreadsheetDocument targetDoc, Worksheet tgtSheet)
 {
     // Import sheet format properties
     tgtSheet.SheetFormatProperties = srcSheet.SheetFormatProperties.Clone() as SheetFormatProperties;
     // Import dimension
     tgtSheet.SheetDimension = srcSheet.SheetDimension.Clone() as SheetDimension;
     // Imported style buffer
     var dicStyles = new Dictionary<uint, uint>();
     // Import columns
     var srcCols = srcSheet.GetFirstChild<Columns>();
     if (srcCols != null)
     {
         var tgtCols = tgtSheet.GetFirstChild<Columns>() ?? tgtSheet.AppendChild(new Columns());
         foreach (var srcCol in srcCols.Descendants<Column>())
         {
             var tgtCol = (Column)srcCol.Clone();
             tgtCols.AppendChild(tgtCol);
             // Handle column style
             if (srcCol.Style != null)
             {
                 uint iNewIdx = 0;
                 if (dicStyles.ContainsKey(srcCol.Style.Value))
                     iNewIdx = dicStyles[srcCol.Style.Value];
                 else
                 {
                     iNewIdx = ImportStyle(sourceDoc, srcCol.Style.Value, targetDoc);
                     dicStyles[srcCol.Style.Value] = iNewIdx;
                 }
                 tgtCol.Style = new UInt32Value(iNewIdx);
             }
         }
     }
     // Import sheet data
     var srcSheetData = srcSheet.GetFirstChild<SheetData>();
     if (srcSheetData != null)
     {
         var tgtSheetData = tgtSheet.GetFirstChild<SheetData>();
         if (tgtSheetData == null)
         {
             tgtSheetData = tgtSheet.AppendChild(new SheetData()) as SheetData;
         }
         // Import rows
         foreach (var row in srcSheetData.Elements<Row>())
         {
             var newRow = row.CloneNode(false) as Row;
             tgtSheetData.AppendChild(newRow);
             // Import row style
             if (newRow.StyleIndex != null)
             {
                 var id = newRow.StyleIndex.Value;
                 var newId = dicStyles.ContainsKey(id) ? dicStyles[id] : ImportStyle(sourceDoc, id, targetDoc);
                 dicStyles[id] = newId;
                 newRow.StyleIndex = new UInt32Value(newId);
             }
             // Import cells
             foreach (var cell in row.Elements<Cell>())
             {
                 var newCell = cell.Clone() as Cell;
                 if (newCell == null) throw new InvalidCastException("Cloned cell is not Cell!");
                 // Handle shared string
                 if (cell.CellValue != null && cell.DataType != null &&
                     cell.DataType.Value == CellValues.SharedString)
                 {
                     uint id = uint.Parse(cell.CellValue.InnerText);
                     uint newId = ImportSharedString(sourceDoc, id, targetDoc);
                     newCell.CellValue = new CellValue(newId.ToString(CultureInfo.InvariantCulture));
                 }
                 // Handle style
                 if (cell.StyleIndex != null)
                 {
                     uint id = uint.Parse(cell.StyleIndex.InnerText);
                     uint newId = dicStyles.ContainsKey(id) ? dicStyles[id] : ImportStyle(sourceDoc, id, targetDoc);
                     dicStyles[id] = newId;
                     newCell.StyleIndex = new UInt32Value(newId);
                 }
                 // Handle cell metadata
                 if (cell.CellMetaIndex != null)
                 {}
                 // Handle value metadata
                 if (cell.ValueMetaIndex != null)
                 {}
                 newRow.AppendChild(newCell);
             }
         }
     }
     // Import merge cells
     var srcMergeCells = srcSheet.GetFirstChild<MergeCells>();
     if (srcMergeCells != null)
     {
         tgtSheet.AppendChild(srcMergeCells.Clone() as MergeCells);
     }
 }
开发者ID:xuehong-geng,项目名称:Giga,代码行数:100,代码来源:ExcelUtiles.cs


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