Tuesday, June 23, 2009

VSTO Excel : GetRange

        /// <summary>
        /// Return a range starting from cell C
        /// The range can contain empty cell
        /// C - - - - - -
        ///  - -   - - - -
        ///  - - - - - - -
        ///  - -   - -          
        ///  </summary>
        /// <param name="ws"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public Excel.Range GetRightDownRegion(Excel.Worksheet ws, Excel.Range start)
        {
            Excel.Range curCell = start;
            Excel.Range endCell = start;
                  
            int lastCol;
            int offsetCol;
            int j = 0;

            while (curCell.Value2 != null)
            {
                curCell = start.get_Offset(0, j);
                if (curCell.Value2 != null)
                {
                    endCell = curCell;
                }
                j++;
            }
            
            lastCol = endCell.Column;

            int i = 0;
            curCell = start;
            endCell = start;

            while (curCell.Value2 != null)
            {
                curCell = start.get_Offset(i, 0);
                if (curCell.Value2 != null)
                {
                    endCell = curCell;
                }
                i++;
            }
            
            offsetCol = lastCol - endCell.Column;

            endCell = endCell.get_Offset(0, offsetCol);
                                    
            return ws.get_Range(start, endCell);            
        }
    }

Thursday, June 11, 2009

VSTO : ListObjects

VSTO is not obvious, here is to name columns

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ExcelWorkbook7
{
public partial class Feuil1
{
NorthwindDataContext ctx = new NorthwindDataContext();

private void Feuil1_Startup(object sender, System.EventArgs e)
{
var cs = from cust in ctx.Customers
select cust;

Microsoft.Office.Tools.Excel.ListObject customerData;
customerData = this.Controls.AddListObject(this.get_Range(this.Cells[1,1],this.Cells[1,2]), "Customers");
DataGridTableStyle s = new DataGridTableStyle();

customerData.TableStyle = "TableStyleLight10";
customerData.ListColumns.get_Item(1).Name = "ID";
customerData.ListColumns.get_Item(2).Name = "Name";
customerData.SetDataBinding(cs,"","CustomerID","CompanyName");

}

private void Feuil1_Shutdown(object sender, System.EventArgs e)
{
}

#region VSTO Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{

this.Startup += new System.EventHandler(this.Feuil1_Startup);
this.Shutdown += new System.EventHandler(this.Feuil1_Shutdown);

}

#endregion

}
}