惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
I
InfoQ
小众软件
小众软件
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
V
V2EX
G
Google Developers Blog
罗磊的独立博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
IT之家
IT之家
J
Java Code Geeks
L
LangChain Blog
D
DataBreaches.Net
F
Fortinet All Blogs
B
Blog
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
P
Privacy & Cybersecurity Law Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
Kaspersky official blog
博客园 - 【当耐特】
T
Tenable Blog
AWS News Blog
AWS News Blog
V
Visual Studio Blog
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
H
Heimdal Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Secure Thoughts
Security Archives - TechRepublic
Security Archives - TechRepublic
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
Latest news
Latest news
TaoSecurity Blog
TaoSecurity Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threatpost
SecWiki News
SecWiki News
S
Security Affairs
H
Help Net Security

博客园 - micenter

Register ASP.NET 4.0 An error has occurred: 0x8007b799 You must have administrative rights on this machine in order to ru 记录一些常用的SQLServer时间日期函数详解 IT民工的2013生活创想 SQL 日志太大,脚本清除 跟随博客园进入2012 .NET 中 操作excel 系列--导入与导出. 设置VSS2005使支持通过Internet访问 中the server configuration settings apply only for local databases....问题解决 vss 2005 配置服务器端的时候提示"IIS没有安装" ASP.NET MVC 实现二级域名 [转] Eclipse快捷键大全(转载) Android单元测试测试[转] 手动使用EDM 生成器 (EdmGen.exe) 工具生成 SSDL,CSDL,MSL 文件。 InnoDB ,MyISAM 等存储引擎中主外键的大小设置. 在 mysql 中 定义 数据库,表,列时,设定的各个的编码格式。 asm.jar 等包的重用问题之解决方案 汇总 Hibernate 与 jdbc,jndi+dbcp, proxool ,c3p0 连接池的配置 - micenter Hibernate中自动增长列的 在Annotations 中的配置和数据库设置默认值问题 - micenter java 字符串的编码与C#的区别 - micenter - 博客园 Hibernate中自动增长列的..hbm.xml 配置和数据库设置默认值问题 - micenter - 博客园
.NET 中 操作excel 系列--copy功能.
micenter · 2011-07-13 · via 博客园 - micenter

 在操作excel 中有很多是需要用到copy 功能的.如,单元格里面的函数功能.如果你要在excel中插入一条数据然后在这条数据的最后一列要从上面那一个复制一个相同的函数给他这时就需要用到copy功能了. 在excel 的range 接中有个copy方法.这个方法不紧紧可以拷贝值,样式,宏,函数都可以完整的拷到新的单元格中.

如下代码:

        ///   <summary> 
        
///   插行(在指定WorkSheet指定行上面插入指定数量行) 
        
///   </summary> 
        
///   <param   name= "rowIndex ">从哪行开始插入</param> 
        
///   <param name="funcColumnIndex">函数所在列</param>
        
///   <param   name= "count ">插入数量 </param> 
        public void InsertRows(int rowIndex,int funcColumnIndex, int count)
        {
            Microsoft.Office.Interop.Excel.Application excel 
= null;
            Microsoft.Office.Interop.Excel.Workbooks wbs 
= null;
            Microsoft.Office.Interop.Excel.Workbook wb 
= null;
            Microsoft.Office.Interop.Excel.Worksheet ws 
= null;
            
object Nothing = System.Reflection.Missing.Value; 
             
try  
            {  
            excel
=new Microsoft.Office.Interop.Excel.Application();  
            excel.UserControl
=true;  
            excel.DisplayAlerts
=false;
            excel.Application.Workbooks.Open(
"D:\\TEST.xls", Nothing, Nothing, Nothing, Nothing,  
            Nothing,Nothing,Nothing,Nothing,Nothing,  
            Nothing,Nothing,Nothing);  

            wbs

=excel.Workbooks;  
            wb
=wbs[1];  //每一个workbook
            ws=(Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];//表格名这 Sheeet1
           
          
                Range range 
= (Range)ws.Rows[rowIndex,Type.Missing];
                Range sourceRg 
= ((Range)range.Cells[1, funcColumnIndex]);//将被复制的函数
                for (int i = 0; i < count; i++)
                {
                    range.Insert(XlInsertShiftDirection.xlShiftDown,
0);//插入新行
                    for (int j = 1; j < funcColumnIndex; j++)
                    {
//给前面的几个单元格随便输入一些值.
                        ws.Cells[rowIndex + i, j] = i + j;
                    }
                    sourceRg.Copy(ws.Cells[rowIndex 
+ i, funcColumnIndex]);//复制到目标单元格(最后一列)
                }
            }
            
catch (Exception e)
            {                
                
throw e;

            }

  } 

 同理,excel 的worksheet 和 row 都可以这样拷贝.果要只拷贝单元格中的文本呢?那就要用到windows剪贴板了,下面是从网上拷贝下来的.

 xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(1);

Excel.Range range = xlSheet.get_Range("A1", Type.Missing);range.Value = "123";

System.Windows.Forms.Clipboard.SetDataObject(range.Value.ToString());

Excel.Worksheet sheet1 = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(2);Excel.Range range1 = sheet1.get_Range("B1", Type.Missing);sheet1.Paste(range1, false);要注意的是,这种方式只能选一个单元格,复制一个,不能选定一批单元格进行复制。