繁体   English   中英

如何将特定单元格(带有值、颜色、字体)从一个 excel 文件复制到另一个 excel 文件 C#?

[英]How can I copy specific cells(with Value,Color,Font) from one excel file to another excel file C#?

在这里,我使用此代码将单元格从一个 Excel 文件复制到另一个。 但这需要很多时间。

     for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j < 10;j++) {  
                xlWorkSheet.Cells[i, j].style.Font.Color = ws.Cells[i, i].style.Font.Color;
                xlWorkSheet.Cells[i, j].Interior.Color = ws.Cells[i, i].Interior.Color;
                xlWorkSheet.Cells[i, j] = ws.Cells[i, i];
            }
        }

有没有其他方法可用?

遵循文档: Range.Copy 方法

如果您只想复制文本或格式(等),则将PasteSpecialCopy方法一起使用: Range.PasteSpecial

用法:

//define cells (range) to copy
Excel.Range from = srcworkSheet.Range["A1:D100"];
//define destination range
Excel.Range to = destworkSheet.Range["A1"];
from.Copy(to);

//this should also works:
//xlWorkSheet.Cells[i, j].Copy(ws.Cells[i, j]);

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM