AdvMobDev Class01
AdvMobDev Class01
Development
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Creates a new Excel workbook
•The Excel.createExcel() method initializes an Excel object with a default sheet named 'Sheet1'.
•The excel object is the main workbook that holds one or more sheets (similar to Excel tabs).
• The async keyword allows the function to handle asynchronous tasks, such as saving files.
1. Sheet: A sheet is a single page inside of an excel file, if you are familiar with
excel you will understand that a single excel file can contain multiple sheets
1. excel[‘01 ]’فاتورة: Each sheet in an excel file has a name, so to get reference to the
sheet you need it’s name
2. In the second line of code is used to get reference to cell “A1”, which as we
know is the first cell in the sheet
3. This line sets the value of cell “A1” to 8
Inserting values into a row with
List<String>
Sheet sheetObject = excel['01 ;]'فاتورة
List<String> data = ["Mr","’mazin", "Alkathiri"];
sheetObject.appendRow(data);
saveExcel() async {
.
.
.
.
.
}
2. Requesting Storage Permission
var res = await Permission.storage.request();
•Requests storage permissions from the user at runtime using the permission_handler
package.
•Explanation:
•Permission.storage.request() prompts the user to allow or deny storage access.
•await ensures the function waits for the user's response before continuing.
•Result:
•The res variable stores whether the permission was granted or denied.
•Important Note:
Make sure the permission_handler dependency is added to the pubspec.yaml file:
permission_handler:
3. Saving the Excel Data
var fileBytes =
excel.save();
Saves the current Excel object (excel) to memory as bytes.
These bytes can be written directly to a file.
•excel.save():
•Serializes the Excel workbook into binary data (byte format).
•Returns the file content as Uint8List for writing.
•Example:
Suppose the Excel workbook contains data like this:
Then, fileBytes will contain encoded binary data representing this structure.
A B C
8
Mr Mazin Alkathiri
4. File Path and Creation
File(join("/storage/emulated/0/Download/billNo03.xlsx"))
Specifies the file path and name for the Excel file.
•Path Explanation:
•/storage/emulated/0/Download/:
Refers to the Download folder in the internal storage of an Android device.
•billNo03.xlsx:
The file name. You can change this to suit your needs.
•File Creation:
..createSync(recursive: true)
•Ensures the file is created synchronously if it does not already exist.
•The recursive: true parameter ensures that any missing parent directories are also created.
5. Writing Data to File
..writeAsBytesSync(fileBytes!);
Declares a function named readexcelfile that accepts an optional File? parameter named file.
It processes the Excel file to extract and print specific cell values.
•async:
Allows asynchronous operations, although in this code there are no await calls. The function could be
modified to handle asynchronous file operations if needed.
•File?:
The nullable type File? ensures the function can handle cases where the file parameter might be
null.
Reading XLSX File Bytes
var bytes = file!.readAsBytesSync();
• Details:
•file!: The null-check operator (!) asserts that file is not null at runtime.
•readAsBytesSync(): Reads the file synchronously, blocking
execution until the read operation completes.
Decoding Excel File
var excel = Excel.decodeBytes(bytes);
} catch (_) {}
super.setState(() {
});
},
child: const Text('Pick an Excel File'),
),