How to write an Excel file in C #

Source: Internet
Author: User

1. Create a Console Application and add Microsoft Excel 12.0 Object Library reference in the COM video card.

2. paste the following source code.

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();            }        }    }}

 

 

References:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.