zoukankan      html  css  js  c++  java
  • vs2010操作excel(增加excell的退出\保存)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Excel = Microsoft.Office.Interop.Excel;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace csharposexcell

    {

        class Program

        {

            static void Main(string[] args)

            {

                // Create a list of accounts.

                var bankAccounts = new List<Account> {new Account { ID = 345678, Balance = 541.27 },new Account { ID = 1230221,Balance = -127.44}};

                //DisplayInExcel(bankAccounts);

                CreateIconInWordDoc();

            }

     

            static void DisplayInExcel(IEnumerable<Account> accounts)

            {

                var excelApp = new Excel.Application();

                // Make the object visible.

                excelApp.Visible = false;

     

                // Create a new, empty workbook and add it to the collection returned 

                // by property Workbooks. The new workbook becomes the active workbook.

                // Add has an optional parameter for specifying a praticular template. 

                // Because no argument is sent in this example, Add creates a new workbook. 

                excelApp.Workbooks.Add();

     

                // This example uses a single workSheet. The explicit type casting is

                // removed in a later procedure.

                Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;

     

                // Establish column headings in cells A1 and B1.

                workSheet.Cells[1, "A"] = "ID Number";

                workSheet.Cells[1, "B"] = "Current Balance";

     

                var row = 1;

                foreach (var acct in accounts)

                {

                    row++;

                    workSheet.Cells[row, "A"] = acct.ID;

                    workSheet.Cells[row, "B"] = acct.Balance;

                }

     

                workSheet.Columns[1].AutoFit();

                workSheet.Columns[2].AutoFit();

                //以?下?三▂行D是?我ò加ó上?的?,?必?须?保馈?证¤excell的?顺3利?退?出?

                workSheet.SaveAs("d:\\okexcel.xls");

                excelApp.Quit();

                System.Console.WriteLine("excell ok");

                

            }

            //这a段?代洙?码?无T法ぁ?正y常£运?行D

            static void CreateIconInWordDoc()

            {

                var wordApp = new Word.Application();

                wordApp.Visible = true;

     

                // The Add method has four reference parameters, all of which are 

                // optional. Visual C# 2010 allows you to omit arguments for them if

                // the default values are what you want.

                wordApp.Documents.Add();

     

                // PasteSpecial has seven reference parameters, all of which are 

                // optional. This example uses named arguments to specify values 

                // for two of the parameters. Although these are reference 

                // parameters, you do not need to use the ref keyword, or to create 

                // variables to send in as arguments. You can send the values directly.

                wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);

                           

            }

     

        }

     

        public class Account

        {

            public int ID { getset; }

            public double Balance { getset; }

        }

     

    }

     

     

  • 相关阅读:
    chrome浏览器中安装以及使用Elasticsearch head 插件
    windows10 升级并安装配置 jmeter5.3
    linux下部署Elasticsearch6.8.1版本的集群
    【Rollo的Python之路】Python 爬虫系统学习 (八) logging模块的使用
    【Rollo的Python之路】Python 爬虫系统学习 (七) Scrapy初识
    【Rollo的Python之路】Python 爬虫系统学习 (六) Selenium 模拟登录
    【Rollo的Python之路】Python 爬虫系统学习 (五) Selenium
    【Rollo的Python之路】Python 爬虫系统学习 (四) XPath学习
    【Rollo的Python之路】Python 爬虫系统学习 (三)
    【Rollo的Python之路】Python sys argv[] 函数用法笔记
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/3128658.html
Copyright © 2011-2022 走看看