Karan Tongay
Karan Tongay

Reputation: 207

Java :: How to import the Excel File Stored in Google Drive to a specific Google Spreadsheet

I have a use case where I need to upload the Excel file programmatically to Google Drive and import that into the existing blank Google Spreadsheet using Java.

The Google Spreadsheet is getting created programmatically and I need to import the existing excel file present in Google Drive to the already created google spreadsheet using the Spreadsheet ID.

I have gone through different documentations but was unable to find an example or any support for the same.

Please suggest.

Upvotes: 0

Views: 3089

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117216

When the file is uploaded to google drive you need to set the mime type for it to convert the file automatically. Importing to Google Docs types

File fileMetadata = new File();
fileMetadata.setName("My Report");
fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");

java.io.File filePath = new java.io.File("files/report.csv");
FileContent mediaContent = new FileContent("text/csv", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
        .setFields("id")
        .execute();
System.out.println("File ID: " + file.getId());

Assuming that the file is a google sheet then a google drive file id is also a google sheet id. so you will be able to access it in Method: spreadsheets.sheets.copyTo

Upvotes: 4

Related Questions