How to add hyperlinks to Excel worksheet programmatically in Flutter?
Syncfusion Flutter XlsIO library is used to create and modify Excel documents. Using this library, you can also add hyperlinks to an Excel worksheet using Flutter. Hyperlinks in the Excel document are used to navigate to web pages or any other external content.
Steps to add Hyperlinks to the Excel document programmatically.
Step 1: Create a new Flutter application project.
- Open Visual Studio Code (after installing the Dart and Flutter extensions as stated on this setup editor page).
- Click View -> Command Palette…
- Type Flutter and choose Flutter: New Project.
- Enter the project name and press the Enter button.
- Now choose the location of the project.
Step 2: Add the following code to your pubspec.yaml file to install the Syncfusion Flutter XlsIO package in your application. It will be automatically downloaded from Pub once you trigger the flutter pub get command or Get packages option from Visual Studio Code.
.
dependencies: syncfusion_flutter_xlsio: ^18.4.31-beta
Import the following package in your main.dart file.
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
Step 3: Add the following code in the lib/main.dart file to create a simple button.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: const Text(
'Generate Excel',
style: TextStyle(color: Colors.white),
),
color: Colors.blue,
onPressed: _addHyperlinkToExcel,
)
],
),
),
);
}
Step 4: Add the following code to the _addHyperlinkToExcel() function to add hyperlinks to a Excel document programmatically.
Future<void> _addHyperlinkToExcel() async {
// Create a Excel document.
// Creating a workbook.
final Workbook workbook = Workbook();
// Accessing via index
final Worksheet sheet = workbook.worksheets[0];
// Creating a Hyperlink for a Website.
final Hyperlink hyperlink = sheet.hyperlinks.add(sheet.getRangeByName('A1'),
HyperlinkType.url, 'https://www.syncfusion.com');
hyperlink.screenTip =
'To know more about Syncfusion products, go through this link.';
hyperlink.textToDisplay = 'Syncfusion';
// Save and launch the excel.
final List<int> bytes = workbook.saveAsStream();
// Dispose the document.
workbook.dispose();
}
Step 5: Use the following code to save and launch the generated Excel file.
- Add the following dependencies in your pubspec.yaml file.
path_provider: ^1.6.7 open_file: ^3.0.1 #Open source library to launch the Excel file in mobile devices
- Import the following packages in your main.dart file.
import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:open_file/open_file.dart';
- Include the following code snippet in the _addHyperlinkToExcel() method to open the Excel document in the mobile‘s default application (any Excel application).
// Get the storage folder location using path_provider package. final Directory directory = await getExternalStorageDirectory(); // Get directory path. final String path = directory.path; // Create an empty file to write Excel data. final File file = File('$path/ExcelHyperlink.xlsx'); // Write Excel data. await file.writeAsBytes(bytes, flush: true); // Launch the file (using open_file package). await OpenFile.open('$path/ExcelHyperlink.xlsx');
Step 6: Run the sample using the flutter run command. This will add hyperlinks to the Excel document.
After the application launches, you will get the Excel document as follows.
Output Excel document created using XlsIO.
You can download the complete work sample from add-hyperlink-to-excel.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to add hyperlinks to an Excel worksheet programmatically in Flutter.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!