Free Support Forum - aspose.com

Error saving excel to browser

I have the following code snippet - when running I get the following error:

the error is in the excel.Save call - any ideas?

Thanks,

travis

System.Exception: Unknown error at Aspose.Excel.Record.ஒ.ஔ(Worksheet ֤, Int32 ߙ, Int32 ֥, Int32 க, Int32 ஖, Int32 ஗) at Aspose.Excel.Worksheet.֙(ڷ Ԏ) at Aspose.Excel.Worksheets.֕(ڷ Ԏ) at Aspose.Excel.Worksheets.֚(FileFormatType ֘) at Aspose.Excel.Worksheets.֙(Stream Ԏ, FileFormatType ֘) at Aspose.Excel.Worksheets.֙(String ֜, SaveType ֝, FileFormatType ֘, HttpResponse ֞) at Aspose.Excel.Excel.Save(String fileName, SaveType saveType, FileFormatType fileFormatType, HttpResponse response) at Vci.Mosaic.Sunesis.Reports.AsposeExcel.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\sunesismosaic\sunesis\reports\asposeexcel.aspx.cs:line 115

========

string xml = Request["MetaXml"];

XmlDocument doc = new XmlDocument();

doc.LoadXml(xml);

XmlElement root = doc.DocumentElement;

XmlNode data;

data = root.SelectSingleNode("/grid/data");

XmlNodeReader reader = new XmlNodeReader(data);

DataSet ds = new DataSet();

ds.ReadXml(reader);

Aspose.Excel.Excel excel = new Aspose.Excel.Excel();

//hard code Coumpound Images

int rowCount = 1;

Pictures pics = excel.Worksheets[0].Pictures;

excel.Worksheets[0].Cells.SetColumnWidth(0,45);

//excel.Worksheets[0].Cells.SetColumnWidth(10,90);

//excel.Worksheets[0].Cells.SetColumnWidth(11,90);

string url = null;

foreach ( DataRow row in ds.Tables["row"].Rows)

{

excel.Worksheets[0].Cells.SetRowHeight(rowCount,350);

url = "http://localhost/SunesisMosaic/image.aspx?Guid=" + row["PlotImage_AssayAURORAAHTRF"];

System.Net.HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

myHttpWebRequest.CookieContainer = new CookieContainer();

myHttpWebRequest.CookieContainer.SetCookies(new System.Uri(url),this.Request.Headers["Cookie"]);

// Sends the HttpWebRequest and waits for the response.

System.Net.HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Gets the stream associated with the response.

Stream receiveStream = myHttpWebResponse.GetResponseStream();

pics.Add(rowCount,10,receiveStream);

myHttpWebResponse.Close();

rowCount++;

}

excel.Save("AuroraAB.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, this.Response);

This problem may be caused by receiveStream. Please read image data into memory streams and close them after calling Excel.Save method.

that didn't seem to make a difference - i did the following

Stream receiveStream = myHttpWebResponse.GetResponseStream();

System.IO.MemoryStream ms = new MemoryStream();

Vci.Core.BinaryFileIO.ReadWriteStream(receiveStream,ms);

receiveStream.Close();

pics.Add(rowCount,10,ms);

What happens if you don’t add images? Could you post your code of ReadWriteStream method? Which version of Aspose.Excel are you using?

yes - it works if I don't use images - I was able to add images using FileStream.

as follows...

url = row["CompoundImageFull"].ToString();

string compoundPath = Server.MapPath("~" + url);

System.IO.FileStream compoundStream = new FileStream(compoundPath,System.IO.FileMode.Open);

pics.Add(rowCount,0,compoundStream);

I am using the download version as I am evauating the software.

Thanks for your help,

Travis

======

public static void ReadWriteStream(Stream readStream, Stream writeStream)

{

int Length = 256;

Byte [] buffer = new Byte[Length];

int bytesRead = readStream.Read(buffer,0,Length);

// write the required bytes

while( bytesRead > 0 )

{

writeStream.Write(buffer,0,bytesRead);

bytesRead = readStream.Read(buffer,0,Length);

}

readStream.Close();

writeStream.Close();

}

In ReadWriteStream method, please don't call

writeStream.Close();

If you close the stream, how can I read data from it. Please close all these streams after calling Excel.Save method.

thanks - that fixed it