diff --git a/app/src/main/java/com/example/if3210_2024_android_aab/SettingFragment.kt b/app/src/main/java/com/example/if3210_2024_android_aab/SettingFragment.kt
index a75a1d58f3bcd015f9fab84461c8c91aae33b3e3..70f86e960cdcf5e29b1cba93ba8daf92c5c35c2d 100644
--- a/app/src/main/java/com/example/if3210_2024_android_aab/SettingFragment.kt
+++ b/app/src/main/java/com/example/if3210_2024_android_aab/SettingFragment.kt
@@ -1,5 +1,7 @@
 package com.example.if3210_2024_android_aab
 
+import android.app.Activity
+import android.app.AlertDialog
 import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
@@ -14,6 +16,8 @@ import android.widget.Toast
 
 import androidx.core.content.FileProvider
 import androidx.fragment.app.viewModels
+import org.apache.poi.hssf.usermodel.HSSFWorkbook
+import org.apache.poi.ss.usermodel.Workbook
 import org.apache.poi.xssf.usermodel.XSSFWorkbook
 import java.io.File
 import java.io.FileOutputStream
@@ -35,6 +39,10 @@ class SettingFragment : Fragment() {
     private var param1: String? = null
     private var param2: String? = null
 
+    private var fileName : String? = null
+
+    private val REQUEST_CODE_EMAIL = 123
+
     private val transactionViewModel: TransactionViewModel by viewModels {
         TransactionViewModelFactory((requireActivity().application as TransactionApplication).repository)
     }
@@ -58,41 +66,34 @@ class SettingFragment : Fragment() {
 
         sendButton.setOnClickListener {
             transactionViewModel.allTransactions.observe(viewLifecycleOwner) { transactions ->
-                val workbook = saveToExcel(transactions)
-
-                val fileName = "transactions_${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))}.xlsx"
-                val cacheDir = requireContext().cacheDir // Use requireContext() to ensure non-null context
-                val file = File(cacheDir, fileName)
-                val outputStream = FileOutputStream(file)
-                workbook.write(outputStream)
-                workbook.close()
-                outputStream.close()
-
-                val uri = FileProvider.getUriForFile(
-                    requireContext(),
-                    "${requireContext().packageName}.provider",
-                    file
-                )
-
-                val gmailIntent = Intent(Intent.ACTION_SEND).apply {
-                    type = "text/plain" // Set the MIME type
-                    putExtra(Intent.EXTRA_EMAIL, arrayOf(SharedPreference.getEmail(requireContext())))
-                    putExtra(Intent.EXTRA_SUBJECT, "Transaction Data")
-                    putExtra(Intent.EXTRA_STREAM, uri)
-                    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
-                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
-                    `package` = "com.google.android.gm" // Specify package for Gmail
-                }
-
-                val pm = requireContext().packageManager
-                val resolveInfo = pm.resolveActivity(gmailIntent, 0)
-
-                if (resolveInfo != null) {
-                    startActivity(gmailIntent)
-                } else {
-                    // Fallback to chooser if Gmail is not available
-                    val emailIntent = Intent(Intent.ACTION_SEND).apply {
+                val options = arrayOf("XLS", "XLSX")
+                var isXlsx: Boolean
+                val alertDialogBuilder = AlertDialog.Builder(requireContext())
+                alertDialogBuilder.setTitle("Choose Excel Format")
+                alertDialogBuilder.setItems(options) { dialog, which ->
+                    val selectedFormat = options[which]
+                    if (selectedFormat == "XLSX") {
+                        isXlsx = true
+                    } else {
+                        isXlsx = false
+                    }
+                    fileName = "transactions_${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))}.$selectedFormat"
+                    val workbook = saveToExcel(transactions, isXlsx)
+
+                    val cacheDir = requireContext().cacheDir // Use requireContext() to ensure non-null context
+                    val file = File(cacheDir, fileName)
+                    val outputStream = FileOutputStream(file)
+                    workbook.write(outputStream)
+                    workbook.close()
+                    outputStream.close()
+
+                    val uri = FileProvider.getUriForFile(
+                        requireContext(),
+                        "${requireContext().packageName}.provider",
+                        file
+                    )
+
+                    val gmailIntent = Intent(Intent.ACTION_SEND).apply {
                         type = "text/plain" // Set the MIME type
                         putExtra(Intent.EXTRA_EMAIL, arrayOf(SharedPreference.getEmail(requireContext())))
                         putExtra(Intent.EXTRA_SUBJECT, "Transaction Data")
@@ -100,29 +101,67 @@ class SettingFragment : Fragment() {
                         addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                         addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                         addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                        `package` = "com.google.android.gm" // Specify package for Gmail
                     }
 
-                    val chooserIntent = Intent.createChooser(emailIntent, "Send email...")
-                    startActivity(chooserIntent)
+                    val pm = requireContext().packageManager
+                    val resolveInfo = pm.resolveActivity(gmailIntent, 0)
+
+                    if (resolveInfo != null) {
+                        startActivity(gmailIntent)
+                    } else {
+                        // Fallback to chooser if Gmail is not available
+                        val emailIntent = Intent(Intent.ACTION_SEND).apply {
+                            type = "text/plain" // Set the MIME type
+                            putExtra(Intent.EXTRA_EMAIL, arrayOf(SharedPreference.getEmail(requireContext())))
+                            putExtra(Intent.EXTRA_SUBJECT, "Transaction Data")
+                            putExtra(Intent.EXTRA_STREAM, uri)
+                            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                        }
+
+                        val chooserIntent = Intent.createChooser(emailIntent, "Send email...")
+                        startActivityForResult(chooserIntent, REQUEST_CODE_EMAIL)
+                    }
+                    fileName = "transactions_${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))}.$selectedFormat"
+                    // Rest of your code for sending the email
                 }
+                alertDialogBuilder.create().show()
             }
         }
 
 
         saveButton.setOnClickListener {
             transactionViewModel.allTransactions.observe(viewLifecycleOwner) { transactions ->
-                val workbook = saveToExcel(transactions)
-                // Save workbook to a file
-                val fileName = "transactions_${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))}.xlsx"
-                val downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
-                val file = File(downloadsDir, fileName)
-                val outputStream = FileOutputStream(file)
-                workbook.write(outputStream)
-                workbook.close()
-                outputStream.close()
-
-                // Notify user of successful save
-                Log.d("SAVE_EXCEL", "saveToExcel: $fileName")
+                val options = arrayOf("XLS", "XLSX")
+                var isXlsx: Boolean
+                val alertDialogBuilder = AlertDialog.Builder(requireContext())
+                alertDialogBuilder.setTitle("Choose Excel Format")
+                alertDialogBuilder.setItems(options) { dialog, which ->
+                    val selectedFormat = options[which]
+                    if (selectedFormat == "XLSX") {
+                        isXlsx = true
+                    } else {
+                        isXlsx = false
+                    }
+                    fileName = "transactions_${
+                        LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
+                    }.$selectedFormat"
+                    val workbook = saveToExcel(transactions, isXlsx)
+                    // Save workbook to a file
+                    val downloadsDir =
+                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+                    val file = File(downloadsDir, fileName)
+                    val outputStream = FileOutputStream(file)
+                    workbook.write(outputStream)
+                    workbook.close()
+                    outputStream.close()
+
+                    // Notify user of successful save
+                    Log.d("SAVE_EXCEL", "saveToExcel: $fileName")
+                }
+                alertDialogBuilder.create().show()
             }
         }
 
@@ -136,6 +175,18 @@ class SettingFragment : Fragment() {
         return view
     }
 
+    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+        super.onActivityResult(requestCode, resultCode, data)
+        if (requestCode == REQUEST_CODE_EMAIL) {
+            // Check if the result is from the email intent
+            val file = File(requireContext().cacheDir, fileName)
+            if (resultCode != Activity.RESULT_OK && file.exists()) {
+                // Delete the file if the email sending was cancelled or unsuccessful
+                file.delete()
+            }
+        }
+    }
+
     companion object {
         /**
          * Use this factory method to create a new instance of
@@ -156,8 +207,8 @@ class SettingFragment : Fragment() {
             }
     }
 
-    private fun saveToExcel(dataList: List<Transaction>) : XSSFWorkbook {
-        val workbook = XSSFWorkbook()
+    private fun saveToExcel(dataList: List<Transaction>, isXlsx: Boolean) : Workbook {
+        val workbook = if (isXlsx) XSSFWorkbook() else HSSFWorkbook()
         val sheet = workbook.createSheet("Transaction Data")
 
         // Add header row