12 Tem

Select A File With OpenFileDialog Using C#

This example shows how to select a file using the OpenFileDialog dialog box. OpenFileDialog allows users to select files. It is found in System.Windows.Forms namespace and it displays the standard Windows dialog box.

Usage:

            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Select A File";
            openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" + 
                                "Image Files (*.png;*.jpg)|*.png;*.jpg" + "|" +
                                "All Files (*.*)|*.*";
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                string file = openDialog.FileName;
            }
11 Tem

C# Write Data To Excel File

This example shows how to write data to an excel file using C#. To achive this, firstly, we need to add a reference to the dynamic link library for Excel which is called Microsoft.Office.Interop.Excel.dll. Firstly, go to your solution explorer and click on add a reference. Then, add Microsoft.Office.Interop.Excel.dll.

Also you must add the using to the following code.

using Excel = Microsoft.Office.Interop.Excel;

Add Microsoft Office Excel Reference

Usage:

        public void WriteSample()
        {
            Excel.Application excelApp = new Excel.Application();
            if (excelApp != null)
            {
                Excel.Workbook excelWorkbook = excelApp.Workbooks.Add();
                Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.Add();

                excelWorksheet.Cells[1, 1] = "Value1";
                excelWorksheet.Cells[2, 1] = "Value2";
                excelWorksheet.Cells[3, 1] = "Value3";
                excelWorksheet.Cells[4, 1] = "Value4";

                excelApp.ActiveWorkbook.SaveAs(@"C:\abc.xls", Excel.XlFileFormat.xlWorkbookNormal);

                excelWorkbook.Close();
                excelApp.Quit();

                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
11 Tem

C# Read Data From Excel File

This example shows how to read data from excel file using C#. To achive this, firstly, we need to add a reference to the dynamic link library for Excel which is called Microsoft.Office.Interop.Excel.dll. Firstly, go to your solution explorer and click on add a reference. Then, add Microsoft.Office.Interop.Excel.dll.

Also you must add the using to the following code.

using Excel = Microsoft.Office.Interop.Excel;

Add Microsoft Office Excel Reference

Usage:

        public void ReadSample()
        {
            Excel.Application excelApp = new Excel.Application();
            if (excelApp != null)
            {
                Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\test.xls", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets[1];

                Excel.Range excelRange = excelWorksheet.UsedRange;
                int rowCount = excelRange.Rows.Count;
                int colCount = excelRange.Columns.Count;

                for (int i = 1; i <= rowCount; i++)
                {
                    for (int j = 1; j <= colCount; j++)
                    {
                        Excel.Range range = (excelWorksheet.Cells[i, 1] as Excel.Range);
                        string cellValue = range.Value.ToString();

                        //do anything
                    }
                }

                excelWorkbook.Close();
                excelApp.Quit();
            }
        }
03 Tem

All Useful Sorting Algorithms

A sorting algorithm is an algorithm that puts elements of a list in a certain order. Following sorting algorithms are used by most developer/student.

03 Tem

C# Merge Sort Algorithm Implementation

Merge sort is a comparison-based sorting algorithm. It is based on divide-and-conquer paradigm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.

For more information about Merge Sort Algorithm:
http://en.wikipedia.org/wiki/Merge_sort

Implementation and Usage:

            int[] arr = new int[10]
            {
                1, 5, 4, 11, 20, 8, 2, 98, 90, 16
            };

            MergeSort(arr, 0, arr.Length - 1);
            Console.WriteLine("Sorted Values:");
            for (int i = 0; i < arr.Length; i++)
                Console.WriteLine(arr[i]);

            //Output:
            //Sorted Values:
            //1
            //2
            //4
            //5
            //8
            //11
            //16
            //20
            //90
            //98
        private void Merge(int[] input, int left, int middle, int right)
        {
            int[] leftArray = new int[middle - left + 1];
            int[] rightArray = new int[right - middle];

            Array.Copy(input, left, leftArray, 0, middle - left + 1);
            Array.Copy(input, middle + 1, rightArray, 0, right - middle);

            int i = 0;
            int j = 0;
            for (int k = left; k < right + 1; k++)
            {
                if (i == leftArray.Length)
                {
                    input[k] = rightArray[j];
                    j++;
                }
                else if (j == rightArray.Length)
                {
                    input[k] = leftArray[i];
                    i++;
                }
                else if (leftArray[i] <= rightArray[j])
                {
                    input[k] = leftArray[i];
                    i++;
                }
                else
                {
                    input[k] = rightArray[j];
                    j++;
                }
            }
        }

        private void MergeSort(int[] input, int left, int right)
        {
            if (left < right)
            {
                int middle = (left + right) / 2;

                MergeSort(input, left, middle);
                MergeSort(input, middle + 1, right);

                Merge(input, left, middle, right);
            }
        }    

See Also: