Skip to content

Commit

Permalink
reading export data from list instead of gridview
Browse files Browse the repository at this point in the history
  • Loading branch information
timokz committed Nov 4, 2021
1 parent 2bd919c commit 9403ac4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
49 changes: 39 additions & 10 deletions lib/get_guest_data.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import 'dart:html';
import 'package:intl/intl.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'guest.dart';
import 'guest_data_source.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:syncfusion_flutter_datagrid_export/export.dart';
import 'dart:convert';

// ignore: avoid_web_libraries_in_flutter
//import 'dart:html';
import 'dart:io';
import 'package:syncfusion_flutter_xlsio/xlsio.dart'
hide Alignment, Column, Row;

import 'home.dart';

class GetGuestData extends StatefulWidget {
const GetGuestData({Key? key}) : super(key: key);

Expand Down Expand Up @@ -75,15 +75,45 @@ class _GetGuestDataState extends State<GetGuestData> {
guestList.add(g.data() as Guest);
}

List<ExcelDataRow> _buildReportDataRows() {
List<ExcelDataRow> excelDataRows = <ExcelDataRow>[];

excelDataRows = guestList.map<ExcelDataRow>((Guest e) {
return ExcelDataRow(cells: <ExcelDataCell>[
ExcelDataCell(columnHeader: 'name', value: e.nName),
ExcelDataCell(columnHeader: 'name', value: e.vName),
ExcelDataCell(columnHeader: 'location', value: e.location),
ExcelDataCell(
columnHeader: 'Entry Time',
value:
DateFormat('yyyy-MM-dd – kk:mm').format(e.entryTime)),
ExcelDataCell(columnHeader: 'email', value: e.email),
ExcelDataCell(columnHeader: 'phone', value: e.phone),
]);
}).toList();

return excelDataRows;
}

Future<void> exportDataGridToExcel() async {
// final Workbook workbook = _key.currentState!.exportToExcelWorkbook();

// final List<int> bytes = workbook.saveAsStream();
// File('DataGrid.xlsx').writeAsBytes(bytes, flush: true);
final Workbook workbook = Workbook();
final Worksheet worksheet = workbook.worksheets[0];
_key.currentState!.exportToExcelWorksheet(worksheet);
final Worksheet sheet = workbook.worksheets[0];
sheet.importList(guestList, 1, 1, true);
final List<ExcelDataRow> dataRows = _buildReportDataRows();
sheet.importData(dataRows, 1, 1);
final List<int> bytes = workbook.saveAsStream();
File('DataGrid.xlsx').writeAsBytes(bytes, flush: true);
}
workbook.dispose();

AnchorElement(
href:
"data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
..setAttribute("download", "GuestData.xlsx")
..click();
}

return Scaffold(
backgroundColor: const Color(0xffffffff),
Expand All @@ -94,8 +124,7 @@ class _GetGuestDataState extends State<GetGuestData> {
actions: <Widget>[
IconButton(
icon: const Icon(Icons.download_rounded),
onPressed: () => exportDataGridToExcel()
),
onPressed: () => exportDataGridToExcel()),
// IconButton(
// onPressed: exportDataGridToExcel,
// icon: const Icon(Icons.airplane_ticket))
Expand Down
1 change: 0 additions & 1 deletion lib/guest.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:phone_number/phone_number.dart';

@immutable
class Guest {
Expand Down
2 changes: 1 addition & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class _HomeScreenState extends State<HomeScreen> {
child:
SnackBar(content: Text("Falsches Passwort")))
},
child: Text("Check")), // Center(child:),
child: const Text("Check")), // Center(child:),
],
)),
// This trailing comma makes auto-formatting nicer for build methods.
Expand Down
24 changes: 10 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class _AppState extends State<App> {
bool _error = false;
final Future<FirebaseApp> _initialization = Firebase.initializeApp();



@override
void initState() {
initializeFlutterFire();
Expand Down Expand Up @@ -61,19 +59,17 @@ class _AppState extends State<App> {
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
print("made it to pos connection ");

return MaterialApp(
title: 'tqwcoviddata',
theme: ThemeData(
primaryColor: Colors.black,
backgroundColor: const Color(0xffffffff),
fontFamily: 'Roboto',
),
home: HomeScreen()//HomeScreen(),

// home: const HomeScreen());
);
title: 'tqwcoviddata',
theme: ThemeData(
primaryColor: Colors.black,
backgroundColor: const Color(0xffffffff),
fontFamily: 'Roboto',
),
home: HomeScreen() //HomeScreen(),
//home: GetGuestData(),
// home: const HomeScreen());
);
}
// Otherwise, show something whilst waiting for initialization to complete
return const CircularProgressIndicator(
Expand Down

0 comments on commit 9403ac4

Please sign in to comment.