繁体   English   中英

如何将SQLite文件导出为表格格式的Excel文件(Excel工作表样式)

[英]How to export SQLite file into CSV file with table format (excel sheet style)

在iPhone应用程序中, 我想将SQLite数据库文件导出到CSV文件 (后来我将此文件附加到邮件中)

也以表格格式(或exel-sheet格式)

尝试了以下逻辑,但是它不像表格式和列对齐方式存在巨大差异。

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"];

数据库文件:

在此处输入图片说明

所需格式:

在此处输入图片说明

我的输出CSV文件:

在此处输入图片说明

有人可以建议我怎么做吗

有没有必要使用多个\\t S和, S的方式同时进行。

您可以像这样简化格式:

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

在任何情况下,只有在适当的程序(例如Excel)中打开结果文件后,您才能看到正确的对齐方式(并选择正确的导入选项)。 文本编辑器通常不会以表格格式显示CSV数据。 除了您的文本编辑器显示方式外,文件格式还不错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM