This article covers how to convert MS Excel XLS or XLSX to Google Sheets programmatically in C# .NET.
Spreadsheets are commonly used for storing small or large-scale data in the form of rows and columns. Various applications are available to create and manipulate spreadsheets, of which MS Excel is a popular one. Alongside, Google provides Google Sheets, which is used to create and update spreadsheets online. In addition, Google Sheets lets you share the spreadsheets with multiple people in real-time. In certain cases, you may need to export data from Excel XLS or XLSX files to a spreadsheet in Google Sheets programmatically. So let’s see how you can read data from an Excel file and write it to Google Sheets’ spreadsheet in C# .NET.
- Prerequisites – Convert Excel File to Google Sheets
- Steps to Export Data from Excel XLSX to Google Sheets
- Complete Source Code
Prerequisites – Convert Excel File to Google Sheets in C#
Google Cloud Project
To communicate with Google Sheets, we will have to create a project on Google Cloud and enable Google Sheets API. The following are the steps to create a project and enable the API.
- Go to Google Cloud console.
- Create a new project on the console.
- Select the project you have just created.
- From the navigation menu, select APIs and Services and then Dashboard.
- Click Enable APIs and Services button (see details).
- Search and enable Google Sheets API.
- Configure OAuth Consent Screen and set scopes of the application.
- Add test users, which are used when application is not published.
- On APIs and Services page, go to Credentials.
- Click Create Credentials button and select OAuth client ID.
- In application type, select Desktop App (as we are creating a console application in this aritcle).
- Download the JSON file.
C# .NET APIs for Excel to Google Sheets Conversion
To export data from Excel XLS/XLSX files to Google Sheets, we will need the following APIs.
- Aspose.Cells for .NET – To read the data from Excel files.
- Google.Apis.Sheets.v4 – To create and update spreadsheets on Google Sheets.
At the time of writing this article, we used Aspose.Cells for .NET 22.2 and Google.Apis.Sheets.v4 1.56.0.2608.
Export Data from Excel XLSX to Google Sheets in C#
The following is the step-by-step guide on how to read data from an Excel XLSX file and write it to Google Sheets in a C# console application.
1. Create a new console application project in Visual Studio 2013 or later.
2. Install Aspose.Cells for .NET and Google Sheets APIs in the project.
PM> Install-Package Aspose.Cells
PM> Install-Package Google.Apis.Sheets.v4
3. Copy the JSON file (we have downloaded after creating credentials in Google Cloud) and paste it into project’s directory.
4. Initialize the Google Sheets service using the credentials (JSON file) and define the scopes of the application. The scopes define the access permissions to the sheets and their properties. The following function initializes the Google Sheets service and returns the SheetsService object.
5. Create CreateSpreadsheet function that creates a new spreadsheet on Google Sheets, sets the name of the default sheet, and returns a Spreadsheet object.
6. Create an AddSheet function to add a new sheet in the Google spreadsheet.
7. Now, create a new function ExportDataFromExcelToGoogleSheet, which reads and exports data from an Excel file to Google Sheets. In this function, first, load the Excel XLS/XLSX file using Aspose.Cells for .NET and get the name of the first worksheet in the workbook.
8. Then, call the CreateSpreadsheet function to create a new spreadsheet on Google Sheets.
9. Loop through the worksheets in the Excel file. In each iteration, read data from the worksheet and add it to a list.
10. For each worksheet in the Excel file, create a request to write data to the spreadsheet in the Google Sheets.
The complete function to 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 C#.
Demo – Convert Excel to Google Sheets in C#
Get a Free Aspose.Cells License
You can get a free temporary license and use Aspose.Cells for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to convert Excel XLS or XLSX files to Google Sheets in C#. We have covered how to create a project on Google Cloud, enable Google Sheets API, read Excel files, and export data from Excel files to Google Sheets. Besides, you can explore other features of Aspose.Cells for .NET using the documentation. Also, you can ask your questions via our forum.