Showing posts with label csharp. Show all posts
Showing posts with label csharp. Show all posts

2013-08-29

C#: If you stuck with an Excel interop

Error message:
System.Runtime.InteropServices.COMException (0x80028018): Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

Solution:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
WB = XL.Workbooks.Open(newfilepath, ...);

Set the CurrentCulture to en-US just before the Workbooks.Open(). This is required because the one and only VBA language supported by Excel is en-US.
And no, the Office MUI does NOT exists (altough it is often referenced on msdn...)

C#: Writing xlsx from C#

http://simpleooxml.codeplex.com/

C#: Crosscontext calls in WPF

public static class DispatcherObjectExtensions{
 public static TRet myInvoke(this T source, Func func)  where T:DispatcherObject {
  return (TRet)source.Dispatcher.Invoke(func);
 }
 public static void myInvoke(this T source, Action act) where T:DispatcherObject {
  source.Dispatcher.Invoke(act);
 }
}

This will put these two extension methods on all DispatcherObject descendants (like System.Windows.Window), and you can call a delegate put in the object's thread/context.

C#: Anonymous delegates asynchronously

this.BeginInvoke(new Action(delegate
{ 
 MessageBox.Show("Asynchronous :D"); 
}));

System.Action is a framework-defined Delegate successor, that does not return anything and doesn't take any arguments. You should use this, instead of fucking around with inheriting from System.Delegate.