Exporting data from a datatable to a Excel Spreadsheet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LoanB
    New Member
    • Nov 2007
    • 62

    Exporting data from a datatable to a Excel Spreadsheet

    Hi Guys

    I am writing an application where by I need to export data from a data table (already populated with data from SQL Server).

    I have never done this before so not sure where to start. Assuming I already have a connection to a spreadsheet called Orders.xls.

    What code do I need to take data from the data table and insert it into a specific cell in the spreadsheet?

    E.g.

    for (int c = 0; c < dt.Rows.Count; c++)
    {
    trip = (dt.Rows[c]["TRIP"]).ToString();

    // Here I want to set it that cell A1 in the spreadsheet must have trip in it
    }

    I have numerous records from which certain field need to populate different cells in the spreadsheet.

    PLEASE any advice will greatly be appreciated.

    Have a great weekend!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is some examples that may help:
    Export Data from Dataset or Datatable to Excel File

    Comment

    • LoanB
      New Member
      • Nov 2007
      • 62

      #3
      That's great Thanks a lot!.

      Comment

      • LoanB
        New Member
        • Nov 2007
        • 62

        #4
        I decided to go by using the ADO connection option.

        So after much reading I have established my connection and can select records. How ever I'm having trouble with inserting:

        I'm sure its just syntax:
        Please take a look at these statements:

        sql = "INSERT INTO [Sheet1$] (A1, A2, A3) VALUES('" + name + "',\'" + size + "',\'" + flav + "')";

        No go...

        Also no go is:

        sql = "INSERT INTO [Sheet1$] (A1, A2, A3) VALUES('" + name + "','" + size + "','" + flav + "')";

        (No Back Slashes)

        Any advice on the correct SQL:

        All I want is to insert into cells A1 the value in "name", A2 the value in "size" and A3 the value in "flav".

        Thanks,

        Comment

        Working...