how to read from memory stream..

Aug 4, 2011 at 5:50 PM
Edited Aug 4, 2011 at 6:56 PM

hi

how can i read an excel file from a memory stream & then zip it.. i tried the below code but error occured..

if (sfdExcel.ShowDialog() != DialogResult.Cancel)
    {
        string file_save_name = sfdExcel.FileName;
       string file_save_name_pre = file_save_name.Substring(0, file_save_name.Length - 4);
       using (MemoryStream memory = new MemoryStream())
             {
                  hssfworkbook.Write(memory);
                  using (ZipFile zip = new ZipFile())
                     {
                      ZipEntry e = zip.AddEntry("report.xls", memory);
                       zip.AddFile(file_save_name_pre + ".xls");
                        zip.Save(file_save_name_pre + ".zip");
                      }
               }
         MessageBox.Show("Report Saved", MessageBoxButtons.OK);
     }

I think i am not using the package the right way... what should i do ??

Thanks in advance.

 

 

Aug 4, 2011 at 7:24 PM
Edited Aug 4, 2011 at 8:26 PM

i have solved the problem partially.. the below code is working but it is saving the whole directory structure...

if (sfdExcel.ShowDialog() != DialogResult.Cancel)
                            {
           string file_save_name = sfdExcel.FileName;
         string file_save_name_pre = file_save_name.Substring(0, file_save_name.Length - 4);
      using (MemoryStream memory = new MemoryStream())
              {
                  hssfworkbook.Write(memory); 
                   byte[] contents = memory.ToArray();
                   using (ZipFile zip = new ZipFile())
                     {
                       ZipEntry e = zip.AddEntry(file_save_name_pre + ".xls", contents);
                 zip.Save(file_save_name_pre + ".zip");
                          }
                       }
               MessageBox.Show("Report Saved", MessageBoxIcon.Information);
            }
what should i do now??
Aug 4, 2011 at 7:41 PM
Edited Aug 4, 2011 at 8:27 PM

okay.. i got it finally.. :)

 

if (sfdExcel.ShowDialog() != DialogResult.Cancel)
   {
     string file_save_name = sfdExcel.FileName;
     string file_save_name_pre = new FileInfo(file_save_name).Name;
     string file_save_path = file_save_name.Substring(0, file_save_name.Length - 4);
     using (MemoryStream memory = new MemoryStream())
           {
               hssfworkbook.Write(memory);
               byte[] contents = memory.ToArray();
               using (ZipFile zip = new ZipFile())
                     {
             ZipEntry e = zip.AddEntry(file_save_name_pre, contents);
               zip.Save(file_save_path + ".zip");
                      }
            }
     MessageBox.Show("Report Saved",MessageBoxIcon.Information);
   }
Coordinator
Aug 4, 2011 at 8:45 PM

oh, good!  You answered your own question!  Glad you got it.