2016-08-23 97 views
0

当将多个Excel工作表合并到一个Excel文档中时,我遇到了问题。所以基本上它是一个工作簿与多个工作表。但是,这可行使用C#和OpenXML合并工作表时保留相同的Excel样式

问题在于它不保留格式和样式。第一张纸的格式正确,但它不应该在整张纸上复制相同的样式。

我需要帮助合并工作表并保持相同的样式和格式。

遍历片C#

// For each worksheet in the child workbook... 
        foreach (Sheet childSheet in childWorkbookSheets) 
        { 
         // Get a worksheet part for the child worksheet using 
         // it's relationship Id. 
         childWorksheetPart = (WorksheetPart)childWorkbook.WorkbookPart.GetPartById(childSheet.Id); 

         // Add a worksheet part to the merged workbook based on 
         // the child worksheet. 
         mergedWorksheetPart = mergedWorkbookPart.AddPart<WorksheetPart>(childWorksheetPart); 

         //There should be only one worksheet that is set 
         //as the main view. 
         CleanView(mergedWorksheetPart); 

         // Create a Sheet element for the new sheet in the 
         // merged workbook. 
         newMergedSheet = new Sheet(); 

         // Set the Name, Id, and SheetId attributes of the 
         // new Sheet element. 
         newMergedSheet.Name = GenerateWorksheetName(mergedWorkbookSheets, childSheet.Name.Value); 

         newMergedSheet.Id = mergedWorkbookPart.GetIdOfPart(mergedWorksheetPart); 

         newMergedSheet.SheetId = (uint)mergedWorkbookSheets.ChildElements.Count + 1; 

         // Add the new Sheet element to the Sheets element in the 
         // merged workbook. 
         mergedWorkbookSheets.Append(newMergedSheet); 

         // Get the SheetData element of the new worksheet part 
         // in the merged workbook. 
         mergedSheetData = mergedWorksheetPart.Worksheet.GetFirstChild<SheetData>(); 

         if (styleCounter == 0) 
         { 
          mergedWorkbook.WorkbookPart.AddPart<WorkbookStylesPart>(childSharedStylePart); 
         } 

         styleCounter++; 

         // For each row of data... 
         foreach (Row row in mergedSheetData.Elements<Row>()) 
         { 
          // For each cell in the row... 
          foreach (Cell cell in row.Elements<Cell>()) 
          { 
           // If the cell is using a shared string, merge 
           // the string from the child workbook into the merged 
           // workbook. 
           CellFormat cellFormat = cell.StyleIndex != null ? GetCellFormat(mergedWorkbookPart, cell.StyleIndex).CloneNode(true) as CellFormat : new CellFormat(); 
           GetCellFormat(mergedWorkbookPart, cell.StyleIndex); 
           if (cell.DataType != null && 
            cell.DataType.Value == CellValues.SharedString) 
           { 
            ProcessCellSharedString(mergedWorksheetPart, cell, mergedSharedStringTablePart, childSharedStringTablePart); 
           } 
           cell.StyleIndex = InsertCellFormat(mergedWorkbookPart, cellFormat); 

           mergedSheetData.AppendChild(new Cell()); 
           mergedCellformat = GetCellFormat(mergedWorkbookPart, cell.StyleIndex); 

           //cellFormat.ReplaceChild(mergedCellformat,mergedCellformat); 

           //attempt to add styling to the other worksheets 
           mergedCellformat.FillId.Value = (cellFormat.FillId.Value); 
           mergedCellformat.BorderId.Value = (cellFormat.BorderId.Value); 
           mergedCellformat.FontId.Value = (cellFormat.FontId.Value); 
           //mergedCellformat.FormatId = (cellFormat.FormatId.Value); 

           //cellFormat.AppendChild(mergedCellformat); 
           //cellFormat.Append(mergedCellformat); 
          } 
         } 

enter image description here

第一片是完美的,它保留格式和它的正确的。 其余的表格都是格式化的。这是不一样的。

enter image description here

private static CellFormat GetCellFormat(WorkbookPart workbookPart, uint styleIndex) 
{ 
    return workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First().Elements<CellFormat>().ElementAt((int)styleIndex); 
} 

private static uint InsertCellFormat(WorkbookPart workbookPart, CellFormat cellFormat) 
{ 
    CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First(); 
    cellFormats.Append(cellFormat); 
    return (uint)cellFormats.Count++; 
} 
+0

任何时间格式化都是拼图的关键部分,图片欣赏。你可以添加一些当前(越野车)行为和预期行为的图像吗? – Mikegrann

+0

@Mikegrann嗨,第一张图片显示了工作表的第一张。它保留相同的格式,很好。但其他表格不是,格式不正确并且未被保留。 –

+1

您可以显示您的'GetCellFormat'和'InsertCellFormat'方法吗? – petelids

回答

0

我不知道,如果你能做到这一点,而不是,但我会尝试为参考,从选择范围和使用OPENXML等价的“PasteSpecial的”的角度攻击这个以下内容:

How to apply cell style to each cell in new row from previous row

因此,而不是试图单独获得格式,创建单元格,填充数据,然后复制格式...

定义源RangeSet。 https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.rangeset_members.aspx

CloneNode(true)。 然后将其插入合并的工作表中。 (因为这应该复制有关范围的所有内容,包括样式)。

(声明:我没有时间来测试这一点,但希望建议帮助下替换路径)

0

我会考虑创建一个Worksheet Template

创建使用该模板的新的片材包括:(VBA)这样的代码:

'Insert sheet template 
With ThisWorkbook 
    Set sh = Sheets.Add(Type:=Application.TemplatesPath & shName, _ 
         after:=.Sheets(.Sheets.Count)) 
End With 

然后只需用原始数据填充它(无论是通过(VBA).PasteSpecial Paste:=xlPasteValues或直接赋值到细胞),并将其命名。

以上代码的OpenXML等价物应该很容易找到。

相关问题