// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_xlsio/xlsio.dart' hide Column;
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:convert';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
Map<String, dynamic>? data;
@override
void initState() {
super.initState();
_getData();
}
_getData() async {
try {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/data.json');
String jsonString = await file.readAsString();
data = jsonDecode(jsonString);
setState(() {});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Json to Excel"),
actions: <Widget>[
IconButton(
constraints: const BoxConstraints.expand(width: 80),
icon: const Text('CREATE Excel', textAlign: TextAlign.center),
onPressed: () {
importJsonData();
},
),
],
),
body: data == null
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: data!['measure'].length,
itemBuilder: (context, index) {
return Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Count: ${data!['measure'][index]['count']}',
style: const TextStyle(fontSize: 18.0),
),
const SizedBox(height: 8.0),
Text(
'Range Array: ${data!['measure'][index]['range_array']}',
style: const TextStyle(fontSize: 18.0),
),
const SizedBox(height: 8.0),
Text(
'Force Array: ${data!['measure'][index]['force_array']}',
style: const TextStyle(fontSize: 18.0),
),
],
),
),
);
},
),
);
}
void importJsonData() async {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/data.json');
String jsonString = await file.readAsString();
final jsonData = jsonDecode(jsonString);
setState(() {});
// Create a new Workbook
final Workbook workbook = Workbook();
final Worksheet worksheet = workbook.worksheets[0];
// Add data to cells
worksheet.getRangeByName('A1').setText('Count');
worksheet.getRangeByName('A2').setValue(jsonData["measure"]![0]["count"]);
worksheet.getRangeByName('B1').setText('Range Array');
worksheet.getRangeByName('B2').setValue(jsonData["measure"]![0]["range_array"]);
worksheet.getRangeByName('c1').setText('Force Array');
worksheet.getRangeByName('C2').setValue(jsonData["measure"]![0]["force_array"]);
worksheet.getRangeByName('A3').setValue(jsonData["measure"]![1]["count"]);
worksheet.getRangeByName('B3').setValue(jsonData["measure"]![1]["range_array"]);
worksheet.getRangeByName('C3').setValue(jsonData["measure"]![1]["force_array"]);
// Save the workbook to a file
// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
await saveAndLaunchFile(bytes, 'Measure.xlsx');
}
}
Future<void> saveAndLaunchFile(List<int> bytes, String fileName) async {
String? path;
final Directory directory = await getApplicationSupportDirectory();
path = directory.path;
final File file = File(Platform.isWindows ? '$path\\$fileName' : '$path/$fileName');
await file.writeAsBytes(bytes, flush: true);
await Process.run('start', <String>['$path\\$fileName'], runInShell: true);
}