Skip to content

Replies

0
JS
JS answered on Oct 13, 2011 2:49 AM

Hi.

I found the solution you want. ^^

step 1

drag UltraWebGridDocumentExporter control to ur page( include ultragrid )

step 2

drag button control and doubleclick.

and modify this code

 declare this to your Grid and EXport to EXcel

DataTable m_oHeadDT = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        for (int i = 0; i < 24; i++)
        {
            m_oHeadDT.Columns.Add("HAHA" + i.ToString());
        }

        for (int k = 0; k< 100; k++)
        {
            DataRow oDR;
            oDR = m_oHeadDT.NewRow();
            oDR.ItemArray = new string[] {"Code : [" + k.ToString() + "]" , "Company Name", "Fiscal Year", "Sales", 
          "Operating Income", "Pretax Continued Operaion Income", "Net Income", "EPS", "", 
          "Sales", "Operating Income", "Pretax Continued Operaion Income", "Net Income", "EPS", "", 
          "Sales", "Operating Income", "Pretax Continued Operaion Income", "Net Income", "EPS", 
          "MKF500 Sector", "Announ. Date" };

            m_oHeadDT.Rows.Add(oDR);
        }


        this.UltraWebGrid1.DataSource = m_oHeadDT;
        this.UltraWebGrid1.DataBind();  

 

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
           //version 10.X
           //Infragistics.Documents.Excel.Worksheet worksheet = workbook.Worksheets.Add(table.TableName);

 
///   Version 8.2 ??
            Infragistics.Excel.Workbook workbook = new Infragistics.Excel.Workbook ();
            // Create the worksheet to represent this data table
            Infragistics.Excel.Worksheet worksheet = workbook.Worksheets.Add("EXCELTEST");
               
            // Create column headers for each column
            for (int columnIndex = 0; columnIndex < m_oHeadDT.Columns.Count; columnIndex++)
            {
                worksheet.Rows[0].Cells[columnIndex].Value  = m_oHeadDT.Columns[columnIndex].ColumnName;
            }

            // Starting at row index 1, copy all data rows in
            // the data table to the worksheet
            int rowIndex = 1;
            foreach (DataRow dataRow in m_oHeadDT.Rows)
            {
                Infragistics.Excel.WorksheetRow row = worksheet.Rows[rowIndex++];

                for (int columnIndex = 0; columnIndex < dataRow.ItemArray.Length; columnIndex++)
                {
                    row.Cells[columnIndex].Value = dataRow.ItemArray[columnIndex];
                }
            }

            workbook.Save("C:\\aaa.xls");
            Page.Response.Write("<script language='javascript'>alert('Check it out  C:\\aaa.xls');</script>");    
    }

you can see the result like this.

HAHA0 HAHA1 HAHA2 HAHA3 HAHA4 HAHA5 HAHA6 HAHA7
Code : [0] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS
Code : [1] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS
Code : [2] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS
Code : [3] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS
Code : [4] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS
Code : [5] Company Name Fiscal Year Sales Operating Income Pretax Continued Operaion Income Net Income EPS

 

 

Good Luck!!

 

 

0
JS
JS answered on Oct 12, 2011 3:22 AM

Hi,

I suprised when I got the mail about " ExcelExport ………".

Actually I forgot the source code about that.

but when I click your ID in Infragistics webpage then some questions are poped up!!

I thought that you are in a hurry. Right?

So I decided to find that source code.         ðŸ™‚

Hers is My Code.

 

Custome Control

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using Infragistics.WebUI.UltraWebGrid;

namespace FrankoControl
{
    //Custom Control Icon.
    [ToolboxBitmap(typeof(FrankoExcelExport))]  
    public class FnExcelExport : Infragistics.WebUI.UltraWebGrid.ExcelExport.UltraWebGridExcelExporter
    {

        #region        

        string[] m_Title;
        int m_Width = 0;
        Boolean bHeaderInitChk = true;
        int m_LastRow = 0;
        #endregion

        #region  - Step First
        public void _FnInitExcel(UltraWebGrid FnGrid, string FnFileName, string[] FnTiltle)
        {
            try
            {
                if (FnGrid.Rows.Capacity != 0)
                {
                    DownloadName = FnFileName;
                    m_Title = FnTiltle;
                    m_LastRow = FnGrid.Rows.Count;
                    this.ExcelStartRow = FnTiltle.Length + 2;
                    this.BeginExport += new Infragistics.WebUI.UltraWebGrid.ExcelExport.BeginExportEventHandler(FnExcelExport_BeginExport);
                    this.InitializeRow += new Infragistics.WebUI.UltraWebGrid.ExcelExport.InitializeRowEventHandler(FnExcelExport_InitializeRow);
                    this.Export(FnGrid);
                }
                else
                {
                   //_FnAlert("NO Data.");
                }
            }
            catch (Exception ex)
            {

            }
        }

