创建EXCLE 文档的例子

来源:互联网 发布:淘宝的活动有哪些 编辑:程序博客网 时间:2024/06/13 13:17

创建EXCLE 文档的例子

1. 选择 "References", 右键点击,选择"Add Reference..."
2. 选择 ".NET" , 选择 "Microsoft.Office.Interop.Execl ..."
3. 选择 OK
4. 检察 Properties 属性,检察 "Embed Interop Types" 是否为 True, 设置为True.

用下面的例子代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using Excel = Microsoft.Office.Interop.Excel;namespace CreateExcelWorkbook{    class Program    {        static void Main(string[] args)        {            int[] values = {4, 6, 18, 2, 1, 76, 0, 3, 11};            CreateWorkbook(values, @"C:\SampleFolder\SampleWorkbook.xls");        }        static void CreateWorkbook(int[] values, string filePath)        {            Excel.Application excelApp = null;            Excel.Workbook wkbk;            Excel.Worksheet sheet;            try            {                    // Start Excel and create a workbook and worksheet.                    excelApp = new Excel.Application();                    wkbk = excelApp.Workbooks.Add();                    sheet = wkbk.Sheets.Add() as Excel.Worksheet;                    sheet.Name = "Sample Worksheet";                    // Write a column of values.                    // In the For loop, both the row index and array index start at 1.                    // Therefore the value of 4 at array index 0 is not included.                    for (int i = 1; i < values.Length; i++)                    {                        sheet.Cells[i, 1] = values[i];                    }                    // Suppress any alerts and save the file. Create the directory                     // if it does not exist. Overwrite the file if it exists.                    excelApp.DisplayAlerts = false;                    string folderPath = Path.GetDirectoryName(filePath);                    if (!Directory.Exists(folderPath))                    {                        Directory.CreateDirectory(folderPath);                    }                    wkbk.SaveAs(filePath);            }            catch            {            }            finally            {                sheet = null;                wkbk = null;                // Close Excel.                excelApp.Quit();                excelApp = null;            }        }    }}