11 Jul

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 Jul

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();
            }
        }
13 Jun

Zip and Unzip Files Programmatically in C#

There are many zip library which used to be popular. I will show you two of them.(7Zip and .NET ZipArchive)
7-Zip is a open source file archiver with a high compression ratio. Most of the source code is under the GNU LGPL license.
For more information:
http://www.7-zip.org/

You can use 7-zip library for zip and unzip to the files as follow:


            //Firstly, you should set 7z.dll path.
            SevenZipExtractor.SetLibraryPath(@"C:\7z.dll");

            //Test Code:
            string[] filePaths = new string[]
            {
                @"C:\Folder1\file1.jpg",
                @"C:\Folder1\file2.txt",
                @"C:\Folder1\file1.png",
            };
            ZipFiles(filePaths, @"C:\zippedFile2.zip");

            ZipFolder(@"C:\Folder1", @"C:\zippedFile1.zip", "password");
            Unzip(@"C:\zippedFile1.zip", @"C:\Folder2", "password");

Sample methods(using 7-Zip):


        public void ZipFiles(string[] filePaths, string outputFilePath, string password = null)
        {
            var tmp = new SevenZipCompressor();
            tmp.ScanOnlyWritable = true;
            tmp.CompressFilesEncrypted(outputFilePath, password, filePaths);
        }

        public void ZipFolder(string folderPath, string outputFilePath, string password = null)
        {
            var tmp = new SevenZipCompressor();
            tmp.ScanOnlyWritable = true;
            tmp.CompressDirectory(folderPath, outputFilePath, password);
        }

        public void Unzip(string zippedFilePath, string outputFolderPath, string password = null)
        {
            SevenZipExtractor tmp = null;
            if (!string.IsNullOrEmpty(password))
                tmp = new SevenZipExtractor(zippedFilePath, password);
            else
                tmp = new SevenZipExtractor(zippedFilePath);

            tmp.ExtractArchive(outputFolderPath);
        }

In .NET framework 4.5, System.IO.Compression namespace get some new classes that allow you to work with zip files programmatically.
Sample code:


            //Zip files
            ZipArchive zip = ZipFile.Open(filePath, ZipArchiveMode.Create);
            foreach (string file in filePaths)
            {
                zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
            }
            zip.Dispose();

            //Unzip
            ZipFile.CreateFromDirectory(folderPath, zippedFilePath);
            ZipFile.ExtractToDirectory(zippedFilePath, outputFolderPath);
13 Jun

C# IStream Implementation and IStream Wrapper Class Example

See example/usage below for istream implementation and wrapper. Attachment has two c# files includes MamagedIStream and IStreamWrapper class.

Usage IStreamWrapper and ManagedIStream:

            
            //IStreamWrapper  usage
            IStream iStream = null; // set a new instance
            IStreamWrapper sw = new IStreamWrapper(iStream);
            byte[] data = new byte[1];
            sw.Read(data, 0, 1);


            //ManagedIStream usage
            Stream ms = null; // set a new stream instance
            ManagedIStream managedIStream = new ManagedIStream(ms); // ManagedIStream class implements IStream interface.

Download Files:
CSharpExamples-Managed IStream and IStreamWrapper.RAR