最近做一个项目,需要把时延以表格的形式输出,方便后续的数据可视化。
```objectivec
-(void)xlsData{
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
NSMutableArray *xlsDataMuArr = [[NSMutableArray alloc] init];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
for (int i = 1; i < 100; i++) {
[xlsDataMuArr addObject:@"0"];
[xlsDataMuArr addObject:@"1"];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
[xlsDataMuArr addObject:@" "];
}
NSString *fileContent = [xlsDataMuArr componentsJoinedByString:@"\t"];
NSMutableString *muStr = [fileContent mutableCopy];
NSMutableArray *subMuArr = [NSMutableArray array];
for (int i = 0; i < muStr.length; i ++) {
NSRange range = [muStr rangeOfString:@"\t" options:NSBackwardsSearch range:NSMakeRange(i, 1)];
if (range.length == 1) {
[subMuArr addObject:@(range.location)];
}
}
for (NSUInteger i = 0; i < subMuArr.count; i ++) {
if ( i > 0 && (i%16 == 0) ) {
[muStr replaceCharactersInRange:NSMakeRange([[subMuArr objectAtIndex:i-1] intValue], 1) withString:@"\n"];
}
}
NSFileManager *fileManager = [[NSFileManager alloc]init];
NSData *fileData = [muStr dataUsingEncoding:NSUTF16StringEncoding];
// NSString *path = NSHomeDirectory();
NSString *filePath = @"/Users/zags k/Desktop/exportVideo.xls";
NSLog(@"文件路径:\n%@",filePath);
[fileManager createFileAtPath:filePath contents:fileData attributes:nil];
}
```
这个代码太好用了,直接粘贴就可以,内容自己填!再也不用手动输入数据了。