This article shows how to convert MS Excel XLS or XLSX to Google Sheets programmatically in Java.

Convert Excel XLS or XLSX to Google Sheets in Java

Google Sheets is a popular online application that lets you create and manipulate spreadsheet documents. Furthermore, it allows you to share the spreadsheets with multiple people in real-time. Therefore, often you have to export data from Excel workbooks to a spreadsheet in Google Sheets programmatically. In this article, we will give a complete walk-through of how to export Excel data to a Google Sheets in Java.

Prerequisites - Export Excel Data to Google Sheets in Java

Google Cloud Project

Google Cloud is a cloud computing platform, which provides various types of services that we can utilize in our applications. To use Google Sheets API, we will have to create a project on the Google Cloud console and enable Google Sheets API. You can read the step-by-step guide on how to create a Google Cloud project and enable Sheets API.

Java APIs to Export Excel Data to Google Sheets

To export data from Excel XLS/XLSX files to Google Sheets, we will need the following APIs.

  • Aspose.Cells for Java - To read the data from Excel files.
  • Google Client APIs for Java - To create and update spreadsheets on Google Sheets.

To install these APIs, you can use the following configurations in your pom.xml file.

<repositories>
	<repository>
		<id>AsposeJavaAPI</id>
		<name>Aspose Java API</name>
		<url>http://repository.aspose.com/repo/</url>
	</repository>
</repositories>
<dependencies>
	<dependency>
		<groupId>com.google.api-client</groupId>
		<artifactId>google-api-client-gson</artifactId>
		<version>1.33.0</version>
	</dependency>
	<dependency>
		<groupId>com.google.oauth-client</groupId>
		<artifactId>google-oauth-client-jetty</artifactId>
		<version>1.32.1</version>
	</dependency>
	<dependency>
		<groupId>com.google.apis</groupId>
		<artifactId>google-api-services-sheets</artifactId>
		<version>v4-rev20210629-1.32.1</version>
	</dependency>
	<dependency>
		<groupId>com.aspose</groupId>
		<artifactId>aspose-cells</artifactId>
		<version>22.2</version>
	</dependency>
</dependencies>

Export Excel Data to Google Sheets in Java

The following are the steps to read an Excel file and then export its data to Google Sheets in a Java console application.

1. Create a new Java application (desktop).

2. Install Aspose.Cells for Java and Google Client APIs in the project, as mentioned in the previous section.

3. Copy the JSON file (the one we have downloaded after creating credentials in Google Cloud) and paste it into the project’s directory.

4. Specify the scopes of the application that define the access permissions to the sheets. Also, create variables for the token directory path and JSON factory.

5. Create a getCredentials method that authorizes the user using the credentials file.

6. Create createSpreadsheet method to create a new spreadsheet on Google Sheets and set the name of the default sheet. This method returns a Spreadsheet object.

7. Create an addSheet method to add a new sheet in the Google spreadsheet.

8. Now, create the exportExcelToGoogle method, and in this method, initialize the Sheets service.

9. Then, load the Excel XLS/XLSX file using Aspose.Cells for Java. Also, get the name of the first worksheet in the workbook.

10. Call the createSpreadsheet method to create a new spreadsheet on Google Sheets.

11. Read data from each worksheet and save it into a list.

12. For each worksheet in the Excel file, create a request to write data to the spreadsheet in the Google Sheets.

The complete method to read and export data from an Excel file to a spreadsheet in Google Sheets is given below.

Complete Source Code

The following is the complete source code to convert an Excel XLSX file to Google Sheets in Java.

Demo - Excel to Google Sheets Conversion in Java

Java Excel to Google Sheets API - Get a Free License

You can get a free temporary license and use Aspose.Cells for Java without evaluation limitations.

Conclusion

In this article, you have learned how to export Excel XLS or XLSX files to Google Sheets programmatically in Java. We have provided the complete guidelines on how to create a Google Cloud project, enable Google Sheets API, read Excel files, and export data from Excel files to Google Sheets. You can also visit the documentation to read more about Aspose.Cells for Java. In case you would have any questions or queries, let us know via our forum.

See Also