如何一行的格式复制到另一行Excel中使用C# [英] How to copy format of one row to another row in Excel with c#

查看:2454
本文介绍了如何一行的格式复制到另一行Excel中使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#插入数据到Excel。每当我使用C#添加一个新行到Excel我想相同的格式,上面一行即,颜色,字体和背景颜色都以编程方式。

I am inserting data to Excel using C#. Whenever I add a new row to Excel using C# I want the same format as above row i.e, color, font and background color everything by programmatically.

这是一个OLEDB插入。

It's an OLEDB insert.

发布插入,欲第一行的格式适用于第二行。与格式画家从UI这是一个简单的工作,我无法找到一个方法来做到用C#一样的。

Post insert, I want to apply the format of first row to the second row. With format painter from UI it's a straightforward job, I can't find a way to do the same with C#.

推荐答案

1 )首先,你需要获取范围要复制的如RngToCopy
2)然后设置要插入的范围。
3)使用下面所提到的代码片断。

1) First you Need to get the Range you want to copy for e.g. RngToCopy 2) Then Set the Range where you want to insert. 3) use the below mentioned code snippet.

  Range RngToCopy = ws.get_Range(StartCell, EndCell).EntireRow;
Range RngToInsert = ws.get_Range(StartCell, Type.Missing).EntireRow;
oRngToInsert.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, oRngToCopy.Copy(Type.Missing));

//ws is the worksheet object, set StartCell and EndCell as per your requirement

这篇关于如何一行的格式复制到另一行Excel中使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