This is a migrated thread and some comments may be shown as answers.

Export to Excel from multiple Grid Views

2 Answers 137 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Viki
Top achievements
Rank 1
Viki asked on 30 Dec 2010, 08:11 AM
Hi,

I need to export data to Excel from multiple Grid Views to:
1. multiple Excel files (without using of SaveFileDialog)
Or
2. one Excel file with multiple sheets

How can I do it?

Thanks,
Viki

2 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Vasilev
Telerik team
answered on 03 Jan 2011, 03:40 PM
Hi Viki,

You can export the data from multiple gridviews to different files in the isolated storage. Here is a sample:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    string extension = "xls";
    ExportFormat format = ExportFormat.ExcelML;
    var data = this.clubsGrid.ToExcelML(true, true);
 
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
        using (var isoStream = store.OpenFile(@"export1.xml", FileMode.OpenOrCreate))
        {
            isoStream.Write(StrToByteArray(data), 0, data.Length - 1);
            isoStream.Close();
 
                     
        }
            //Remove isolated storage.
        //store.Remove();
    }          
}

I hope this will get you started.

Best wishes,
Veselin Vasilev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Viki
Top achievements
Rank 1
answered on 06 Feb 2011, 07:52 AM
Hi Veselin,

Thanks for your help.

For the solution I saved the data from all the Grid Views in isolated storage and then zipped it
and saved the zip file localy on my machine by the SaveFileDialog, like this:
(For the Zip method there is need of reference to SharpZipLib.Silverlight4.dll)

private void RenderZip_Click(object sender, RoutedEventArgs e)
        {
            List<string> fileslist = new List<string>();
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                for (int i = 0; i < 100; i++)
                {
                    using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("File" + i.ToString(), FileMode.Create, isf))
                    {
                        using (TextWriter fl = new StreamWriter(isfs))
                        {
                            fl.WriteLine("test" + i.ToString());
                            fileslist.Add("File" + i.ToString());
                        }
 
                    }
                }
                Helper.Zip(fileslist, "test.zip");
 
            }
        }
 
        private void SaveZip_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            byte[] buffer = new byte[100000];
            int size;
            if (fd.ShowDialog().Value)
            {
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fl = new IsolatedStorageFileStream("test.zip", FileMode.Open, isf))
                    {
                        using (Stream sw = fd.OpenFile())
                        {
                            using (BinaryWriter bw = new BinaryWriter(sw))
                            using (BinaryReader br = new BinaryReader(fl))
 
                                do
                                {
                                    size = br.Read(buffer, 0, buffer.Length);
                                    bw.Write(buffer, 0, size);
                                } while (size > 0);
 
                        }
 
                    }
                }
            }
        }
 
 
public static void Zip(List<string> SrcFiles, string DstFile)
        {
 
 
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStreamOut = new IsolatedStorageFileStream(DstFile, FileMode.Create, FileAccess.Write, isf))
                {
 
                    using (ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut))
                    {
                        foreach (var SrcFile in SrcFiles)
                        {
 
                            using (IsolatedStorageFileStream fileStreamIn = new IsolatedStorageFileStream(SrcFile, FileMode.Open, FileAccess.Read, isf))
                            {
 
                                byte[] buffer = new byte[10000];
                                ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(SrcFile));
                                zipOutStream.PutNextEntry(entry);
                                int size;
                                do
                                {
                                    size = fileStreamIn.Read(buffer, 0, buffer.Length);
                                    zipOutStream.Write(buffer, 0, size);
                                } while (size > 0);
 
 
                            }
                        }
                    }
                }
 
            }
        }
Tags
GridView
Asked by
Viki
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Viki
Top achievements
Rank 1
Share this question
or