Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2005, 12:54 AM
Authorized User
 
Join Date: Aug 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Byock
Default How to populate an excel files as an attachment to

Hi,

Im a newbie in C#....I need to send an email with an excel file containing the data from the database....I use select statement to call retrieve the needed data....How would I do the attachment and how would i place the data from the database to an excel file? Sample codes please.....

thank you



 
Old August 9th, 2005, 08:23 AM
Registered User
 
Join Date: Oct 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You Can place the data from the database to an excel file using Excel.Querytable.

http://support.microsoft.com/?kbid=306023




 
Old August 9th, 2005, 09:33 PM
Authorized User
 
Join Date: Aug 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Byock
Default

i had tried the codes in that link that you gave mrbalaji but the problem is i dont have Microsoft Excel 10.0 Object Library or Microsoft Excel 11.0 Object Library in my Add Reference option because i am using windows 2000 OS not windows XP....i only have Microsoft Excel 9.0 Object Library.....so how would i do it? I cannot change OS...cos it very expensive to buy a license one...

 
Old August 10th, 2005, 03:23 AM
Registered User
 
Join Date: Oct 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this way using Excel 9.0 Obejct libary

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);
}








Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating Excel FIles from resource Files abinashpatra ASP.NET 1.0 and 1.1 Basics 0 July 28th, 2008 02:07 AM
How to mail btowse files as an attachment jijish Classic ASP Basics 1 April 30th, 2008 08:56 AM
How to mail uploaded files as an attachment jijish Classic ASP Professional 0 April 29th, 2008 03:35 PM
Merge many excel files into one excel files Roshanjoshi2001 Excel VBA 2 November 14th, 2007 06:23 PM
Opening Excel and Excel files dinosaur_uk VB.NET 2002/2003 Basics 3 September 17th, 2004 03:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.