6

I need to insert (embed) a file object (.txt file) in MS Excel sheet using Java. The requirement is not to put the contents of .txt file into Excel. Instead I need to put the whole file as an embedded object into Excel. I am using Apache POI jar for this.

I have seen all the examples present in poi-3.7-beta1 but was not able to find any example, to insert (embed) a file object in Excel sheet. I have seen POIFSFileSystem classes but was not able to find the appropriate class to use for this problem. I am facing issue in embedding file object into Excel. Please help me doing this using Apache POI or any other jar.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jyoti
  • 2,065
  • 3
  • 25
  • 28
  • Is such embedding even possible with the Excel UI, or with VBA (behind-the-scenes programming still directly to Excel)? --- If the answer is "yes", then search Excel's documentation, or VBA examples and sources, for how to do it in VBA, then visit POI's source-code, and verify if it's implemented anywhere. --- If the answer is "yes" again, track the implementations' uses to find out the highest level functions you can use, and what they do, and then come up with an implementation for what you need based on the functions that are available. – CosmicGiant Nov 05 '15 at 01:03
  • Not sure how "translatable" VBA is to Java, but you could of course use the Macro Recorder in Excel to do this, then look at the VBA code. Perhaps that will help give you an idea? – BruceWayne Nov 23 '15 at 20:35

2 Answers2

1

You could manipulate it through Visual Basic Script, just store it somewhere and call it from java.

Script Example:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("myExcel.xlsm")
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Quit

Java:

cmd = "you_path\\myVBS.vbs";
Runtime.getRuntime().exec(cmd);
JCKUSANAGI
  • 11
  • 3
0

Here is the VBA code from Macro Recorder:

Sub InsertObject()
    ActiveSheet.OLEObjects.Add(Filename:="C:\Placeholder.txt", _
        Link:=False, DisplayAsIcon:=True, IconFileName:= _
        "C:\Windows\system32\packager.dll", IconIndex:=0, IconLabel:= _
        "C:\Placeholder.txt").Select
End Sub

Also check out this question: Embed files into Excel using Apache POI

theodore
  • 1
  • 2
  • I am unclear how your VBA solution, while interesting, has anything to do with a request for how to accomplish this using Java. – David Hoelzer Dec 12 '15 at 01:41