objective-c - Objective-C - 如何打开 .csv 文件?
问题描述
我是编程新手。不幸的是,我没有 C 或 Objective-C 的经验。我的问题很简单。
如何使用 MS Excel 从打开的面板打开 .csv 文件?如何将 .csv 文件导出到数据库中?
感谢您的时间和帮助。
#import "AppDelegate.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (IBAction)Browse:(id)sender {
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:[NSArray arrayWithObjects:@"csv", nil]];
// Permet seulement d'ouvrir un fichier .csv
[openPanel setAllowsMultipleSelection:YES];
[openPanel setDirectoryURL:[NSURL URLWithString:[NSHomeDirectory() stringByAppendingPathComponent:@"Bureau"]]];
// Ouvre le répertoire Bureau en automatique
if([openPanel runModal] == NSModalResponseOK)
[[NSWorkspace sharedWorkspace] openFile:
withApplication:@"Microsoft Excel"];
NSLog(@"Fichier ouvert : %@", [openPanel URL]);
}
}
@end
解决方案
请试试这个片段。请记住,您可能需要将每一列作为一个行值来处理。
NSMutableArray *colA = [NSMutableArray array];
NSMutableArray *colB = [NSMutableArray array];
NSString* fileContents = [NSString stringWithContentsOfURL:@"microsoft-excel.csv"];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (NSString *row in rows){
NSArray* columns = [row componentsSeparatedByString:@","];
[colA addObject:columns[0]];
[colB addObject:columns[1]];
}
推荐阅读
- java - 使用 Jolt 变形时反击
- firebase - 为什么 firebase_core 和 firebase_messaging 之间的颤振版本解决失败?
- r - 将字符串向量转换为符号
- javascript - 当 vuetify 的 v-card 有关联的链接时,从 v-card 中移除点击效果
- javascript - json文件中的国家如何显示
- powershell - 使用 PowerShell 获取 IIS 8.5 日志文件信息
- r - dplyr mutate ifelse 返回组的第一个值而不是逐行
- r - 如何在R中按数字对列进行排序?
- python - 覆盖模拟对象的“is”运算符
- amazon-web-services - ECS服务和EC2实例的CPU利用率差异