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

Free Support Forum - aspose.com

Workbook.worksheets.copy throw exception

Hi there,

we I use the following code to copy one of my worksheet I got exception "object no reference" I have attached the excel file. This is cirtical to our client. Thanks for your help.

Tony

public bool GetIndexTextReader(Stream inputDataStream, out TextReader indexTextReader)
{
if (inputDataStream != null && inputDataStream.Length > 0)
{
StreamReader reader = null;
MemoryStream ms = null;
try
{
AsposeLicenseHelper.VerifyLicense();

Workbook workbook = new Workbook(inputDataStream);
ms = new MemoryStream();
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
if (workbook.Worksheets[i].IsVisible)
{
Workbook wbk = new Workbook();
wbk.Worksheets[0].Copy(workbook.Worksheets[i], );
wbk.Save(ms, SaveFormat.CSV);
}
}


reader = new StreamReader(ms, Encoding.Default);
reader.BaseStream.Seek(0, SeekOrigin.Begin);
indexTextReader = reader;

return true;
}
catch
{
if (ms != null)
ms.Close();
if (reader != null)
reader.Close();
}
}

indexTextReader = null;
return false;
}
}


Hi,


Thanks for the template file and sample code.

I have tested your scenario/ case a bit using the following sample code, it works fine. I am using our latest version/fix: Aspose.Cells for .NET v8.1.2.1, please try it.
e.g
Sample code:

string filePath = “e:\test2\Book2.xlsx”;
FileStream stream = new FileStream(filePath, FileMode.Open);
Workbook workbook = new Workbook(stream);
MemoryStream ms = new MemoryStream();
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
if (workbook.Worksheets[i].IsVisible)
{
Workbook wbk = new Workbook();
wbk.Worksheets[0].Copy(workbook.Worksheets[i]);
wbk.Save(ms, SaveFormat.CSV);
}
}


StreamReader reader = new StreamReader(ms, Encoding.Default);
reader.BaseStream.Seek(0, SeekOrigin.Begin);
TextReader indexTextReader = reader;

Thank you.



Hi ,


I have updated my aspose.cell to latest. I still experience the same issue. I have attached a different excel file. Please discard the excel file after this case close. Thanks,

Hi,


Thanks for template file.

I have tested your scenario/ case using my code with your new file, still I could not find the issue. You may also try my code segment on your end if it works fine. I am not sure how do you get the Excel file from/to streams. We appreciate if you could create a separate console application (runnable) with v8.1.2.1, zip the project and post it here to reproduce the issue on our end, we will check it soon.

Thank you.