1. Produkty
  2.   Arkusz
  3.   Java
  4.   Documents4J
 
  

Biblioteka Open Source Java dla dokumentów arkuszy kalkulacyjnych

Konwertuj pliki Excela w aplikacjach Java za pomocą interfejsu Open Source API.

Documents4J to open-source Java API z konwersji Microsoft Excel na inne formaty plików. Osiąga się to poprzez delegowanie konwersji do dowolnej aplikacji natywnej, która rozumie konwersję danego pliku do pożądanego formatu docelowego. API oferuje dwa rodzaje implementacji lokalnych i zdalnych. Korzystając z wersji lokalnej, dokument można przekonwertować na tym samym komputerze, który jest kablem konwertującym żądany format pliku. Korzystając z Remote API, wysyłasz dokument do serwera za pomocą REST-API, a serwer wykonuje żądaną konwersję.

Documents4J jest przejrzysty i prosty w użyciu. Deweloperzy mogą pracować z lokalną wersją interfejsu API podczas programowania, a wersja zdalna może być używana, gdy deweloperzy publikują aplikację w środowisku produkcyjnym.

Previous Next

Pierwsze kroki z dokumentami4J

Przede wszystkim musisz utworzyć kopię document4j na swoim lokalnym komputerze. Po prostu sklonuj repozytorium document4j za pomocą git lub sklonuj je bezpośrednio na GitHub. Po sklonowaniu repozytorium możesz zbudować projekt za pomocą Mave

Zainstaluj Documents4J przez GitHub


git clone https://github.com/documents4j/documents4j.git
cd documents4j
mvn package
            

Konwertuj Microsoft Excel za pomocą Javy

Documents4J to płynny interfejs API do wykonywania konwersji dokumentów. Interfejs API nie ujawnia żadnych szczegółów implementacji konwertera kopii zapasowych. Aby przeprowadzić konwersję dokumentów, API oferuje interfejs IConverter. Za pomocą tego interfejsu możesz przekonwertować format pliku Microsoft Excel na żądany format pliku. Aby znaleźć obsługiwane formaty plików konwersji, możesz zapytać metodę getSupportedConversion() , która zwróci źródłowe i docelowe formaty plików.

Konwertuj plik Excel do innych formatów plików przez Java


Const WdExportFormatPDF = 17
Const MagicFormatPDF = 999
Dim arguments
Set arguments = WScript.Arguments
' Transforms a file using MS Excel into the given format.
Function ConvertFile( inputFile, outputFile, formatEnumeration )
  Dim fileSystemObject
  Dim excelApplication
  Dim excelDocument
  ' Get the running instance of MS Excel. If Excel is not running, exit the conversion.
  On Error Resume Next
  Set excelApplication = GetObject(, "Excel.Application")
  If Err <> 0 Then
    WScript.Quit -6
  End If
  On Error GoTo 0
  ' Find the source file on the file system.
  Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  inputFile = fileSystemObject.GetAbsolutePathName(inputFile)
  ' Convert the source file only if it exists.
  If fileSystemObject.FileExists(inputFile) Then
    ' Attempt to open the source document.
    On Error Resume Next
    Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True)
    If Err <> 0 Then
      WScript.Quit -2
    End If
    On Error GoTo 0
    On Error Resume Next
    If formatEnumeration = MagicFormatPDF Then
      excelDocument.ExportAsFixedFormat xlTypePDF, outputFile
    Else
      excelDocument.SaveAs outputFile, formatEnumeration
    End If
    ' Close the source document.
    excelDocument.Close False
    If Err <> 0 Then
      WScript.Quit -3
    End If
    On Error GoTo 0
    ' Signal that the conversion was successful.
    WScript.Quit 2
  Else
    ' Files does not exist, could not convert
    WScript.Quit -4
  End If
End Function
' Execute the script.
Call ConvertFile( WScript.Arguments.Unnamed.Item(0), WScript.Arguments.Unnamed.Item(1), CInt(WScript.Arguments.Unnamed.Item(2)) )

Konwertuj dokumenty biurowe do PDF przez Java

Biblioteka open source Documents4J zawiera kilka ważnych funkcji do konwersji dokumentów biurowych Microsoft, takich jak Word, Excel i PowerPoint plików do innych formatów plików wsparcia, takich jak PDF lub obraz itp. Poniższy przykład pokazał, jak łatwo programistów może załadować i konwertować plik Microsoft Word Docx do PDF z zaledwie kilka linii kodu.

Przelicz Office Docx File do PDF przez Java Biblioteka


public class NewMain {
    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException, ExecutionException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    InputStream in = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator +"out.rtf"));
    IConverter converter = LocalConverter.builder()
            .baseFolder(new File(System.getProperty("user.dir") + File.separator +"test"))
            .workerPool(20, 25, 2, TimeUnit.SECONDS)
            .processTimeout(5, TimeUnit.SECONDS)
            .build();
    Future conversion = converter
            .convert(in).as(DocumentType.RTF)
            .to(bo).as(DocumentType.PDF)
            .prioritizeWith(1000) // optional
            .schedule();
    conversion.get();
    try (OutputStream outputStream = new FileOutputStream("out.pdf")) {
        bo.writeTo(outputStream);
    }
    in.close();
    bo.close();
}
}
 Polski