简体   繁体   English

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

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

in iphone app, I want to export a SQLite database file to CSV file . 在iPhone应用程序中, 我想将SQLite数据库文件导出到CSV文件 (later i attach this file in mail) (后来我将此文件附加到邮件中)

that too in a table format ( or exel-sheet format) 也以表格格式(或exel-sheet格式)

I have tried the following logic but it is not like a table format and huge differences in column alignments. 尝试了以下逻辑,但是它不像表格式和列对齐方式存在巨大差异。

storedataBaseArray = [DataBase selectAllFromDB]; 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"];

DataBase File: 数据库文件:

在此处输入图片说明

Required Format: 所需格式:

在此处输入图片说明

My Out Put CSV file: 我的输出CSV文件:

在此处输入图片说明

Could any one suggest me how to do this 有人可以建议我怎么做吗

There is no need for using multiple \\t s and , s at the same time. 有没有必要使用多个\\t S和, S的方式同时进行。

You can simplify your format like this: 您可以像这样简化格式:

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

In any case, you will see a correct alignment only if you open the resulting file in a suitable program, eg, Excel (and also choose the correct import options). 在任何情况下,只有在适当的程序(例如Excel)中打开结果文件后,您才能看到正确的对齐方式(并选择正确的导入选项)。 A text editor will not usually display your CSV data in a tabular format. 文本编辑器通常不会以表格格式显示CSV数据。 Aside from the way your text editor displays it, the file format is fine. 除了您的文本编辑器显示方式外,文件格式还不错。

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

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