Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9707

C#で会計データのコンバートを行う。

$
0
0
はじめに 汎用形式の会計データをコンバートしてインポートする機会があって、 C#で会計データの変換プログラムを作成しました。 要点 汎用形式の会計データはCSV形式で表題行、明細行、日次合計行、月度合計行があり、表題行と明細行のみを抽出して、表題業の列名を使って連想配列に格納し、順番を変えてテキストファイルとして出力します。今回は弥生データにコンバートしました。 発生した問題点 csvデータが文字化け 簡単に対処できると思いましたが、csvデータが文字化けしました。ファイルがShift-jis形式だったのでストリームでエンコーディングを指定して読めるようになりました。 ※必ず元データはバックアップを取っておいてください。 プログラムコード(一部抜粋) using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ReadKaikeiDataConsoleApp { class Program { static void Main(string[] args) { FileStream f = new FileStream("C:\\Users\\user\\Documents\\Yayoi\\Exchange\\テストデータ.txt", FileMode.Open); FileStream fout = new FileStream("C:\\Users\\user\\Documents\\Yayoi\\Exchange\\インポートデータ.txt", FileMode.OpenOrCreate); StreamReader r = new StreamReader(f, Encoding.GetEncoding("Shift_jis")); StreamWriter w = new StreamWriter(fout, Encoding.GetEncoding("Shift_jis")); string[] headerLine = null; while (r.EndOfStream == false) { var line = r.ReadLine(); if (line == "") break; Hashtable ht = new Hashtable(); string[] strArray = line.Trim().Split(','); if (strArray[0] == "\"[表題行]\"") { headerLine = strArray; } else if (strArray[0] == "\"[明細行]\"") { if (strArray.Length != 29) { System.Console.WriteLine("明細行エラー"); break; } for (int i = 0; i < strArray.Length; i++) ht[headerLine[i]] = strArray[i]; //System.Console.WriteLine(line); string str = ""; str = "\"2000\"" + "," + ht["\"伝票No.\""] + ","; str += ht["\"決算\""] + ","; ... str += ht["\"調整\""]; System.Console.WriteLine(str); w.WriteLine(str); } } r.Close(); w.Close(); f.Close(); fout.Close(); } } } 結果 うまく変換できました。 参考

Viewing all articles
Browse latest Browse all 9707

Trending Articles