美文网首页
word转Excel(ios)

word转Excel(ios)

作者: 1234yws | 来源:发表于2018-10-10 09:33 被阅读14次

    表格转成word,ios代码
    1、获取word文件转成Nsstring,通过webView

    NSString *path = [[NSBundle mainBundle] pathForResource:@"word" ofType:@"docx"];
        NSURL *url = [NSURL fileURLWithPath:path];
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    
    {
        NSString *strings = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];
        ResultViewController *ov = [[ResultViewController alloc]init];
           NSString * address = strings;
       address = [address stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
        address = [address stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        address = [address stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        ov.str = address;
        [self.navigationController pushViewController:ov animated:YES];
    }
    

    2、处理字符串

        NSArray *array = [self.str componentsSeparatedByString:@"案例编号:-0"]; //从字符A中分隔成2个元素的数组
    //有多少个该分类
        NSArray *smallArray = [array subarrayWithRange:NSMakeRange(1, 8)];
        NSMutableArray *textArr = [NSMutableArray array];
        for (int i = 0; i < smallArray.count; i++) {
            NSString *s = smallArray[i];
            NSArray *array = [s componentsSeparatedByString:@"测试概述"];
            NSArray *arrays = [array.lastObject componentsSeparatedByString:@"测试结果"];
            [textArr addObject:arrays.firstObject];
        }
        // 创建存放XLS文件数据的数组,Excel行
        NSMutableArray  *xlsDataMuArr = [[NSMutableArray alloc] init];
        [xlsDataMuArr addObject:@"执行人员"];
        [xlsDataMuArr addObject:@"客户端"];
        [xlsDataMuArr addObject:@"案例编号"];
        [xlsDataMuArr addObject:@"测试概述"];
        [xlsDataMuArr addObject:@"测试结果"];
        [xlsDataMuArr addObject:@"输出"];
      //Excel列
        for (int i = 0; i < smallArray.count; i ++) {
            [xlsDataMuArr addObject:@"XXX"];
            [xlsDataMuArr addObject:@"iOS"];
            [xlsDataMuArr addObject:[NSString stringWithFormat:@"要素类-0%02d",i]];
            [xlsDataMuArr addObject:STRING(textArr[i])];
            [xlsDataMuArr addObject:@"XXX"];
            [xlsDataMuArr addObject:@"XXX"];
        }
        // 把数组拼接成字符串,连接符是 \t(功能同键盘上的tab键)
        NSString *fileContent = [xlsDataMuArr componentsJoinedByString:@"\t"];
        // 字符串转换为可变字符串,方便改变某些字符
        NSMutableString *muStr = [fileContent mutableCopy];
        // 新建一个可变数组,存储每行最后一个\t的下标(以便改为\n)
        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)];
            }
        }
        // 替换末尾\t
        for (NSUInteger i = 0; i < subMuArr.count; i ++) {
    #warning  下面的6是列数,根据需求修改
            if ( i > 0 && (i%6 == 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 = [path stringByAppendingPathComponent:@"/Documents/export.xls"];
        
        NSLog(@"文件路径:\n%@",filePath);
        // 生成xls文件
        [fileManager createFileAtPath:filePath contents:fileData attributes:nil];
    

    相关文章

      网友评论

          本文标题:word转Excel(ios)

          本文链接:https://www.haomeiwen.com/subject/snnuaftx.html