Convert Excel XLS or XLSX to Google Sheets in Java

Excel is a popular choice for creating and managing data, but it may not always be the best option for collaboration, especially when multiple users need to work on the same document simultaneously. Google Sheets, with its cloud-based nature and real-time collaboration features, is an excellent alternative. In this blog post, we’ll explore how to convert Excel files to Google Sheets in a Java application using Aspose.Cells for Java, a powerful and flexible Java library for working with Excel files.

Prerequisites - Convert Excel File 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 Convert 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>23.8</version>
	</dependency>
</dependencies>

Convert Excel File 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.

Get a Free License

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

Conclusion

Converting Excel files to Google Sheets using Aspose.Cells for Java is a straightforward process. With this powerful Java library, you can automate the conversion and easily share your data with collaborators using Google Sheets’ real-time collaboration features. This method is especially valuable when you need to work with others on spreadsheet data and want to take advantage of Google Sheets’ cloud-based capabilities.

By following the steps outlined in this blog post, you can streamline your workflow and enhance your collaborative efforts, making it easier than ever to work with Excel data in a collaborative and dynamic way.

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