Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Express C# to excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jed@auto-soft.co.za - 10 Oct 2007 11:23 GMT
What is the best way for me to send data from C# to excel and open the
file in excel from a windows application please help.Is there an add
on i can download to gain access to the excel namespace or some other
way.thanks
Som Nath Shukla - 10 Oct 2007 12:59 GMT
class ReportGeneration
   {
       Excel.Application oXL = new Excel.Application();
       Excel.Workbook oBook;
       Excel.Worksheet oSheet, oSheet1, oSheet2;
       string fileNameMerchant = null, fileNameRecycle = null;

       public ReportGeneration(string fileNameMerchant, string
fileNameRecycle)
       {
           this.fileNameMerchant = fileNameMerchant ;
           this.fileNameRecycle = fileNameRecycle ;
       }

       public int MerchantReportGeneration(SqlConnection con)
       {
           int status=0;
           oBook = oXL.Workbooks.Add(Type.Missing);
           oXL.Visible = false;
           try
           {
               if (oBook.Worksheets.Count > 0)
               {
                   oSheet = (Excel.Worksheet)oBook.Sheets[1];
                   oSheet.Activate();
                   SqlDataAdapter da = new SqlDataAdapter("select * from
table", con);
                   DataSet ds = new DataSet();
                   da.Fill(ds);
                   Range rg = oSheet.get_Range("A1", "B1");
                   rg.Select();
                   rg.Font.Bold = true;
                   rg.Font.Name = "Arial";
                   rg.Font.Size = 16;
                   rg.WrapText = true;
                   rg.MergeCells = true;
                   rg.HorizontalAlignment = Excel.Constants.xlCenter;
                 
                 

                   rg = oSheet.get_Range("B1", Type.Missing);
                   rg.Cells.ColumnWidth = 30;
                   rg.Font.Bold = true;
                   rg.Font.Name = "Arial";
                   rg.Font.Size = 10;
                   rg.Value2 = "columnname";

             

                   int x = 9;

                   int i = 0;
                   while (i < count)
                   {
                       string s = "A" + x.ToString();
                       rg = oSheet.get_Range(s.ToString(), Type.Missing);
                       rg.Cells.ColumnWidth = 30;
                       rg.Font.Bold = true;
                       rg.Font.Name = "Arial";
                       rg.Font.Size = 10;
                       rg.Value2 = ds.Tables[0].Rows[i][1].ToString();

                       string ss = "B" + x.ToString();
                       rg = oSheet.get_Range(ss.ToString(), Type.Missing);
                       rg.Cells.ColumnWidth = 40;
                       rg.HorizontalAlignment = Excel.Constants.xlRight;
                       rg.Value2 = "'" + ds.Tables[0].Rows[i][0].ToString();
                       x++;

                 
                   
                   
                   }
               }
               fileNameMerchant = fileNameMerchant +
DateTime.Now.ToString("yyMMdd") + DateTime.Now.ToString("HHmmss");
               
oXL.ActiveWorkbook.SaveAs(fileNameMerchant,Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
               Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
               System.Console.WriteLine("Merchant Report is archived at :
{0}", fileNameMerchant);
               oXL.ActiveWorkbook.Close(true, fileNameMerchant, false);
               status = 1;
           }
           catch (Exception ex)
           {
               string excString = ex.Message + "   " + ex.Source + "   " +
ex.TargetSite;
               //ExceptionLogger.LogException(excString);
           }
         
       }

this is code for generating excel report throgh .net .steps are given below
1. add microsoft excell as reference from com .
2. using Excel add name space.
3. after selecting worksheet u can add as much valu as u want
here i am taking data from table and throu a for loop assingning data

u can take data from where u want

for opening excel
repeate step 1 and 2 , after defing variale. use worksheet.ope() method.

at ur sytem u must have excel installed.


> What is the best way for me to send data from C# to excel and open the
> file in excel from a windows application please help.Is there an add
> on i can download to gain access to the excel namespace or some other
> way.thanks
Ignacio Machin ( .NET/ C# MVP ) - 10 Oct 2007 14:48 GMT
Hi,

> What is the best way for me to send data from C# to excel and open the
> file in excel from a windows application please help.Is there an add
> on i can download to gain access to the excel namespace or some other
> way.thanks

Well, it can be as easy es creating a CSV file and call excel with that file
as parameter
Tara - 12 Oct 2007 05:35 GMT
Hi ,
To refer the namespace of Excel , Add reference from com tab in the
reference . Choose Microsoft Excel 11.0 Object library or Microsoft Excel
5.0 library , whichever is available .
 For more information , refer the following website :
http://www.codeproject.com/office/csharp_excel.asp

Regards,
Tara

> What is the best way for me to send data from C# to excel and open the
> file in excel from a windows application please help.Is there an add
> on i can download to gain access to the excel namespace or some other
> way.thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.