2016-08-23 94 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等價物應該很容易找到。

相關問題