avisnet's blog

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


首先添加对Com组件Microsoft.Excel 11.0 Object Library的引用。

using System;

using System.Data;

using System.Data.SqlClient;

using System.Reflection;

 

using Excel = Microsoft.Office.Interop.Excel;

 

namespace Avisnet

{

    class ExcelProgram

    {

        static void Main(string[] args)

        {

            ExcelProgram p = new ExcelProgram();

            p.FormulaTest();

        }

 

        public void FormulaTest()

        {

            // Starts excel and gets an excel application object.

            Excel.Application excel = new Excel.Application();

 

            // Adds a new workbook to the excel application.

            Excel.Workbook book = excel.Workbooks.Add(Missing.Value);

            Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;

 

            try

            {

                Excel.Range range = sheet.get_Range("D2", "D8");

                range.Formula = "=RAND() * 100000";

                range.NumberFormat = "$0.00";

 

                // Saves and cloeses the workbook;

                book.Close(true, "C:\\fx.xls", Missing.Value);

            }

            catch

            {

                throw;

            }

            finally

            {

                // Exit excel application.

                excel.Quit();

            }

        }

    }

}

 

 

posted on 2006-09-17 00:16  avisnet  阅读(1289)  评论(0编辑  收藏  举报