        #endregion

        #region - Step Third
       void FnExcelExport_InitializeRow(object sender, Infragistics.WebUI.UltraWebGrid.ExcelExport.ExcelExportInitializeRowEventArgs e)
        {
            try
            {
               // e.Row.Style.Font.Name = "Tahoma";
                e.Row.Style.Font.Size = 8;

                if (bHeaderInitChk)
                {
                    for (int i = 0; i < m_Width + 1; i++)
                    {
                        //e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.BottomBorderStyle = Infragistics.Excel.CellBorderLineStyle.Thin;
                        //e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.TopBorderStyle = Infragistics.Excel.CellBorderLineStyle.Thin;
                        //e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.RightBorderStyle = Infragistics.Excel.CellBorderLineStyle.Thin;
                        e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.FillPatternForegroundColor = Color.FromArgb(140, 204, 222);
                        e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.Font.Name = "Tahoma";
                        e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.Font.Bold = Infragistics.Excel.ExcelDefaultableBoolean.True;
                        e.CurrentWorksheet.Rows[m_Title.Length + 1].Cells[i].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
                    }
                    bHeaderInitChk = false;
                }

                if (m_LastRow - 1 == e.Row.Index)
                {
                    for (int i = 0; i < m_Width; i++)
                    {
                        e.CurrentWorksheet.Columns[i].Width += 500;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        } 
        #endregion

        #region - Step Two
        void FnExcelExport_BeginExport(object sender, Infragistics.WebUI.UltraWebGrid.ExcelExport.BeginExportEventArgs e)
        {
            try
            {
                m_Width = e.Rows[0].Cells.Count - 1;
                for (int i = 0; i < m_Title.Length; i++)
                {
                    e.CurrentWorksheet.Rows[i].Cells[0].Value = m_Title[i];
                    e.CurrentWorksheet.Rows[i].Cells[0].CellFormat.Font.Color = Color.FromArgb(0, 51, 102);
                    e.CurrentWorksheet.Rows[i].Cells[0].CellFormat.Font.Bold = Infragistics.Excel.ExcelDefaultableBoolean.True;
                    e.CurrentWorksheet.Rows[i].Cells[0].CellFormat.Font.Name = "Tahoma";
                    e.CurrentWorksheet.MergedCellsRegions.Add(i, 0, i, m_Width);
                    e.CurrentWorksheet.Rows[i].Cells[0].CellFormat.FillPatternForegroundColor = Color.FromArgb(204, 255, 204);
                    e.CurrentWorksheet.Rows[i].Cells[0].CellFormat.RightBorderStyle = Infragistics.Excel.CellBorderLineStyle.Thin;
                }
                e.CurrentWorksheet.Rows[m_Title.Length - 1].Cells[0].CellFormat.BottomBorderStyle = Infragistics.Excel.CellBorderLineStyle.Thin;
                e.CurrentWorksheet.Name = m_Title[0];
                e.CurrentWorksheet.DisplayOptions.TabColor = Color.FromArgb(151, 0, 0);

                e.CurrentWorksheet.Columns[1].CellFormat.FormatString = "####.##";
            }
            catch (Exception ex)
            {

            }
        } 
        #endregion

        #region 
        protected void _FnAlert(string strErrMsg)
        {
            
         Page.Response.Write("<script language='javascript'>alert('" + strErrMsg + "');</script>");
       } 
       #endregion
        
    }
}

* how to use
Make a useControl using above Class.
then make a test page then use this code..
protected void Button1_Click(object sender, EventArgs e)
{
        string[] FnTitle = { "test", "test1", "test2", "test3","test4" };
        FrankoExcelExport1._FnInitExcel(UltraWebGrid1, "FnExcel.xls", FnTitle);
// ==>
}


Good luck!!

Sorry for my Poor English!!
0
JS
JS answered on Jun 12, 2008 12:44 AM

It works!!!  Thank you so much.

So this is another quick Question. I can't find "Serialize" function, so I use "Save" function.

workbook.Serialize( "C:\\Data.xls" );

workbook.Save( "C:\\Data.xls" );

What is the difference between "Serialize" and "Save"

Thanks in advance.