C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Export Sql Table to Excel for Windows Application


Posted Date: 26 May 2005    Resource Type: Articles    Category: .NET Framework
Author: sanjeev kumarMember Level: Gold    
Rating: 1 out of 5Points: 8



Introduction



To run this application, you have to add com reference Microsoft Excel 9.0 Object library


try
try
{
SqlConnection conn = new SqlConnection("Integrated Security=yes;Initial Catalog=Emp;Data Source=(local)");
conn.Open();

SqlCommand command = new SqlCommand("select * from emp", conn);

SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet dataset = new DataSet();

adapter.Fill(dataset);


Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true);

System.Data.DataTable table = dataset.Tables[0];
int ColumnIndex=0;
foreach( System.Data.DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName; } int rowIndex=0;
foreach(DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex=0; foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,ColumnIndex]=row[col.ColumnName];
}
}

excel.Visible = true;

Worksheet worksheet = (Worksheet)excel.ActiveSheet;

worksheet.Activate();


}
catch (XmlException exml)
{
// catch an xmlexception errors
MessageBox.Show( exml.Message);
}



Responses

Author: Munish Sehgal    30 May 2005Member Level: Bronze   Points : 0
The access denied error is generated although the folder has the permissions of the IUSER / ASPNET account.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Popup windows in ASP.NET using C#
Previous Resource: .NET and COM+ (Part 2) - Creating Transactional Component
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use