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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 鱼十七

[Programming Entity Framework] 第3章 查询实体数据模型(EDM)(二) [Programming Entity Framework] 第3章 查询实体数据模型(EDM)(一) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(三) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(二) [Programming Entity Framework] 第2章 探究实体数据模型(EDM)(一) [Programming Entity Framework] 第1章 ADO.NET实体框架介绍(二) [Programming Entity Framework] 第1章 ADO.NET实体框架介绍(一) Programming Entity Framework 第二版 翻译索引 设计模式学习笔记 1.介绍 WP7 学习手记1.你好 WP7 excel2007内容转成xml ASP.NET 生成静态页面的思路介绍 scrum介绍 SQL SERVER 2005服务无法启动问题的解决办法 - 鱼十七 [翻译]MS project 与 MS Team Foundation Server(TFS)的域映射 - 鱼十七 [翻译]通过调用多个动作创建ASP.NET MVC视图 [翻译]在ASP.NET MVC中绑定数据(包括分页和排序) MOSS文章过滤QueryString Filter Web Parts使用及Web Parts部署 ASP.NET方式在工作组内修改帐户密码 - 鱼十七
C#操作Excel知识点
鱼十七 · 2010-08-04 · via 博客园 - 鱼十七

近期在使用C#操作excel,主要是读取excel模板,复制其中的模板sheet页,生成多个sheet页填充相应数据后另存到excel文件,所用到的知识点如下。

一、添加引用和命名空间

添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

代码中添加引用using Microsoft.Office.Interop.Excel;

二、Excel类的简单介绍

此命名空间下关于Excel类的结构分别为:
ApplicationClass - 就是我们的excel应用程序。
Workbook - 就是我们平常见的一个个excel文件,经常是使用Workbooks类对其进行操作。
Worksheet - 就是excel文件中的一个个sheet页。
Worksheet.Cells[row, column] - 就是某行某列的单元格,注意这里的下标row和column都是从1开始的,跟我平常用的数组或集合的下标有所不同。

知道了上述基本知识后,利用此类来操作excel就清晰了很多。

三、Excel的操作
任何操作Excel的动作首先肯定是用excel应用程序,首先要new一个ApplicationClass 实例,并在最后将此实例释放。

ApplicationClass xlsApp = new ApplicationClass();  // 1. 创建Excel应用程序对象的一个实例,相当于我们从开始菜单打开Excel应用程序。
if (xlsApp == null)
{
  
//对此实例进行验证,如果为null则表示运行此代码的机器可能未安装Excel
}

1. 打开现有的Excel文件

代码

Workbook workbook = xlsApp.Workbooks.Open(excelFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Worksheet mySheet 

= workbook.Sheets[1as Worksheet; //第一个sheet页
mySheet.Name = "testsheet";  //这里修改sheet名称

2.复制sheet页

mySheet.Copy(Type.Missing, workbook.Sheets[1]); //复制mySheet成一个新的sheet页,复制完后的名称是mySheet页名称后加一个(2),这里就是testsheet(2),复制完后,Worksheet的数量增加一个

注意 这里Copy方法的两个参数,指是的复制出来新的sheet页是在指定sheet页的前面还是后面,上面的例子就是指复制的sheet页在第一个sheet页的后面。

3.删除sheet页

xlsApp.DisplayAlerts = false//如果想删除某个sheet页,首先要将此项设为fasle。
(xlsApp.ActiveWorkbook.Sheets[1as Worksheet).Delete();

4.选中sheet页

(xlsApp.ActiveWorkbook.Sheets[1as Worksheet).Select(Type.Missing); //选中某个sheet页

5.另存excel文件

workbook.Saved = true;
workbook.SaveCopyAs(filepath);

6.释放excel资源            

workbook.Close(true, Type.Missing, Type.Missing);
workbook 
= null;
xlsApp.Quit();
xlsApp 
= null;