Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

export list to excel c#

/*
OK, here is a step-by-step guide if you want to use COM.

You have to have Excel installed.
Add a reference to your project to the excel interop dll. To do this on the .NET tab
select Microsoft.Office.Interop.Excel. There could be multiple assemblies with this name.
Select the appropriate for your Visual Studio AND Excel version.
Here is a code sample to create a new Workbook and fill a column with the items from your list.
*/
using NsExcel = Microsoft.Office.Interop.Excel;

public void ListToExcel(List<string> list)
{
    //start excel
    NsExcel.ApplicationClass excapp = new Microsoft.Office.Interop.Excel.ApplicationClass();

    //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:	est.xls";
    var workbook = excapp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);

    //Not done yet. You have to work on a specific sheet - note the cast
    //You may not have any sheets at all. Then you have 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 not 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 by now, so a detailed explanation about workbook.SaveAs and workbook.Close is not necessary
    //important: if you did not make excel visible terminating your application will terminate excel as well - I tested it
    //but if you did it - to be honest - I don't know how to close the main excel window - maybee somewhere around excapp.Windows or excapp.ActiveWindow
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# create excel file 
Csharp :: sorting a datatable in c# 
Csharp :: how to make player movement in unity 2d 
Csharp :: c# allowedusernamecharacters 
Csharp :: How to post request C# with returning responsebody 
Csharp :: c# implement ienumerable t 
Csharp :: c# string console readline array 
Csharp :: c# backup sql 
Csharp :: assembly project name c# .net 
Csharp :: wpf clear grid 
Csharp :: how to make a string a list of characters c# 
Csharp :: c# xml get child node by name 
Csharp :: authentication and authorization in asp.net c# with example 
Csharp :: random string generator c# 
Csharp :: c# remove all items from list where item value is null 
Csharp :: convert pdf to image c# 
Csharp :: c# chunk array 
Csharp :: get connection string from web.config in c# 
Csharp :: serial number unity pro 
Csharp :: declare string array c# without size 
Csharp :: interface property implementation c# 
Csharp :: unity detect when an object has been clicked 
Csharp :: new list/array with values c# 
Csharp :: div element position in screen 
Csharp :: loop for x amount of seconds c# 
Csharp :: c# compare dateTime with string 
Csharp :: private Vector3 direction; 
Csharp :: unity pickup and drop objects 
Csharp :: program.cs entity framework 
Csharp :: ontriggerenter2d 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =