C # note38:export data into Excel

Source: Internet
Author: User

    • Microsoft.Office.Interop.Excel
    1. You have the to have Excel installed.
    2. ADD a reference to your project to the Excel interop DLL. To does this on the. NET tab Select Microsoft.Office.Interop.Excel. There could is multiple assemblies with the this name. Select the appropriate for your Visual Studio and Excel version.
    3. Here's a code sample to create a new Workbook and fill a column with the items from your list
If you want to make Excel visible Excapp.    Visible = true; Create a blank workbook var workbook = Excapp.    Workbooks.Add (NsExcel.XlWBATemplate.xlWBATWorksheet); or open one-this is no pleasant, but Yue ' re probably interested in the first parameter string Workbookpath = "C:\te    St.xls "; var workbook = Excapp. Workbooks.Open (Workbookpath, 0, False, 5, "", "", False, Excel.XlPlatform.xlWindows, "", True, False, 0, Tru    E, False, false); Not done yet. You should have a specific sheet-note the cast//you and no sheets at all. Then you had to add one with NSEXCEL.WORKSHEET.ADD () var sheet = (nsexcel.worksheet) workbook. SHEETS[1];  Indexing starts from 1//do something usefull:you Select now an individual cell var range = Sheet.get_range ("A1",    "A1"); Range. Value2 = "Test";    Value2 is a typo//now the list string cellname;    int counter = 1; foreach (var item in list) {cellname = "A" + couNter.        ToString ();        var range = Sheet.get_range (Cellname, cellname); Range. Value2 = Item.        ToString ();    ++counter; }//you ' ve probably got the point is now, so a detailed explanation about workbook. SaveAs and Workbook.  Close is not necessary//important:if do not make Excel visible terminating your application would terminate Excel As Well-i tested it//but if you do it-to be honest-i don ' t know what to close the main Excel Window-maybee so Mewhere around Excapp. Windows or Excapp. ActiveWindow}

 

Using the CSV idea

Using system.io;using (StreamWriter SW = File.createtext ("List.csv")) {for  (int i = 0; i < L.count; i++)  {    s W.writeline (L[i]);}  }

  

Using Closedxml Library (there is no need to install MS Excel

 

Https://github.com/closedxml/closedxml/wiki

https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/

C # note38:export data into Excel

Related Article

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.