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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/aoumeior/article/details/72840342

作为微软自己开发的语言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();

        }
    }
}
展开阅读全文

没有更多推荐了,返回首页