Opening excel sheet without saving to disk

Hi All,

Can anyone help me in opening excel sheet in my application without saving it anywhere in the disk.I have an windows application right now i am able to open a excel sheet but i am saving it to disk every time using

Workbook.Save("filepath+name",FileFormatType)

I want to only open the excel sheet and dont want to save it anywhere on disk?

Can anyoe help me regarding this

Thank You,

Bindu.

Hi,

Yes, you may open excel workbook from streams and save the excel workbook to streams. Workbook's both Open() and Save() methods provide some overloaded versions for the task:

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/aspose.cells.workbook.open_overloads.html

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/aspose.cells.workbook.save_overloads.html

Thank you.

Can you please tell me how to d it.

As of now i have done something like this

I have opened search1.xls template edited it and now want that edited sheet to be opened without saving anywhere in the disk :

private void button1_Click(object sender, EventArgs e)

{

Workbook wb = new Workbook();

FileStream fstream = new FileStream("C:\\search1.xls", FileMode.Open);

//Opening the Excel file through the file stream

wb.Open(fstream);

Worksheet worksheet = wb.Worksheets[0];

worksheet.Cells["B5"].PutValue("a");

worksheet.Cells["B6"].PutValue("b");

worksheet.Cells["B7"].PutValue("c");

worksheet.Cells["A11"].PutValue("d");

worksheet.Cells["A12"].PutValue("e");

//Setting the name of the newly added worksheet

worksheet.Name = "My Worksheet";

//Saving the Excel file

fstream.Close();

//wb.Save("C:\\search2.xls", FileFormatType.Excel2003);

**********FileStream fs = new FileStream("c:\\search3.xls",FileMode.Open);

//Stream s = new stream

****wb.Save(fs, FileFormatType.Excel2003);

*********wb.Open(fs,FileFormat.Excel2003);

//System.Diagnostics.Process.Start("c:\\search3.xls");

string value = worksheet.Cells["B5"].Value.ToString();

MessageBox.Show(value);

}

I think i am stil storing it on the c drive .I dont want it to store anywhere.

I want to know how to open the edited xl sheet on click of button without saving it to disk .I am not sure how to use filestream class can you help me regarding this.

Can you be more precise as i am new to this

Thank You,

Bindu.

Hi Bindu,

Well, you may use MemoryStream class. Kindly change your code to:

Workbook wb = new Workbook();

FileStream fstream = new FileStream("C:\\search1.xls", FileMode.Open);

//Opening the Excel file through the file stream

wb.Open(fstream);

Worksheet worksheet = wb.Worksheets[0];

worksheet.Cells["B5"].PutValue("a");

worksheet.Cells["B6"].PutValue("b");

worksheet.Cells["B7"].PutValue("c");

worksheet.Cells["A11"].PutValue("d");

worksheet.Cells["A12"].PutValue("e");

//Setting the name of the newly added worksheet

worksheet.Name = "My Worksheet";

//Saving the Excel file

fstream.Close();

//wb.Save("C:\\search2.xls", FileFormatType.Excel2003);

MemoryStream fs = new MemoryStream();

//Stream s = new stream

wb.Save(fs, FileFormatType.Excel2003);

wb.Open(fs,FileFormatType.Excel2003);

//System.Diagnostics.Process.Start("c:\\search3.xls");

string value = worksheet.Cells["B5"].Value.ToString();

MessageBox.Show(value);

Thank you.

Hi ,

Thank you for the reply.Code is working fine but when i am clicking on the button(i want the new excel sheet with changes in it to be opened(not the template) ) nothing is there.I am not able to see any excel sheet coming up.

Can yo please look into this.

Thank You,

Bindu.

Hi,

If you are are using an Asp.NET project, you may add a few lines for the task... e.g.,

MemoryStream stream = new MemoryStream();

wb.Save(stream, FileFormatType.Default);

Response.ContentType = "application/vnd.ms-excel";

Response.AddHeader("content-disposition", "attachment; filename=newsearch1.xls");

Response.BinaryWrite(stream.ToArray());

Response.End();

OR

wb.Save("newsearch1.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Response);

For you current winform code, you can Save As your template file as follows:

wb.Open("C:\\newsearch1.xls",FileFormatType.Excel2003);

Thank you.

Hi ,

Thank you for your reply.But ,right now i am working in winforms application and pne of my requirement is i have so many excel sheets coming up.They are opened on the click of button.If i use wb.save("name",fileformat) i am supposed to save on my disk but my requirement is not to save them it is only to view them once the user wants to save then he does that .Is there any way in aspose to do this without saving it.

Thank You,

Bindu.[

Hi,

Well, I think you do need to save the file (either temporarily) then you can view it using System.Diagnostics.Process.Start API. And I think you can delete it from the disk using System.IO.File.Delete() API.

Thank you.

Hi,

Thank you .Suppose i have deleted the file on form close event ,but if the user is already viewing the file it is open in the application then when he closes the form or application then this gives an exception like the file is being used by another process ,Is there a way that we can avoid this and even the pdf or xl sheet is open can we close it.Or forcibly can we close the sheets and delete them.

Is there any sample code for doing the above said things in lotus script?

Thanks

Hi,

Well, I am not well aware of lotus script but it should be similar to vbscript as it syntax seems to me, here i write the sample code in it, so that you may try to achieve your task accordingly in lotus script.

Sample code:

'Save the document to the stream
Dim stream
set stream = workbook.SaveToStream()
Response.Clear
'
Specify the document type
Response.ContentType = "application/vnd.ms-excel"

'Other options:
'
Response.ContentType = "text/plain"
'Response.ContentType = "text/html"

'
Specify how the document is sent to the browser.
Response.AddHeader "content-disposition","attachment; filename=MyBook.xls"


'Another option could be:
'
Response.AddHeader "content-disposition","inline; filename=MyBook.xls";

'Get data bytes from the stream and send it to the response.
Dim bytes
bytes = stream.ToArray()


Response.BinaryWrite(bytes)
Response.End

Thank you.

Hi ,

Thanks for the sample code. I have seen the similar code in other examples too.The problem I'm facing with this is coz of 'Response' class that you have used.

I'm not able to find this class either in Java or in the method overload list that you have sent. So I'm not able to use the 'Response' class and my code stops in the middle.

Thanks,

Naresh

Hi Naresh,

Well, Response is not Aspose.Cells API rather an ASP built in object. In my code, I have used Vbscript code in ASP. In JSP / Servlet java, the similar class is there in javax.servlet.http.HttpServletResponse. So, you may study them for your need.

Thank you.