Monday, December 24, 2012

OpenXMLSDK: Pros and Cons

My task was to write Excel in a bulk. So here is my analysis:

Pros:
- Open XML SDK is an great gift from Microsoft because you do not have to be dependent on installing Microsoft Office. When reading/generating an Excel file through other methods, you need to have installed on the server machine the office runitme dlls and in some cases the office should be installed too. While generating the excel file, always an instance of the Excel.exe is created on the server machine, meaning that if 50 files are getting gnerated 50 Excel.exe instances will be opened on the server machine, which could be stessful for the server performance and also handling the closing of the instances after the generation is finished is another problem. But in case of Open XML SDK, all you need to have is to install Open XML SDK and nothing else. You do not need to have Office on the server machine and no instances are created, so the performance of the server is always in control.
- It is a solid choice if you want to generate template-based Office documents.
- It is completely interoperable, meaning the excel document created in this format can be worked on across any platform.
- The SDK is stable and it is supported by Microsoft.
- It is easy to use.
- It has Open XML SDK productivity tool to generate code.
- LINQ can be used to navigate data of excel file.
- It performs comples operations with just a few lines of code.
- It simplifies the task of manipulation Open XML packages and the underlying Open XML schema elements within a package.
- OOXML documents are essentially zipped XML files and Open XML SDK is a collection of classes that allows you to work with the content of OOXML documents in a strongly-typed way. That is instead of unzipping a file to extract XML, loading that XML into a DOM tree and working with XML elements and attributes directly. It provides classes to do that.

Cons:
- It can not render Office functions. The page numbers of a TOC (Table of Content) or the actual page numbers of your Word document are not rendered until the user refreshes the document. The same is true for Excel calculations, so you can't do this rendering/calculation on the server-side.
- It supports only the documents created in Office 2007 or later(i.e. xlsx, docs etc), which means that you wish to work with documents created in the older versions of the office, it will now work as desired. But the documents that are created in Office 2007 or later and saved in Office 97-2003 format, will still work.
- It is a dead slow on manipulating the records while older methed like COM Interop libraries are too much fast in this case. My system has following specifications:
   Processor: Intel Core i3, 3.30 GHz
   RAM: 4 GB
   System Type: 64 Bit OS
   I had to update 4025 records in an Excel file and this task took 33 minutes on my system and when I used COM Interop libraries, it just took 33 seconds to update that excel file. Extreme difference indeed. Obviously it is wrapper on Office libraries, it saves the data in a stream and it has to manage even internal detail of the document by itself while the many internal details of the document are handled by Interop libraries themselves. But if you are working with small amount of data then it is great option to use but with bulk data, you have to analyze it first then suggest someone to use it as a primary library to Office documents automation. Do not suggest it without analyzing it first.

--------------------------------------------------
COM Interop:

Pros:
- Use native Microsoft libraries
- Fast and reliable


Cons:

- Use native Microsoft libraries because Office should be installed otherwise they can not be used. In other words Microsoft Office is the dependency to use it.
- Dependency/Version matching issues
- Concurrency/data integrity issues for web use when reading
- Scaling issues for web use (different from concurrency): need to create many instances of heavy Excel app on the server.

Wednesday, December 5, 2012

Overriding ShowDialog() in C#

Overriding ShowDialog() is not possible in WinForms because there is no overridable method declared for it in Form Class. But there is a way there is will:


 public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        public DialogResult ShowDialog(string mes)
        {
            this.textBox1.Text = mes;
            return base.ShowDialog();
        }
        public DialogResult ShowDialog()
        {
           // Your Code
            return base.ShowDialog();
        }
    }

Tuesday, November 20, 2012

How to play a media file in a loop in Windows media player in .net/C#


We can achieve this through:                                                                    // Set the PlayCount property to a big number and normally before the countdown finishes user   // will interact with system definitly                                                          axWindowsMediaPlayer1.settings.playCount = 200000000;                                           // Set the Mode Loop to true                                                                    axWindowsMediaPlayer1.settings.setMode("Loop", true);                                           // in PlayStateChange event, when it is observed that media player state is stopped then play   // is again                                                                                      axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axWindowsMediaPlayer1_PlayStateChange);
void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
            {         
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

how to check the state of media in window media player in .Net/C#


   axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axWindowsMediaPlayer1_PlayStateChange);

  void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                 // Your code
            }
        }

Usage of Windows media player in .net


axWindowsMediaPlayer1 is default name of windows media player control name.
     axWindowsMediaPlayer1.URL = @"C:\test.wmv";

how to add windows media player in C#.net


On toolbox, right click and choose Items....

From COM Components select Windows Media Player
 You will see that Windows media player icon at the right side of left toolbox and as you will place that icon on the form windows media player will added on teh form and you will the two references in the right side references area.


Friday, November 16, 2012

How to remove a blank row at the end of WPF datagrid


  <DataGrid Name="dataGrid1" CanUserAddRows="False">

WPF - How to fill the datagrid with its propotionate columns

Width="0.25*" in this way columns will be proportionately filled in the WPF datagrid
all the columns will have 25% of the area of the datagrid.
<DataGrid Name="dataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding Values}">
    <DataGrid.Columns>
         <DataGridTextColumn  Header="Value1" Binding="{Binding Value1, Width="0.25*"/>
         <DataGridTextColumn  Header="Value2" Binding="{Binding Value2, Width="0.25*"/>
         <DataGridTextColumn  Header="Value3" Binding="{Binding Value3, Width="0.25*"/>
         <DataGridTextColumn  Header="Value4" Binding="{Binding Value4, Width="0.25*"/>
    </DataGrid.Columns>           
 </DataGrid>

WPF - Inheriting a style



In the following style, a TextBlock's style in created with blinking animation.
// Here is the base style for TextBlock
        <Style x:Key="CellBaseStyle"  TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="Transparent"/> 
            <Setter Property="Padding" Value="0,50,0,0"></Setter>
        </Style>

// Here you will use BasedOn property of Style to use the base style for TextBlock, properties in base style can be overridden in this style as well just like background property
        <Style TargetType="{x:Type TextBlock}" x:Key="SelectedCellStyle" 
               BasedOn="{StaticResource CellBaseStyle}">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush>
                        <SolidColorBrush.Color>
                            
                            <Color R="0" G="168" B="0"/>
                        </SolidColorBrush.Color>
                    </SolidColorBrush>
                </Setter.Value>
            </Setter>           
 
            <Setter Property="Foreground" Value="White"/>
            
            <Style.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard HandoffBehavior="Compose">
                        <Storyboard TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                            <ColorAnimation Duration="0:0:0.7" To="DarkGreen"   AutoReverse="True" RepeatBehavior="1" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>

Friday, November 2, 2012

TableLayoutPanel Flickering in WinForms



Now use this tablelayoutpanel instead of default TableLayoutPanel in VS. It will decease the intensity of flickering of controls in tablelayoutpanel.

 public partial class MyTablelayoutPanel : TableLayoutPanel
    {
        public MyTablelayoutPanel()
        {
            InitializeComponent();

// this will reduce flicker
            this.DoubleBuffered = true;

            SetStyle(ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint, true);
                     }

        public NXBTablelayoutPanel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();

// this will reduce flicker
            this.DoubleBuffered = true;
            SetStyle(ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint, true);
        }
    }