Thư viện Java nguồn mở cho tài liệu bảng tính

Chuyển đổi tệp Excel trong các ứng dụng Java thông qua API nguồn mở.

Documents4J là một Java API mã nguồn mở để chuyển đổi Microsoft Excel sang các định dạng tệp khác. Điều này đạt được bằng cách ủy quyền chuyển đổi cho bất kỳ ứng dụng gốc nào hiểu được việc chuyển đổi tệp đã cho thành định dạng mục tiêu mong muốn. API cung cấp hai loại triển khai cục bộ và từ xa. Sử dụng tài liệu phiên bản cục bộ có thể được chuyển đổi trên cùng một máy, đó là cáp chuyển đổi định dạng tệp được yêu cầu. Sử dụng Remote API, bạn gửi tài liệu đến máy chủ bằng REST-API và máy chủ thực hiện chuyển đổi được yêu cầu.

Documents4J minh bạch và dễ sử dụng. Các nhà phát triển có thể làm việc với phiên bản địa phương của API trong khi quá trình phát triển và phiên bản từ xa có thể được sử dụng khi các nhà phát triển xuất bản ứng dụng trên phiên bản sản xuất.

Previous Next

Bắt đầu với Documents4J

Trước hết, bạn cần tạo một bản sao của document4j trên máy cục bộ của mình. Chỉ cần sao chép kho lưu trữ của Documents4j bằng cách sử dụng git hoặc bằng cách sao chép trực tiếp trên GitHub. Khi kho lưu trữ được nhân bản, bạn có thể xây dựng dự án bằng Mave

Cài đặt Documents4J qua GitHub


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

Chuyển đổi Microsoft Excel bằng Java

Documents4J là một API thông thạo để thực hiện chuyển đổi tài liệu. API không tiết lộ bất kỳ chi tiết nào về việc triển khai trình chuyển đổi hỗ trợ. Để thực hiện chuyển đổi tài liệu, API cung cấp giao diện IConverter. Sử dụng giao diện này, bạn có thể chuyển đổi định dạng tệp Microsoft Excel sang định dạng tệp mong muốn của bạn. Để tìm ra các định dạng tệp chuyển đổi được hỗ trợ, bạn có thể truy vấn phương thức getSupportedConversion (), phương thức này sẽ trả về nguồn và định dạng tệp đích.

Chuyển file Excel sang định dạng tập tin khác thông qua 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)) )

Chuyển tài liệu văn phòng PDF qua Java

Thư viện Documents4J nguồn mở đã bao gồm một số tính năng quan trọng để chuyển đổi các tài liệu văn phòng của Microsoft như các tệp Word, Excel và PowerPoint sang các định dạng tệp hỗ trợ khác như PDF hoặc hình ảnh, v.v. Ví dụ sau đây cho thấy các lập trình viên phần mềm có thể tải và chuyển đổi tệp Microsoft Word Docx dễ dàng như thế nào sang tệp PDF chỉ bằng một vài dòng mã.

Chuyển tập tin Docx đến PDF qua thư viện Java


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();
}
}
 Tiếng Việt