如何在C#中写Excel文件

1. 创建一个Console Application, 添加COM选显卡中的Microsoft Excel 12.0 Object Library引用.

2. 粘贴下面的源代码.

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace CShartExcelTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string[,] values = { 
                                             {"Tom",   "18",  "Beijing",     "13912345678"},
                                             {"Jerry",  "17",  "Shanghai",  "13687654321"}
                                        };
                Excel.Application objApp;
                Excel._Workbook objBook;
                Excel.Workbooks objBooks;
                Excel.Sheets objSheets;
                Excel._Worksheet objSheet;
                Excel.Range range;

                // Instantiate Excel and start a new workbook.
                objApp = new Excel.Application();
                objBooks = objApp.Workbooks;
                objBook = objBooks.Add(System.Reflection.Missing.Value);
                objSheets = objBook.Worksheets;
                objSheet = (Excel._Worksheet)objSheets.get_Item(1);

                //Get the range where the starting cell has the address
                range = objSheet.get_Range("A2", System.Reflection.Missing.Value);
                range = range.get_Resize(2, 4);
                range.set_Value(System.Reflection.Missing.Value, values);

                //Construct the header.
                objSheet.Cells[1, 1] = "Name";
                objSheet.Cells[1, 2] = "Age";
                objSheet.Cells[1, 3] = "HomeTown";
                objSheet.Cells[1, 4] = "Mobile";

                bool bSave = false;

                if (bSave)
                {
                    //Return control of Excel to the user.
                    objApp.Visible = true;
                    objApp.UserControl = true;
                }
                else
                {
                    objBook.SaveAs(@"c:\temp\csharp-Excel.xls",
                        Excel.XlFileFormat.xlWorkbookNormal,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        Excel.XlSaveAsAccessMode.xlExclusive,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value);

                    objBook.Close(true,
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value);
                    objApp.Quit();

                    releaseObject(objSheet);
                    releaseObject(objBook);
                    releaseObject(objApp);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

    }
}

 

 

参考资料:

How to create Excel file in C#

http://csharp.net-informations.com/excel/csharp-create-excel.htm 

How to automate Excel by using Visual C# to fill or to obtain data in a range by using arrays

http://support.microsoft.com/kb/302096

How to automate Microsoft Excel from Microsoft Visual C# .NET

http://support.microsoft.com/kb/302084

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值