Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS应用程序生成的.CSV文件在Windows中不可读_Ios_Objective C_Windows_Macos_Csv - Fatal编程技术网

iOS应用程序生成的.CSV文件在Windows中不可读

iOS应用程序生成的.CSV文件在Windows中不可读,ios,objective-c,windows,macos,csv,Ios,Objective C,Windows,Macos,Csv,我已经使用Obj C在我的应用程序中创建了一个.csv文件,并将其附加到一封发送电子邮件中。所有这些都可以很好地工作,当使用OSX打开文件附件时,文件名会如我所期望的那样读取;“example.csv” 但是,当我在windows中尝试此操作时,文件扩展名不再可见,并且“文件不可读”。当我更改文件名并在文件末尾添加.csv时,它变得可读 将附件下载到windows计算机后,文件扩展为什么会丢失? 这里是我定义“文件路径”的地方 以下是用于生成.csv文件的代码 // Creates a temp

我已经使用Obj C在我的应用程序中创建了一个.csv文件,并将其附加到一封发送电子邮件中。所有这些都可以很好地工作,当使用OSX打开文件附件时,文件名会如我所期望的那样读取;“example.csv”

但是,当我在windows中尝试此操作时,文件扩展名不再可见,并且“文件不可读”。当我更改文件名并在文件末尾添加.csv时,它变得可读

将附件下载到windows计算机后,文件扩展为什么会丢失?

这里是我定义“文件路径”的地方

以下是用于生成.csv文件的代码

// Creates a temporary GPS object that we will use to save our database as a .CSV file.
    GPS *saveGPS = [[GPS alloc] init];
    @synchronized(FilePath) {
        [[NSFileManager defaultManager] createFileAtPath:FilePath contents:nil attributes:nil];
        // Creates a file handler which will allow us to write to our file.
        NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:FilePath];
        // Creates and writes the first line to our CSV file, which tells the program reading it what the column titles are.
        NSString *csvTitleString =@"Source/Receiver, Latitude, Longitude";
        [myHandle writeData:[csvTitleString dataUsingEncoding:NSUTF8StringEncoding]];
        // Creates initializes another string object which will hold each line we want to write.
        NSString *csvString = [[NSString alloc] init];
        // Declares an array and fills it with all GPS objects found in our Database.
        NSArray *allGPS = [[NSArray alloc]initWithArray:[database getAllbyProposal:proposalNumber]];
        // While the current index value is less than the length of the array write the GPS values into our file then take a new line.
        for(int i=0;i <= allGPS.count;i++){
            if(i < allGPS.count){
                saveGPS = [allGPS objectAtIndex:i];
                csvString = [NSString stringWithFormat:@"\n %@ %d, %@, %@", [saveGPS sourceReceiver], [[saveGPS positionNo] intValue], [saveGPS latitude], [saveGPS longitude]];
                [myHandle seekToEndOfFile];
                [myHandle writeData:[csvString dataUsingEncoding:NSUTF8StringEncoding]];
            }
            else if (i == allGPS.count){
                @synchronized(FilePath) {
                    // Checks if the device can send email.
                    if([MFMailComposeViewController canSendMail]){
                        // Sets the subject to data from (our current proposal number).
                        [mail setSubject:[NSString stringWithFormat:@"Data from %@", proposalNumber]];
                        [mail setMessageBody:@"Please see the attached .CSV file." isHTML:NO];
                        // Finds the .CSV file we just saved, and attaches it to the email.
                        NSData *myattachment = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@", FilePath]];
                        [mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];
                        // Opens up the email screen.
                        [self presentViewController:mail animated:YES completion:NULL];
                    }
                    else
                    {
                        // Creates a popup window to inform the user that their email wasn't able to send.
                        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                       message:@"Unable to send email. Have you set up a mail account on this device?"
                                                                                preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction* dismissAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}];
                        alert.view.tintColor = [UIColor orangeColor];
                        [alert addAction:dismissAction];
                        [self presentViewController:alert animated:YES completion:nil];
                    }
                }
            }
        }
    }
//创建一个临时GPS对象,用于将数据库保存为.CSV文件。
GPS*saveGPS=[[GPS alloc]init];
@已同步(文件路径){
[[NSFileManager defaultManager]createFileAtPath:FilePath内容:nil属性:nil];
//创建一个允许我们写入文件的文件处理程序。
NSFileHandle*myHandle=[NSFileHandle fileHandleForWritingAtPath:FilePath];
//创建并将第一行写入我们的CSV文件,它告诉读取它的程序列标题是什么。
NSString*csvTitleString=@“源/接收器,纬度,经度”;
[myHandle writeData:[CSVTitleStringDataUsingEncoding:NSUTF8StringEncoding];
//创建并初始化另一个字符串对象,该对象将保存我们要写入的每一行。
NSString*csvString=[[NSString alloc]init];
//声明一个数组并用数据库中找到的所有GPS对象填充它。
NSArray*allGPS=[[NSArray alloc]initWithArray:[数据库getAllbyProposal:proposalNumber]];
//当当前索引值小于数组长度时,将GPS值写入我们的文件,然后换行。

对于(int i=0;i您正在使用扩展名“.xls”,而不是csv。请使用“.csv”作为您的扩展名。

在这一行中:

[mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];
您正在设置mime类型,但文件名不包含扩展名。Mac OS X足够智能,可以通过查看mime类型和可能的文件内容来确定要做什么。Windows没有。Windows更依赖于实际的文件扩展名


将文件扩展名添加到附加到电子邮件的文件名中。

这是一个错误,为了解决此问题,我尝试使用xls库编写.xls而不是.csv,但忘记将其更改回去,谢谢提醒。请注意,iOS对换行使用*nix约定,即在csv的每一行末尾都有一个LF(换行)。Windows希望使用CR LF(回车,换行)。这可能会导致以后导入数据时出现问题。感谢您的帮助,犯了这个错误后,感觉非常愚蠢!
[mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];