2012-09-12 54 views
0

iphone应用程序,我想将SQLite数据库文件导出为CSV文件。 (后来才将该文件附加在邮件)如何将SQLite文件导出为CSV格式的文件(excel表单样式)

太以表格形式(或EXEL表格式)

我已经试图以下逻辑,但它不像一个表格式和巨大列对齐中的差异。

storedataBaseArray = [DataBase selectAllFromDB];

NSString *[email protected]"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"]; 

数据库文件:

enter image description here

格式要求:

enter image description here

我出把CSV文件:

enter image description here

可以在任何一个建议我如何做到这一点

+0

CSV邮件附件不会以表格格式显示。使用

标签生成额外的html附件。 – melfar

回答

1

没有必要在同一使用多个\t S和,小号时间。

您可以简化您的格式是这样的:

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

在任何情况下,你会看到只有当你打开一个合适的程序生成的文件一个正确的定位,如Excel文件(并选择正确的导入选项)。文本编辑器通常不会以表格格式显示您的CSV数据。 除了你的文本编辑器显示它的方式,文件格式没问题。

+0

是的,你是对的。我也是在上面的代码的帮助下做的。但在这里我有一个问题,如果我们在任何列中都有大字符串,那么我们如何才能将它与相同列中的下一行进行比较。其实我陷入了这个问题。任何想法? – Anju

相关问题