0

I have an excel file that i want to save as Document (word) in java, i tried to use Aspose library, i didn't find a way to convert from excel to word, so i converted the excel file to pdf and then from pdf to word .

That's what i did :

       import com.aspose.cells.PdfSaveOptions;
       import com.aspose.cells.SaveFormat;
       import com.aspose.cells.Workbook;
       import com.aspose.pdf.Document;

         .....

      Workbook workbook1 = new Workbook("file.xlsx");

      // Create PDF options
      PdfSaveOptions options = new PdfSaveOptions();
      options.setOnePagePerSheet(true);

      // Save the document in PDF format
      workbook1.save("file1.pdf", options);

first problem i get the pdf file with a red signature, how to remove it ?

After that i converted the pdf to word :

      Document pdfDocument = new Document("file1.pdf");
        //Save the file into MS document format
      pdfDocument.save("file2.docx", SaveFormat.DOCX);
     

The problem is that the doc file is totaly empty and i get an exception :

Caused by: class com.aspose.pdf.internal.ms.System.lv: Specified argument was out of the range of valid values.
Parameter name: Unknown save format
com.aspose.pdf.ADocument.lj(Unknown Source)
com.aspose.pdf.ADocument.lf(Unknown Source)
com.aspose.pdf.Document.lf(Unknown Source)
com.aspose.pdf.ADocument.lI(Unknown Source)
com.aspose.pdf.ADocument.save(Unknown Source)
com.aspose.pdf.Document.save(Unknown Source)
application.RecapController.print_tab(RcController.java:333)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
sun.reflect.misc.Trampoline.invoke(Unknown Source)
sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
sun.reflect.misc.MethodUtil.invoke(Unknown Source)
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
javafx.event.Event.fireEvent(Event.java:198)
javafx.scene.Node.fireEvent(Node.java:8411)
javafx.scene.control.Button.fire(Button.java:185)
com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
javafx.event.Event.fireEvent(Event.java:198)
javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
java.security.AccessController.doPrivileged(Native Method)
com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
com.sun.glass.ui.View.handleMouseEvent(View.java:555)
com.sun.glass.ui.View.notifyMouse(View.java:937)
com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
java.lang.Thread.run(Unknown Source)
    at com.aspose.pdf.ADocument.lj(Unknown Source)
    at com.aspose.pdf.ADocument.lf(Unknown Source)
    at com.aspose.pdf.Document.lf(Unknown Source)
    at com.aspose.pdf.ADocument.lI(Unknown Source)
    at com.aspose.pdf.ADocument.save(Unknown Source)
    at com.aspose.pdf.Document.save(Unknown Source)
    at application.RecapController.print_tab(RcController.java:333)
    ... 62 more

the line specified here (RcController.java:333) ->

  pdfDocument.save("file2.docx", SaveFormat.DOCX); 

Any idea ? or are there other libraries so i can use to convert directly from excel to doc ?

dEs12ZER
  • 788
  • 4
  • 24
  • 51

1 Answers1

1

Aspose.Cells introduced new Excel to DOCX feature in newer version (e.g. Aspose.Cells for Java v21.9) which can directly convert an Excel spreadsheet to DOCX (without using intermediate PDF). So, you may try it if your file is not complex. See the sample code that you may try: e.g.

Sample code:

Workbook workbook = new Workbook("f:\\files\\ExcelFile.xlsx");
workbook.save("f:\\files\\out1.docx", SaveFormat.DOCX);

If your file is not converted to DOCX properly with direct approach, please share your input Excel file. We will check your issue soon. Moreover, for complex Excel files, you may still use your existing approach, i.e., Excel to PDF via Aspose.Cells for Java, then PDF to DOCX via Aspose.PDF. We also recommend you to post your issue (in case, you are not able to sort it out using latest versions of the APIs) with resource files on dedicated forums where we could address your issue precisely.

PS. I am working as Support developer/ Evangelist at Aspose.

Amjad Sahi
  • 1,813
  • 1
  • 10
  • 15