簡體   English   中英

使用 C# 將格式從一行復制到另一行

[英]Copy format from one row to another using c#

這個問題與這里提出的問題非常相似。 但是給出的答案建議將格式與數據一起復制。 我有一個使用 SSIS 生成的 Excel 表 (.xlsx)。 現在我已經在第一行中設置了格式,我想將其復制到工作表中已填充的所有行。 我怎樣才能使用 C# 做到這一點? 我正在使用 Excel 互操作。

您可以將PasteSpecialxlPasteFormats一起xlPasteFormats

Excel.Range R1 = (Excel.Range)oSheet.Cells[11, 11];
R1.Copy(Type.Missing);

Excel.Range R2 = (Excel.Range)oSheet.Cells[15, 15];
R2.PasteSpecial(Excel.XlPasteType.xlPasteFormats,
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);

因此,您想從第一個單元格復制格式並將其應用於所有工作

有一種處理方式:

 Range sourceRange = sheet.get_Range("A1:A1");
 sourceRange.Copy();

 Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
 Range destinationRange = sheet.get_Range("A1", last);

 destinationRange.PasteSpecial(XlPasteType.xlPasteFormats);

我按照 mickro 的解釋使用它,並且效果很好!!!!!!

                Range contentAlarms =exlWsheetAlarms.get_Range("A1:G"+countList);
                contentAlarms.Copy(Type.Missing);

                Range last = exlWsheetUlt.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
                Range destinationRange = exlWsheetUlt.get_Range("B90", last);

                destinationRange.PasteSpecial(XlPasteType.xlPasteFormats);

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM