We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

SaveDataToExcelFile with streams

Dear all,

We need to process a big amount of files, and we are using SaveDataToExcelFile (Aspose.Grid.Web.Data Namespace > WebWorksheets Class > Methods > SaveDataToExcelFile Method ) because we need to use template files.

The problem we have is the disk access, we would like to be able to call this function with Streams, to write files to MemoryStreams or Response.OutputStream and read the template files from MemoryStreams also, is this possible?

Regards,

Nelson Ruivo

Hi,

I think you may try write the file contents to Response streams

e.g..,

// Generates a temporary file name.
string filename = System.IO.Path.GetTempPath() + Session.SessionID + ".xls";
// Saves to the file.
this.GridWeb1.WebWorksheets.SaveToExcelFile(filename);
// Sents the file to browser.
Response.ContentType = "application/vnd.ms-excel";
//Adds header.
Response.AddHeader("content-disposition", "attachment; filename=book1.xls");
// Writes file content to the response stream.
Response.WriteFile(filename);
Response.End();

Or you may try to refer the following code for your task :

e.g..,

// Opens a new file stream.
FileStream fs = new FileStream("d:\\bookx.xls", FileMode.Open);
try
{
// Imports data from this stream.
GridWeb1.WebWorksheets.ImportExcelFile(fs);
GridWeb1.RefreshControl();

// Writes data to a memory stream.
MemoryStream mstream = new MemoryStream();
GridWeb1.WebWorksheets.SaveToExcelFile(mstream);
mstream.Flush();

// Writes the content of the memory stream to a file.
FileStream fout = new FileStream("d:\\out.xls", FileMode.Create);
fout.Write(mstream.GetBuffer(), 0, (int)mstream.Length);
fout.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
fs.Close();
}

Thank you.

Dear Amjad,

I think you missed my point, I can't use SaveToExcelFile method, I need to use SaveDataToExcelFile which doesn't support streams.

Regards,

Nelson Ruivo

Hi,

Currently that is not supported. We will support this feature at a future time. For now, I think you may use temp files to handle that.

Thank you for considering Aspose.

Hi,

I’ve tried both codes you supplied, but neither work… I’m getting a NullReference exception on
GridWeb1.WebWorksheets.SaveToExcelFile(filename)

and in
GridWeb1.WebWorksheets.SaveToExcelFile(mstream).

So the problem seems to be in the SaveToExcelFile method.
Do you have any idea?
I’m using version 2.0.0.2001.

Many thanks,
António Carvalho
Elo, Sistemas de informação, lda

Hi,

Where you place the codes. Please put any of the codes into GridWeb1_SaveCommand event handler and click the save icon (button) of the GridWeb.

Thank you.

Hi,

I was trying to use a custom command; now, I’m trying to use the save button, but the control is not displayed…

Here is my configuration:

GridWeb1.ShowTabBar = False
GridWeb1.ShowHeaderBar = False
GridWeb1.ShowBottomBar = True
GridWeb1.ShowSaveButton = True
GridWeb1.ShowSubmitButton = False
GridWeb1.ShowUndoButton = False
GridWeb1.ShowCommandBarAtTop = True

GridWeb1.XhtmlMode = False
GridWeb1.EnableSmartNavigation = False
GridWeb1.WebWorksheets.Clear()
GridWeb1.WebWorksheets.Add(“New one”)
GridWeb1.EnableDoubleClickEvent = True
GridWeb1.EditMode = False
GridWeb1.ForceValidation = False
GridWeb1.EnableClientColumnOperations = False
GridWeb1.EnableClientMergeOperations = False
GridWeb1.EnableClientRowOperations = False
GridWeb1.EnableClientResizeColumnRow = False
GridWeb1.EnableStyleDialogbox = False

GridWeb1.SessionMode = SessionMode.Session
GridWeb1.DisplayCellTip = False
GridWeb1.EnableAJAX = False

If GridWeb1.CustomCommandButtons.Count = 0 Then
’ add my custom buttons…
End if

Do you have any idea?

Many Thanks,
Antonio Carvalho

Hi Antonio Carvalho,

Please try to change only one line in your code i.e..,

GridWeb1.ShowTabBar = False
GridWeb1.ShowHeaderBar = False
GridWeb1.ShowBottomBar = True
GridWeb1.ShowSaveButton = True
GridWeb1.ShowSubmitButton = False
GridWeb1.ShowUndoButton = False
GridWeb1.ShowCommandBarAtTop = True

GridWeb1.XhtmlMode = False
GridWeb1.EnableSmartNavigation = False
GridWeb1.WebWorksheets.Clear()
GridWeb1.WebWorksheets.Add("New one")
GridWeb1.EnableDoubleClickEvent = True

GridWeb1.EditMode = True

GridWeb1.ForceValidation = False
GridWeb1.EnableClientColumnOperations = False
GridWeb1.EnableClientMergeOperations = False
GridWeb1.EnableClientRowOperations = False
GridWeb1.EnableClientResizeColumnRow = False
GridWeb1.EnableStyleDialogbox = False

GridWeb1.SessionMode = SessionMode.Session
GridWeb1.DisplayCellTip = False
GridWeb1.EnableAJAX = False

If GridWeb1.CustomCommandButtons.Count = 0 Then
' add my custom buttons...
End if

Thank you.


Hi Amjad Sahi,

First let me apologize for my very late reply… this was a nice to have feature, so I din’t give it much priority.

Any way, I would like to know if there is another way to do it, as I can’t use the save command, since it’s a kind of strange for the user to set the grid in edit mode (normally it’s not!) and then click the save button.
Basically I would like to have an command button, and the grid has the EditMode=false and when the user clicks the button, the application would save the excel.

Do you have a suggestion on how can I do it?

Thanks for your time.

Regards,
Ant�nio Carvalho

Hi Carvalho,

Well, I think you may try to add Grid's custom command button (you may use GridWeb's custom command button editor at design time) and put the Save (icon) code there with GridWeb's EditMode false. Alternatively, you may also use an ASP.NET button and put the saving code there. But be sure to put the following line of code in Page_Load event

e.g..,

if (!IsPostBack)

{

// Adds an onclick event hook for the server button.

Button1.Attributes["onclick"] = "GridWeb1.updateData(); return GridWeb1.validateAll();";

}

Thank you.