tags:

views:

690

answers:

5
BOWorkerDetail oBOWorkerDetail = new BOWorkerDetail();
            WorkerDetails oWorkerDetails = new WorkerDetails();
            oWorkerDetails = oBOWorkerDetail.Gets();

oWorkerdetails is a collection of all workers. I need to save this value in Excel. how to do ? Can any one help?i Work on C# window platform vs05.

+1  A: 

My recomendation is to use a native library and avoid Interop. Take a look at Aspose.Cells

Gonzalo Quero
+2  A: 

hi there, you should look at this http://www.connectionstrings.com/excel, there you can find some connection strings that works with ODBC engine. An example is:

OdbcConnection connection = new OdbcConnection(@"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + xlsFilePath + "; ReadOnly=False; DefaultDir=" + xlsDir + ";");

(You should have Microsoft Excel installed on the workstation)

then you can create a IDbCommand like this: IDbCommand command = connection.CreateCommand();

you can use this command like any sql command : "CREATE TABLE" , "INSET INTO" for each of DataRow in your DataTable.

Hope this helps...

Alex Pacurar
Why the down vote? This seems perfectly reasonable to me. I have used this server side and it works well.
BarneyHDog
+1  A: 

If you don't have Excel installed on the machine running the code you can export as XML or CSV (Comma Separated Values) file and then import that in to Excel at a later date.

If you use XML you can export the schema as well.

As BarneyHDog points out in the comments, Excel can do nasty to things to data based on what it thinks the data type. So if you go down this route double check that your output is handled correctly.

ChrisF
CSV can work well but you have to be aware that Excel can do nasty to things to data based on what it thinks the data type. I have imported barcodes via CSV numerous times only to have Excel mangle them in to scientific notation.
BarneyHDog
Fair point - I'll add a caveat to the answer
ChrisF
+1  A: 

Here is an example of using Visual Studio Tools for Office 2008 and Office Excel Interop to write a List> to new Excel file:

using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.Office.Interop.Excel;

namespace Project1
{
  public class ExportExcel
  {
    public void Export(string fileName, List<List<string>> data)
    {
      _Application excel = null;
      _Workbook wb = null;
      try
      {
        excel = new ApplicationClass { Visible = false }; // new excel application (not visible)
        wb = excel.Workbooks.Add(Missing.Value); // new excel file

        var sh = ((_Worksheet)wb.ActiveSheet); // current sheet
        for (var i = 0; i < data.Count; i++)
        {
          var listMaster = data[i];
          for (var j = 0; j < listMaster.Count; j++)
          {
            sh.get_Range(sh.Cells[j + 1, i + 1], sh.Cells[j + 1, i + 1]).Value2 = listMaster[j];
            // get_Range(start, end) where start, end is in R1C1 notation
          }
        }
      }
      finally
      {
        if (wb != null)
        {
          wb.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        }
        if (excel != null)
        {
          excel.Quit();
        }
        GC.Collect();
      }
    }
  }
}

VSTO requires Excel to be installed on client machine

abatishchev
A: 

A really simple way of doing this is using the filehelpers library

http://filehelpers.sourceforge.net/

there are loads of examples there.

The library is really reliable, I ve used it many times. Hope it helps

Miau