您的位置:首页 > 编程语言 > C#

office操作笔记:c#与excel之helloword

2017-06-02 01:01 435 查看
作为微软自己开发的语言c#和自己家的软件的交互真是太简单了。

例子很简单,APPlication实例化、添加工作簿、操纵工作簿、保存退出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;

namespace excel
{
class Program
{
static void Main(string[] args){

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp == null) {

Console.Error.WriteLine("excel is not properly installed!");

}

Workbook xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "ID";
xlWorkSheet.Cells[1, 2] = "Name";
xlWorkSheet.Cells[2, 1] = "1";
xlWorkSheet.Cells[2, 2] = "One";
xlWorkSheet.Cells[3, 1] = "2";
xlWorkSheet.Cells[3, 2] = "Two";
xlWorkBook.SaveAs("mm.xlsx");
xlWorkBook.Close(true, "mm.xlsx", System.Reflection.Missing.Value);
xlApp.Quit();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: