Express C# to excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jed@auto-soft.co.za

    #1

    Express C# to excel

    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

  • =?Utf-8?B?U29tIE5hdGggU2h1a2xh?=

    #2
    RE: Express C# to excel


    class ReportGeneratio n
    {
    Excel.Applicati on oXL = new Excel.Applicati on();
    Excel.Workbook oBook;
    Excel.Worksheet oSheet, oSheet1, oSheet2;
    string fileNameMerchan t = null, fileNameRecycle = null;

    public ReportGeneratio n(string fileNameMerchan t, string
    fileNameRecycle )
    {
    this.fileNameMe rchant = fileNameMerchan t ;
    this.fileNameRe cycle = fileNameRecycle ;
    }

    public int MerchantReportG eneration(SqlCo nnection con)
    {
    int status=0;
    oBook = oXL.Workbooks.A dd(Type.Missing );
    oXL.Visible = false;
    try
    {
    if (oBook.Workshee ts.Count 0)
    {
    oSheet = (Excel.Workshee t)oBook.Sheets[1];
    oSheet.Activate ();
    SqlDataAdapter da = new SqlDataAdapter( "select * from
    table", con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    Range rg = oSheet.get_Rang e("A1", "B1");
    rg.Select();
    rg.Font.Bold = true;
    rg.Font.Name = "Arial";
    rg.Font.Size = 16;
    rg.WrapText = true;
    rg.MergeCells = true;
    rg.HorizontalAl ignment = Excel.Constants .xlCenter;




    rg = oSheet.get_Rang e("B1", Type.Missing);
    rg.Cells.Column Width = 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_Rang e(s.ToString(), Type.Missing);
    rg.Cells.Column Width = 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_Rang e(ss.ToString() , Type.Missing);
    rg.Cells.Column Width = 40;
    rg.HorizontalAl ignment = Excel.Constants .xlRight;
    rg.Value2 = "'" + ds.Tables[0].Rows[i][0].ToString();
    x++;




    }
    }
    fileNameMerchan t = fileNameMerchan t +
    DateTime.Now.To String("yyMMdd" ) + DateTime.Now.To String("HHmmss" );

    oXL.ActiveWorkb ook.SaveAs(file NameMerchant,Ex cel.XlFileForma t.xlWorkbookNor mal, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing,
    Excel.XlSaveAsA ccessMode.xlNoC hange,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing);
    System.Console. WriteLine("Merc hant Report is archived at :
    {0}", fileNameMerchan t);
    oXL.ActiveWorkb ook.Close(true, fileNameMerchan t, false);
    status = 1;
    }
    catch (Exception ex)
    {
    string excString = ex.Message + " " + ex.Source + " " +
    ex.TargetSite;
    //ExceptionLogger .LogException(e xcString);
    }

    }


    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.



    "jed@auto-soft.co.za" wrote:
    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
    >
    >

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Express C# to excel

      Hi,

      <jed@auto-soft.co.zawrote in message
      news:1192011785 .081824.19490@y 42g2000hsy.goog legroups.com...
      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


      Comment

      • Tara

        #4
        Re: Express C# to excel

        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 :


        Regards,
        Tara


        <jed@auto-soft.co.zawrote in message
        news:1192011785 .081824.19490@y 42g2000hsy.goog legroups.com...
        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
        >

        Comment

        Working...