1

I have a list array which contains some data. Currently I can see the output in the console and now trying to add to an excel file. Can anyone explain me how to do that. Here is the code to create an excel sheet and write something to it. But how do I combine both of this codes to see the output in excel. I tried several combinations but couldn't write to excel. I'm newbie to c#.Thanks in advance!

foreach (Match m in linkParser.Matches(html))
                {
                 list.Add(m.Value);
                 Console.WriteLine(m.Value); 
                }



    Excel.Application oApp; // to open excel
                Excel.Worksheet oSheet;
                Excel.Workbook oBook;
                oApp = new Excel.Application();
                oBook = oApp.Workbooks.Add();
                oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
                string fileTest = "output123.xlsx";
                if (File.Exists(fileTest))
                {
                    File.Delete(fileTest);
                }


                oSheet.Cells[1, 1] = "some value";

                oBook.SaveAs(fileTest);
                oBook.Close();
                oApp.Quit();
221B
  • 831
  • 2
  • 11
  • 21
  • There's plenty of libraries to do it in [this old question](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp?rq=1). You can also hack something together by writing HTML and calling it an .xls, but it's not a real Excel file. – Rup Nov 03 '13 at 18:41
  • One possibility would be to automate Office products [using Primary Interop Assemblies - PIA](http://msdn.microsoft.com/en-us/library/dww0e04a%28v=vs.90%29.aspx). Using PIA you create an excel instance, open / create a workbook through it, fill the sheet with data, save it and close the excel instance. – keenthinker Nov 03 '13 at 18:45

2 Answers2

2

Interop, cells - a bit overhead. Create an empty workbook and save it as binary resource. Anytime you need to create a new file, just grab that resource and write to disk. Then use Microsoft.Ace.OleDb.<version> to connect to this Excel file. You can write to it like to a database table. Here is a good article that explains on the subject

http://yoursandmyideas.wordpress.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/

See, with interop, especially if you write server-side application, it is not efficient to do new Excel.Application() - you literally open an Excel program. You don't want to open any Office program on the server, unless the server is dedicated for it, and you have logic that can recover memory from stuck Office App. With ACE you just open a connection - really thin, memory efficient approach.

T.S.
  • 18,195
  • 11
  • 58
  • 78
0

Include Excel interop in your reference. Here's some quick and dirty code. Please mention which framework you are using as some new syntax have been added in v4.0 which will not work on v3.5

using Excel = Microsoft.Office.Interop.Excel;

Now write following code to create Excel app.

    Excel.Application excel = new Excel.Application();
    excel.Visible = true;
    Excel.Workbook wb = excel.Workbooks.Add();
    Excel.Worksheet sh = wb.Sheets.Add();
    sh.Name = "TestSheet";

    // Write some kind of loop to write your values in sheet. Here i am adding values in 1st columns
    for (int i = 0; i < list.Count; i++)
    {
       sh.Cells[i.ToString(), "A"].Value2 = list[i];

    }
    string filePath = @"C:\output123.xlsx";

    // Save file to filePath
    wb.Save(filePath);

    wb.Close(true);
    excel.Quit(); 

** PS- I haven't tested the code but you should be able to run it with some little tweaks.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116