ASP.NET - make the first sheet name of excel file as EXCELDATA

Asked By George n t on 13-Feb-12 12:06 AM
Hi friends,
I am using C#,ASP.NET,.NET FRAMEWORK 2.
I want to export data to excel 2007 in web application.
How to make the first sheet name of excel file as EXCELDATA instead of sheet1.
How to do this?

Thanks in advance
george
Web Star replied to George n t on 13-Feb-12 12:38 AM
If you want to specify your own worksheet name when exproting excel file than use ExcelPackage to easily set name of the worksheet by using property of that as follows
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = "Sample Worksheet"; //Setting Sheet's name
See more details here http://zeeshanumardotnet.blogspot.in/2010/08/creating-advanced-excel-2007-reports-on.html 
Somesh Yadav replied to George n t on 13-Feb-12 12:51 AM
Hi you can do this by the following code.

var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirectory());
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();

adapter
.Fill(ds, "anyNameHere");

DataTable data = ds.Tables["anyNameHere"];

This is what I usually use. It is a little different because I usually stick a AsEnumerable() at the edit of the tables:

var data = ds.Tables["anyNameHere"].AsEnumerable();

as this lets me use LINQ to search and build structs from the fields.

var query = data.Where(x => x.Field<string>("phoneNumber") != string.Empty).Select(x =>
                       
new MyContact
                               
{
                                        firstName
= x.Field<string>("First Name"),
                                        lastName
= x.Field<string>("Last Name"),
                                        phoneNumber
=x.Field<string>("Phone Number"),
                               
});
kalpana aparnathi replied to George n t on 13-Feb-12 04:55 AM
hi,

ExcelWorksheet w = p.Workbook.Worksheets["EXCELDATA"];

Regards,