export xlsx
This commit is contained in:
@ -3,7 +3,9 @@ import 'dart:io';
|
||||
import 'package:excel/excel.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class OrderList extends StatefulWidget {
|
||||
const OrderList({super.key});
|
||||
@ -16,8 +18,10 @@ class _OrderListState extends State<OrderList> {
|
||||
final List lst = [];
|
||||
List<String> headers = [];
|
||||
List<List<dynamic>> data = [];
|
||||
|
||||
late Excel _excel;
|
||||
ScrollController scrollController = ScrollController();
|
||||
final TextEditingController _logComController =
|
||||
TextEditingController(text: "");
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -31,7 +35,11 @@ class _OrderListState extends State<OrderList> {
|
||||
importExcelFile();
|
||||
},
|
||||
icon: const Icon(Icons.add_circle)),
|
||||
IconButton(onPressed: () {}, icon: const Icon(Icons.output))
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
exportExcelFile();
|
||||
},
|
||||
icon: const Icon(Icons.output))
|
||||
],
|
||||
),
|
||||
body: Container(
|
||||
@ -55,11 +63,11 @@ class _OrderListState extends State<OrderList> {
|
||||
columns: headers.map((el) {
|
||||
return DataColumn(label: Text(el));
|
||||
}).toList(),
|
||||
rows: data.map((row) {
|
||||
rows: data.asMap().entries.map((row) {
|
||||
return DataRow(
|
||||
cells: row.asMap().entries.map((col) {
|
||||
String col_title = headers[col.key];
|
||||
return DataCell(col_title == "物流名称"
|
||||
cells: row.value.asMap().entries.map((col) {
|
||||
String colTitle = headers[col.key];
|
||||
return DataCell(colTitle == "物流名称"
|
||||
? Row(
|
||||
children: [
|
||||
Text("${col.value}"),
|
||||
@ -67,7 +75,9 @@ class _OrderListState extends State<OrderList> {
|
||||
// padding: EdgeInsets.all(2),
|
||||
iconSize: 12,
|
||||
onPressed: () {
|
||||
// Dialog()
|
||||
_logComController.text =
|
||||
data[row.key][col.key]
|
||||
.toString();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext
|
||||
@ -76,25 +86,52 @@ class _OrderListState extends State<OrderList> {
|
||||
// icon: const Icon(Icons.edit),
|
||||
title: const Text(
|
||||
"填写物流公司"),
|
||||
content: const Column(
|
||||
content: Column(
|
||||
mainAxisSize:
|
||||
MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
decoration:
|
||||
InputDecoration(
|
||||
const InputDecoration(
|
||||
labelText:
|
||||
"物流公司"),
|
||||
controller:
|
||||
_logComController,
|
||||
)
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
context.pop();
|
||||
},
|
||||
child: const Text(
|
||||
"取消")),
|
||||
ElevatedButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
String
|
||||
logisticsCom =
|
||||
_logComController
|
||||
.text;
|
||||
if (logisticsCom
|
||||
.isEmpty) {
|
||||
ScaffoldMessenger.of(
|
||||
context)
|
||||
.showSnackBar(const SnackBar(
|
||||
content:
|
||||
Text("请输入物流公司")));
|
||||
return;
|
||||
}
|
||||
final _data =
|
||||
data;
|
||||
data[row.key][col
|
||||
.key] =
|
||||
logisticsCom;
|
||||
setState(() {
|
||||
data = _data;
|
||||
});
|
||||
context.pop();
|
||||
},
|
||||
child: const Text(
|
||||
"确认")),
|
||||
],
|
||||
@ -103,10 +140,29 @@ class _OrderListState extends State<OrderList> {
|
||||
icon: const Icon(Icons.edit))
|
||||
],
|
||||
)
|
||||
: col_title == "物流信息"
|
||||
: colTitle == "物流信息"
|
||||
? ElevatedButton(
|
||||
onPressed: () {
|
||||
context.push("/logistics");
|
||||
String orderNumber =
|
||||
data[row.key][2].toString();
|
||||
String orderPayWay =
|
||||
data[row.key][15].toString();
|
||||
if (orderPayWay.isEmpty ||
|
||||
orderNumber.isEmpty) {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(const SnackBar(
|
||||
content: Text("该行数据有误")));
|
||||
return;
|
||||
}
|
||||
context.pushNamed("logistics",
|
||||
queryParameters: {
|
||||
"orderNumber": data[row.key]
|
||||
[2]
|
||||
.toString(),
|
||||
"orderPayWay": data[row.key]
|
||||
[15]
|
||||
.toString(),
|
||||
});
|
||||
},
|
||||
child: const Text("填写"),
|
||||
)
|
||||
@ -130,18 +186,22 @@ class _OrderListState extends State<OrderList> {
|
||||
File file = File(result.files.single.path!);
|
||||
var bytes = file.readAsBytesSync();
|
||||
Excel excel = Excel.decodeBytes(bytes);
|
||||
_excel = excel;
|
||||
String tableKey = excel.tables.keys.first;
|
||||
List<String> _headers = excel.tables[tableKey]?.rows.first.map((el) {
|
||||
return el?.value.toString() ?? "";
|
||||
}).toList() ??
|
||||
[];
|
||||
List<List<dynamic>> _data =
|
||||
excel.tables[tableKey]?.rows.sublist(1).map((row) {
|
||||
return row.map((col) {
|
||||
return col?.value ?? "";
|
||||
}).toList();
|
||||
}).toList() ??
|
||||
[];
|
||||
List<List<dynamic>> _data = excel.tables[tableKey]?.rows
|
||||
.where((el) => (el.first?.value?.toString() ?? "").isNotEmpty)
|
||||
.toList()
|
||||
.sublist(1)
|
||||
.map((row) {
|
||||
return row.map((col) {
|
||||
return col?.value ?? "";
|
||||
}).toList();
|
||||
}).toList() ??
|
||||
[];
|
||||
setState(() {
|
||||
headers = _headers;
|
||||
data = _data;
|
||||
@ -151,4 +211,30 @@ class _OrderListState extends State<OrderList> {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
exportExcelFile() async {
|
||||
data.asMap().entries.forEach((row) {
|
||||
row.value.asMap().entries.forEach((col) {
|
||||
int rowIndex = row.key + 1;
|
||||
int colIndex = col.key;
|
||||
if (colIndex == 11 || colIndex == 12) {
|
||||
_excel.updateCell(
|
||||
"订单列表",
|
||||
CellIndex.indexByColumnRow(
|
||||
columnIndex: colIndex, rowIndex: rowIndex),
|
||||
TextCellValue(col.value.toString()));
|
||||
}
|
||||
});
|
||||
});
|
||||
var fileBytes = _excel.save();
|
||||
// var directory = await getApplicationDocumentsDirectory();
|
||||
File(
|
||||
"C:\\Users\\ayaya\\Desktop\\导出订单${DateTime.now().millisecondsSinceEpoch}.xlsx")
|
||||
..createSync(recursive: true)
|
||||
..writeAsBytesSync(fileBytes!);
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text("导出文件成功"),
|
||||
backgroundColor: Colors.green,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user