пятница, 22 августа 2008 г.

CarlosAg.ExcelXmlWriter: freeze top row










For freeze top row in Microsoft Excel 2007 worksheet using CarlosAg.ExcelXmlWriter
from CarlosAg Excel Xml Writer Library use following code:

Workbook myWorkBook = new Workbook();
Worksheet myWorksheet = myWorkBook.Worksheets.Add("Data");
myWorksheet.Options.Selected = true;
myWorksheet.Options.FreezePanes = true;
myWorksheet.Options.SplitHorizontal = 1;
myWorksheet.Options.TopRowBottomPane = 1;
myWorksheet.Options.ActivePane = 2;

Also you can use CarlosAg.ExcelXmlWriter.Generator.exe from CarlosAg Excel Xml Writer Library for get c# code for Excel *.xml files saved in XML Spreadsheet format.

вторник, 19 августа 2008 г.

Microsoft.Office.Interop.Excel reference isn't resolved

For .net developers which works with MS Office interop, maybe following information would seem useful. Did you happen to face with unresolved Office Interop references?
Each time as I saw this strange phenomena I would need some magic to resolve the problem.
Mostly the reason hides in conflicting of different asseblies references. In this case usually it's better to remove all unresolved references and add each new assembly refererence step-by-step checking conflict absence.

понедельник, 18 августа 2008 г.

Types casting safety in .NET

Not long ago I clashed with following code:

namespace Test
{
class A
{
public object mVal = new B();
};

class B
{
public string mVal = "ValueB";
};

class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
a.mVal = a.mVal + b.mVal;
}
}
}

At first I wondered how it was compiled.
At second do you mean what value will assigned to
a.mVal? The answer is: "Test.BValueB". I'll trying to explain what happened in this code. b.mVal type is 'string' but a.mVal type is 'object', and types casting problem resolved by C# through call a.mVal.ToString(). I.e we have expression:

a.mVal
= a.mVal.ToString() + b.mVal


In other words System.Object casts to System.String implicitly and result string reference assigned to System.Object again. Real trouble is C# compiler doesn't write any errors or warnings about danger of types casting safety.
Open question is: "Where is types casting safety in .NET?"

How to get PowerShell current runspace from C#

It's possible you faced with situation when you need access to PowerShell current runspace in C# code. For example you need get value of script variable which exists only in runspace of hosting application (i.e. usually poweshell.exe). Of course you can use RunspaceFactory.CreateRunspace(), but the trouble is that new runspace doesn't contain any script variables and added earlier snap-ins.

Here is example how to do this.


Runspace theRunSpace = System.Management.Automation.Runspaces.Runspace.DefaultRunspace;

if (theRunSpace.RunspaceStateInfo.State == RunspaceState.Opened)
{
string theCommand = "$MyScriptVariable";
using (Pipeline thePipeline = theRunSpace.CreateNestedPipeline(theCommand, true))
{
Collection theRetVal = thePipeline.Invoke();
}
}


Note that in shown code CreateNestedPipeline() called instead CreatePipeline(), because at the moment of call CreateNestedPipeline() we stand in executing pipeline already.

Hello World!

Hello! This is my first topic.
Here is I will post some information about programming (predominantly .NET) and related areas: performance tools, coding tools, useful assistants etc.