Attach .txt file as OLE object in excel file using .NET C# API

Hi,

I have written a code to attach the text file in excel file generated using Aspose.cells. Below is the code that I have written. When I click on the object in Excel file after file generation it gives me "cannot start the source application for this project" error in excel file. Now using the same code I am able to open the excel file. Is there a different way to open txt file attached OLE object ?

Workbook oWB = new Workbook();

Worksheet oSheet = oWB.Worksheets[0];

Cells oCells = oSheet.Cells;

string sAttachFilePath = sFileDIR + "text.txt"; //"test.xls";

string sImageFilePath = sFileDIR + "IconImage.JPG";

FileStream fs = File.OpenRead(sImageFilePath);

byte[] imageData = new Byte[fs.Length];

fs.Read(imageData, 0, imageData.Length);

fs.Close();

fs = File.OpenRead(sAttachFilePath);

byte[] objectData = new Byte[fs.Length];

fs.Read(objectData, 0, objectData.Length);

fs.Close();

oSheet.OleObjects.Add(2, 2, 100, 100, imageData);

//oSheet.OleObjects[0].DisplayAsIcon = true;

//oSheet.OleObjects[0].SourceFullName = "Source Full Name";

//oSheet.OleObjects[0].Name = "Name";

//oSheet.OleObjects[0].Text = "Text";

oSheet.OleObjects[0].ObjectData = objectData;

oWB.Save(sFileDIR + "TestAttachment1.xls");

Can you pls help me resolving this problem?

Thanks,

Hetal Sheth

Hi,


Please add a line to your code, I have tested it works fine:

Workbook oWB = new Workbook();

Worksheet oSheet = oWB.Worksheets[0];

Cells oCells = oSheet.Cells;

string sAttachFilePath = sFileDIR + “text.txt”; //“test.xls”;

string sImageFilePath = sFileDIR + “IconImage.JPG”;

FileStream fs = File.OpenRead(sImageFilePath);

byte[] imageData = new Byte[fs.Length];

fs.Read(imageData, 0, imageData.Length);

fs.Close();

fs = File.OpenRead(sAttachFilePath);

byte[] objectData = new Byte[fs.Length];

fs.Read(objectData, 0, objectData.Length);

fs.Close();

oSheet.OleObjects.Add(2, 2, 100, 100, imageData);


oSheet.OleObjects[0].SourceFullName = sAttachFilePath;


oSheet.OleObjects[0].ObjectData = objectData;

oWB.Save(sFileDIR + “TestAttachment1.xls”);

Thanks for the help, it worked. but I have another question here. It does not show like how we insert OLE object in Excel file. here with this code it shows that image and on double click on that it does open the file but its does not show it as Icon and also in Excel when we attach it shows the text below Icon which is not appearing. I tried that by adding below line of code but didn't work.

oSheet.OleObjects.Add(2, 2, 25, 25, imageData);

oSheet.OleObjects[0].SourceFullName = sAttachFilePath;

oSheet.OleObjects[0].DisplayAsIcon = true;

oSheet.OleObjects[0].Text = "Text";

oSheet.OleObjects[0].ObjectData = objectData;

Can you help me on this?

Hi,


Could you paste your complete sample code and attach the output file. Also attach your desired Excel file having your desired embedded .txt file. We will check it soon.

Thank you.

hi,

I got your reply via mail and the URL for that is "https://forum.aspose.com/c/cells" and when I try to access this URL, it does not display anything on browser except "test" word.

Pls check this URL is correct or not?

Also, mail says that you tried using v7.0 of aspose. We have 6.0. Can you pls try on this version and check the output.

Thanks,

Hetal S.

Hi,

We recommend you to download and try the latest version:
Aspose.Cells for .NET (Latest Version) I tested it before, v7.0 was working fine. The post is lost now.

hi,

Thanks for reply. I got that but the problem is we do not have the latest dll license so wven if this works in latest dll I will not be able to use it.

So pls suggest a solution in 6.0 version.

Thanks,

Hetal

Hi,

I am afraid, there is no other solution because v6.0 and v7.0 APIs are same. Also, we do not fix bugs based in older versions.