0

iPhoneアプリで、SQLiteデータベースファイルをCSVファイルにエクスポートしたい。(後でこのファイルをメールで添付します)

それも表形式(またはExcelシート形式)で

次のロジックを試しましたが、テーブル形式や列の配置の大きな違いとは異なります。

StoredataBaseArray=[データベースselectAllFromDB];

  NSString *CSVstring=@"SNO \t\t\t Index \t\t\t  Definition\n" ;

  NSString *CSVPath,*record;;

   NSString  *temporayCSV= @"" ;

  for(int i=0;i<storedataBaseArray.count ;i++)
  {


        record = [NSString stringWithFormat:@"%@, \t\t\t %@, \t\t\t %@, [ [storedContactsArray objectAtIndex:i] objectForKey"id"] ,  [ [storedContactsArray objectAtIndex:i] objectForKey"title"],  [ [storedContactsArray objectAtIndex:i] objectForKey"Definition"]];

   // NSString *role =[storedContactsArray objectAtIndex:i]  ;

    NSLog(@"%d",i);

    temporayCSV = [NSString stringWithFormat:@"%d  %@  \n ",(i+1),record];

       CSVstring = [CSVstring stringByAppendingFormat:temporayCSV];       
     NSLog(@"%@",CSVstring);

}

  CSVPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv", @"CSV_FormatedTable"]];

  //add our file to the path
  [fileManager createFileAtPath:CSVPath contents:[CSVstring dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

  NSData *rolesCSVData =[NSData dataWithContentsOfFile:CSVPath];
  [mailpage addAttachmentData:rolesCSVData mimeType:@"text/csv" fileName:@"CSV_FormatedTable.csv"];

データベースファイル:

ここに画像の説明を入力してください

必要な形式:

ここに画像の説明を入力してください

My Out Put CSVファイル:

ここに画像の説明を入力してください

誰かが私にこれを行う方法を提案できますか

4

1 に答える 1

1

\t複数のsと,sを同時に使用する必要はありません。

次のようにフォーマットを簡略化できます。

    record = [NSString stringWithFormat:@"%@, %@, %@", [ [storedContactsArray objectAtIndex:i] objectForKey"id"] ,  [ [storedContactsArray objectAtIndex:i] objectForKey"title"],  [ [storedContactsArray objectAtIndex:i] objectForKey"Definition"]];

いずれの場合も、Excelなどの適切なプログラムで結果のファイルを開いた場合にのみ正しい配置が表示されます(また、正しいインポートオプションを選択します)。テキストエディタは通常、CSVデータを表形式で表示しません。テキストエディタでの表示方法は別として、ファイル形式は問題ありません。

于 2012-09-12T10:14:30.907 に答える