Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hI,
I have a table as shown:

C#
DataSet ds = new DataSet();
           System.Data.DataTable table = new System.Data.DataTable();
           table = ds.Tables.Add();
           string[] s1 = { "name", "age" };
           string[] s2 = { "John", "30" };
           table.Columns.Add("Attributes");
           table.Columns.Add("Values");
           for (int i = 0; i < 2; ++i)
           {
               table.Rows.Add(s1[i], s2[i]);
           }


I need to put the values of Attributes and Values into an excel sheet. I've been trying many methods, but only to get errors :( Please help me out with this. Thanks!
Posted
Updated 31-Oct-11 0:02am
v2

try this
C#
// Create new ExcelFile.
var ef = new ExcelFile();

// Imports all the tables from DataSet to new file.
foreach (DataTable dataTable in dataSet.Tables)
{
    // Add new worksheet to the file.
    var ws = ef.Worksheets.Add(dataTable.TableName);

    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
}

// Save the file to XLS format.
ef.SaveXls("DataSet.xls");
 
Share this answer
 
Are you looking for something like this

http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx[^]
 
Share this answer
 
Comments
bizzare1988 31-Oct-11 6:18am    
Hi Anuja,My application is not a web application. will it work then? When i do this, i get an error: Object reference not set to an instance of an object.
On checking,The response value is null. What can i do?
Anuja Pawar Indore 31-Oct-11 7:30am    
Refer this thread too
http://stackoverflow.com/questions/373925/c-sharp-winforms-app-export-dataset-to-excel
bizzare1988 2-Nov-11 0:07am    
works perfectly, the link you sent! thanks a lot :) i mean, the second link, for verification.
Anuja Pawar Indore 2-Nov-11 2:10am    
WC :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900