0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++builderでCSVを読み込んでTStringGridに表示

Last updated at Posted at 2015-04-22

CSVをTStringListで読み込んで、1行ずつ別のTStringListで読み込む。

FireMonkey.h
///std::unique_ptr用(C++11対応の環境用)
# include <memory>
///
void __fastcall FunctionReadCSV(const UnicodeString &var_file_name,
	TStringGrid *var_string_grid);
FireMonkey.cpp
void __fastcall TForm1::FunctionReadCSV(const UnicodeString &var_file_name,
	TStringGrid *var_string_grid) {
	std::unique_ptr<TStringList>var_list(new TStringList); // csv読み込み用
	std::unique_ptr<TStringList>var_temp(new TStringList); // var_listから1行読み込み
	if (FileExists(var_file_name)) { // ファイルを確認
		var_list->LoadFromFile(var_file_name); // ファイルを読み込み
		var_string_grid->RowCount = var_list->Count; // 行数を設定。
		for (int i = 0; i < var_list->Count; i++) {
			UnicodeString tmp_c = StringReplace(var_list->Strings[i], L"\\,",
				L"&comma;", TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
			// 文字列に明示的なカンマ \, が有るときは一旦別の文字列にしておく。
			var_temp->CommaText = tmp_c; // commaTextで区分け
			for (int i2 = 0; i2 < 4; i2++) { // 必要な列数に応じて4の部分を変更
				tmp_c = var_temp->Strings[i2];
				tmp_c = StringReplace(tmp_c, L"&comma;", L",",
					TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
				// カンマを戻す。この例では「\,」を「,」にしている点に注意。
				var_string_grid->Cells[i2][i] = tmp_c;
			}
		}
	}
}

書き出しはこちら
C++builderでTStringGridからCSVを書き出す

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?