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

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - 散步的蠕虫

[整理收藏]CSS Hack IE6,IE7,IE8 and Firefox 【转载】Javascript标准DOM Range操作 [收藏]winform拖动方法 [收藏]SQL SERVER2005自动备份 [更新+源码]一个随时随地随便记的超轻量级的记事本软件WheneverNote V1.0.9.218 [发布+源码]一个随时随地随便记的超轻量级的记事本软件WheneverNote V1.0.9.215 [总结]Server Application Error(IIS5 HTTP500)内部错误分析及解决办法 [转载]XML和HTML常用转义字符 新概念III WCF - Message Security with Mutual Certificates WCF - Common Security Scenarios 【转载】LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control) 【转载】LINQ to SQL (Part 4 - Updating our Database) 【转载】LINQ to SQL (Part 3 - Querying our Database) 【转载】LINQ to SQL (Part 2 - Defining our Data Model Classes) 【转载】Using LINQ to SQL (Part 1) 常用开发辅助工具清单 JavaScript 实用方法库 Local Resource应用概述
C# TO Excel
散步的蠕虫 · 2008-09-02 · via 博客园 - 散步的蠕虫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;namespace WinFormTest
{
    
public class ExcelProvider
    {
        
//private Excel.Application objApp;
        
//private Excel._Workbook objBook;

        
public ExcelProvider()
        { }
public void Create()
        {
//创建Application对象 
            Excel.Application xApp = new Excel.ApplicationClass();

            xApp.Visible 

= false;
            
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件 
            Excel.Workbook xBook = xApp.Workbooks._Open(@"C:\SampleTemplate.xls",
            Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码 
            
            
//指定要操作的Sheet,两种方式:
            Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets.Sheets[1];
            
//Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet; //读取数据,通过Range对象 
            
//Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
            
//Console.WriteLine(rng1.Value2);//读取,通过Range对象,但使用不同的接口得到Range 
            
//Excel.Range rng2 = (Excel.Range)xSheet.Cells[3, 1];
            
//Console.WriteLine(rng2.Value2);//写入数据 
            
            Excel.Range rng3 
= xSheet.get_Range("C6", Missing.Value);
            rng3.Value2 
= "Hello";
            rng3.Interior.ColorIndex 
= 6//设置Range的背景色 //保存方式一:保存WorkBook 
            xBook.SaveAs(@"C:\SampleTemplate200809002.xls",
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);
//保存方式二:保存WorkSheet 
            
//xSheet.SaveAs(@"C:\SampleTemplate200809002.xls",
            
//Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            
//Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//保存方式三 
            
//xBook.Save();

            xSheet 
= null;
            xBook 
= null;
            xApp.Quit(); 
//这一句是非常重要的,否则Excel对象不能从内存中退出 
            xApp = null

        }
    }
}