-1

我正在使用核心数据和 libxl。每当我在 libxl 中实现核心数据字符串时,它都会给我错误“不兼容的指针类型将 nsstring 传递给 const char 类型的参数”

不知道为什么...

这是我的代码,namelabel.text 是核心数据 NSString,它产生错误不兼容的指针类型,将 nsstring 传递给 const char 类型的参数。

- (IBAction)createExcel:(id)sender
{
    NSLog(@"createExcel");

    BookHandle book = xlCreateBook(); // use xlCreateXMLBook() for working with xlsx files

    SheetHandle sheet = xlBookAddSheet(book, "Sheet1", NULL);

    xlSheetWriteStr(sheet, 1, 1, namelabel.text, 0);
    xlSheetWriteNum(sheet, 4, 1, 1000, 0);
    xlSheetWriteNum(sheet, 5, 1, 2000, 0);

    FontHandle font = xlBookAddFont(book, 0);
    xlFontSetColor(font, COLOR_RED);
    xlFontSetBold(font, true);
    FormatHandle boldFormat = xlBookAddFormat(book, 0);
    xlFormatSetFont(boldFormat, font);
    xlSheetWriteFormula(sheet, 6, 1, "SUM(B5:B6)", boldFormat);

    FormatHandle dateFormat = xlBookAddFormat(book, 0);
    xlFormatSetNumFormat(dateFormat, NUMFORMAT_DATE);
    xlSheetWriteNum(sheet, 8, 1, xlBookDatePack(book, 2011, 7, 20, 0, 0, 0, 0), dateFormat);

    xlSheetSetCol(sheet, 1, 1, 12, 0, 0);

    NSString *documentPath =
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename = [documentPath stringByAppendingPathComponent:@"insuranceclaim.xls"];

    xlBookSave(book, [filename UTF8String]);

    xlBookRelease(book);

    if (![MFMailComposeViewController canSendMail]) {
        //Show alert that device cannot send email, this is because an email account     hasn't been setup.
    }

    else {

        //**EDIT HERE**
        //Use this to retrieve your recently saved file

        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filename = [documentPath stringByAppendingPathComponent:@"insuranceclaim.xls"];

        //**END OF EDIT**

        NSString *mimeType = @"application/vnd.ms-excel"; //This should be the MIME type for els files. May want to double check.
        NSData *fileData = [NSData dataWithContentsOfFile:filename];
        NSString *fileNameWithExtension = @"insuranceclaim.xls"; //This is what you want the file to be called on the email along with it's extension:

        //If you want to then delete the file:
        NSError *error;
        if (![[NSFileManager defaultManager] removeItemAtPath:filename error:&error])
            NSLog(@"ERROR REMOVING FILE: %@", [error localizedDescription]);


        //Send email
        MFMailComposeViewController *mailMessage = [[MFMailComposeViewController alloc] init];
        [mailMessage setMailComposeDelegate:self];
        [mailMessage addAttachmentData:fileData mimeType:mimeType fileName:fileNameWithExtension];
        [self presentViewController:mailMessage animated:YES completion:nil];
    }


}


- (void)mailComposeControllerone:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}

@end
4

1 回答 1

8

转换为const char *尝试

const char *s = [namelabel.text UTF8String];

并将其传递到您需要的任何地方。

编辑

xlSheetWriteStr(sheet, 1, 1, [namelabel.text UTF8String], 0);
于 2014-07-08T21:10:43.470 回答