【问题标题】:.CSV files generated by iOS application not readable in WindowsiOS 应用程序生成的 .CSV 文件在 Windows 中不可读
【发布时间】:2016-07-09 04:51:33
【问题描述】:

我使用 Obj C 在我的应用程序中创建了一个 .csv 文件,并将其附加到发送的电子邮件中。所有这些都可以正常工作,当使用 OSX 打开文件附件时,文件名会按照我的预期读取; “example.csv”。

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

为什么附件下载到windows电脑后文件扩展名丢失了?

这里是我定义“FilePath”的地方;

NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Jobs"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
FilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/Jobs/%@.csv",proposalNumber]];

这是用于生成 .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];
                    }
                }
            }
        }
    }

【问题讨论】:

    标签: ios objective-c windows macos csv


    【解决方案1】:

    在这一行:

    [mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];
    

    您正在设置 mime 类型,但文件名不包含扩展名。 Mac OS X 足够智能,可以通过查看 mime 类型和可能的文件内容来确定要做什么。窗户不是。 Windows 更依赖于实际的文件扩展名。

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

    【讨论】:

    • 请注意,iOS 对换行使用 *nix 约定,即它以 LF(换行)结束 csv 的每一行。 Windows 需要 CR LF(回车,换行)。这可能会导致稍后导入数据时出现问题。
    • 感谢您的帮助,犯了这个错误感觉很愚蠢!
    【解决方案2】:

    您使用的是扩展名“.xls”,而不是 csv。使用“.csv”作为您的扩展名。

    【讨论】:

    • 搞错了,为了解决这个问题,我尝试了一个xls库来写一个.xls而不是.csv,结果忘记改回来了,谢谢提醒。
    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多