前提
Flutterでデスクトップアプリを作っています。
Excelファイルをアプリ画面にドラッグ&ドロップし、別フォーマットのExcelファイルに置き換えて出力します。
実現したいこと
- 元のExcelファイルの各セルの文字列は同じまま、別フォーマットのExcelファイルに置き換えて出力したい
使っているFlutterのライブラリはexcel(2.0.0-null-safety-3)です。
excel
発生している問題・エラーメッセージ
セルの値に漢字が含まれている場合、その漢字の読み仮名がカタカナ表記で語尾についてしまう。
(例)駐車場代 ⇒ 駐車場代チュウシャジョウダイ
該当のソースコード
dart
1import 'package:desktop_drop/desktop_drop.dart'; 2import 'package:flutter/material.dart'; 3import 'dart:io'; 4import 'package:excel/excel.dart'; 5import 'package:intl/intl.dart'; 6import 'package:cross_file/cross_file.dart'; 7import 'package:path/path.dart'; 8 9 10void main() { 11 runApp(const MyApp()); 12} 13 14class MyApp extends StatelessWidget { 15 const MyApp({Key? key}) : super(key: key); 16 17 18 Widget build(BuildContext context) { 19 return MaterialApp( 20 debugShowCheckedModeBanner: false, 21 home: Scaffold( 22 endDrawer: Drawer( 23 child: ListView.builder( 24 itemCount: 5, 25 itemBuilder: (BuildContext context, int index) { 26 return ListTile( 27 title: Text("Item $index"), 28 ); 29 }, 30 ), 31 ), 32 appBar: AppBar( 33 title: const Text('ドキュメント変換ツール(仮)'), 34 ), 35 body: Center( 36 child: Container( 37 child: Column( 38 mainAxisAlignment: MainAxisAlignment.center, 39 children: <Widget>[ 40 Wrap( 41 direction: Axis.horizontal, 42 runSpacing: 8, 43 spacing: 8, 44 children: const [ExmapleDragTarget()], 45 ), 46 ], 47 ), 48 ), 49 ) 50 )); 51 } 52} 53 54class ExmapleDragTarget extends StatefulWidget { 55 const ExmapleDragTarget({Key? key}) : super(key: key); 56 57 58 _ExmapleDragTargetState createState() => _ExmapleDragTargetState(); 59} 60 61class _ExmapleDragTargetState extends State<ExmapleDragTarget> { 62 final List<XFile> _list = []; 63 64 bool _dragging = false; 65 66 Offset? offset; 67 68// ファイルをドロップするエリアの実装 69 70Widget build(BuildContext context) { 71 return DropTarget( 72 onDragDone: (detail) async { 73 setState(() { 74 _list.clear(); 75 _list.addAll(detail.files); 76 }); 77 78 for (final file in detail.files) { 79 debugPrint(' ${file.path} ${file.name}' 80 ' ${await file.lastModified()}' 81 ' ${await file.length()}' 82 ' ${file.mimeType}'); 83 int extIndex = file.name.lastIndexOf('.') + 1; 84 85 if (file.name.substring(extIndex, file.name.length) == 86 'xlsx') { 87 var bytes; 88 89 bytes = File(file.path).readAsBytesSync(); 90 var excel = Excel.decodeBytes(bytes); 91 92 // 行ごとのループ 93 for (var i = 0; i < excel.tables['Sheet1']!.rows.length; i++) { 94 var rowData = excel.tables['Sheet1']!.rows[i]; 95 // 列ごとのループ 96 for (var j = 0; j < rowData.length; j++) { 97 Data? data = rowData[j]; 98 if (data != null) { 99 debugPrint(data.value); // 表示確認用 100 } 101 } 102 } 103 } 104 } 105 } 106} 107
試したこと
VSCodeで上記ソースの変数"excel"について、ウォッチ式で見た図です。
decodeBytesが原因な気がしていますが、解決方法が分かっていません。
dart
1 bytes = File(file.path).readAsBytesSync(); 2 var excel = Excel.decodeBytes(bytes);
補足情報(FW/ツールのバージョンなど)
- Flutter 3.4.0-19.0.pre.192
- Framework • revision 4930444f4a
- Dart 2.19.0(build 2.19.0-183.0.dev)
- Engine • revision f72107f457
- DevTools 2.17.0
- Flutterライブラリ excel 2.0.0-null-safety-3

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。