Showing posts with label Third Party. Show all posts
Showing posts with label Third Party. Show all posts

Thursday, December 09, 2010

Exporting to Excel from Telerik Gridview in Silverlight

Here is the sample code for exporting Telerik Gridview in silverlight.

private void btnExportOrders_Click(object sender, RoutedEventArgs e)
   // Checking whether grid has rows
    if (OrderGrid.Items.Count > 0)
          string extension = "xls";
          string selectedItem = "Excel";
          ExportFormat format = ExportFormat.ExcelML;
          SaveFileDialog dialog = new SaveFileDialog();
          dialog.DefaultExt = extension;
          dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem);
          dialog.FilterIndex = 1;
          if (dialog.ShowDialog() == true)
              using (Stream stream = dialog.OpenFile())
                GridViewExportOptions options = new GridViewExportOptions();
                options.Format = format;
                options.ShowColumnHeaders = true;
                options.Encoding = Encoding.UTF8;
                options.ShowColumnHeaders = true;
               // name of the grid which you want export      
                OrderGrid.Export(stream, options);
         // User friendly Message
          MessageBox.Show("There are no records to export.", "Export Warning!", MessageBoxButton.OK);

This export code will also support different formats like CSV,Text and HTML. All you need to change is the enum type for ExportFormat to above formats and the extension. Its easy and simple. Good luck Winking smile

Friday, August 06, 2010

Utility: 7-Zip Format

7-Zip is the new archive format, providing high compression ratio. 7-Zip is open source software. Most of the source code is under the GNU LGPL license. The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.

Main Features of 7z format:

  • High compression ratio in new 7z format with LZMA compression
  • Supported formats:
    • Packing / unpacking: 7z, ZIP, GZIP, BZIP2 and TAR
    • Unpacking only: ARJ, CAB, CHM, CPIO, DEB, DMG, HFS, ISO, LZH, LZMA, MSI, NSIS, RAR, RPM, UDF, WIM, XAR and Z.
  • For ZIP and GZIP formats, 7-Zip provides a compression ratio that is 2-10 % better than the ratio provided by PKZip and WinZip
  • Strong AES-256 encryption in 7z and ZIP formats
  • Self-extracting capability for 7z format
  • Integration with Windows Shell
  • Powerful File Manager
  • Powerful command line version
  • Plugin for FAR Manager
  • Localizations for 74 languages

I personally like it very much. Its really helpful for me. Hope you all like this. You can Download 7-Zip for Windows.

Friday, July 30, 2010

Ionic Zip Utility : SaveProgress Event

Save() method in Ionic Zip, this method allows the application to explicitly specify the name of the zip file when saving. Use this when creating a new zip file, or when updating a zip archive.

The ZipFile instance is written to storage, typically a zip file in a filesystem, only when the caller calls Save. The Save operation writes the zip content to a temporary file, and then renames the temporary file to the desired name. If necessary, this method will delete a pre-existing file before the rename.

using (ZipFile zip = ZipFile.Read(""))

There is an Save Progress event handler which invoked when a Save() starts, before and after each entry has been written to the archive, when a Save() completes, and during other Save events.
Depending on the particular event, different properties on the SaveProgressEventArgs parameter are set.

The following  EventTypes describes under which this event handler is invoked  with the given EventType.
ZipProgressEventType.Saving_Started Fired when ZipFile.Save() begins.
ZipProgressEventType.Saving_BeforeSaveEntry Fired within ZipFile.Save(), just before writing data for each particular entry.
ZipProgressEventType.Saving_AfterSaveEntry Fired within ZipFile.Save(), just after having finished writing data for each particular entry.
ZipProgressEventType.Saving_Completed Fired when ZipFile.Save() has completed.
ZipProgressEventType.Saving_AfterSaveTempArchive Fired after the temporary file has been created. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_BeforeRenameTempArchive Fired just before renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterRenameTempArchive Fired just after renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterCompileSelfExtractor Fired after a self-extracting archive has finished compiling. This EventType is used only within SaveSelfExtractor().
ZipProgressEventType.Saving_BytesRead Set during the save of a particular entry, to update progress of the Save(). When this EventType is set, the BytesTransferred is the number of bytes that have been read from the source stream. The TotalBytesToTransfer is the number of bytes in the uncompressed file.

In example below to see how it can be used.

static bool justHadByteUpdate= false;
public static void SaveProgress(object sender, SaveProgressEventArgs e)
    if (e.EventType == ZipProgressEventType.Saving_Started)
        Console.WriteLine("Saving: {0}", e.ArchiveName);
    else if (e.EventType == ZipProgressEventType.Saving_Completed)
        justHadByteUpdate= false;
        Console.WriteLine("Done: {0}", e.ArchiveName);
    else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
        if (justHadByteUpdate)
        Console.WriteLine("  Writing: {0} ({1}/{2})",
                          e.CurrentEntry.FileName, e.EntriesSaved, e.EntriesTotal);
        justHadByteUpdate= false;
    else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
        if (justHadByteUpdate)
            Console.SetCursorPosition(0, Console.CursorTop);
         Console.Write("     {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer,
                      e.BytesTransferred / (0.01 * e.TotalBytesToTransfer ));
        justHadByteUpdate= true;
public static ZipUp(string targetZip, string directory)
  using (var zip = new ZipFile()) {
    zip.SaveProgress += SaveProgress;

I have copied this information from Ionic help file for my future reference.