如何在ix的libxl中嵌入公式

时间:2014-08-30 18:50:34

标签: ios xcode excel formulas libxl

我希望能够为libxl添加一些简单的公式,如加法和减法,只是不知道该怎么做。只是想知道如何添加简单的加法和减法公式。

这是我的代码:

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

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

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

这些是我希望公式更改的数字。(100& 150)

  xlSheetWriteStr(sheet, 1, 0, 100, 0);
  xlSheetWriteStr(sheet, 1, 0, 150, 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 =   self.personFirstnameTextField.text; //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]);


    }


}

2 个答案:

答案 0 :(得分:0)

添加公式很简单。以下是LibXl for iOS下载附带的示例应用程序中的相关行:

xlSheetWriteFormula(sheet, 6, 1, "SUM(B5:B6)", boldFormat);

看起来唯一重要的是要记住公式需要是C字符串,而不是Objective-C字符串。以下是其文档中的相关段落:http://www.libxl.com/spreadsheet.html#writeFormula

答案 1 :(得分:0)

对于抱怨我们应该阅读文档"的人来说,主要的问题是Objective-C中没有示例,仅适用于普通的旧C。

这对我有用,我建立一个公式字符串,在D列中添加两行,然后应用它。

NSString *formula=[NSString stringWithFormat:@"SUM(D%d:D%d)",startRow,endrow]; 
xlSheetWriteFormula(sheet, row, 2, [formula cStringUsingEncoding: NSUTF8StringEncoding], cellFormat);

cellFormat是我为所需的字体和边框效果定义的格式。

我猜猜人们遇到的问题是使用字符串编码。

我会敦促任何购买了LibXL许可证的人鼓励开发人员为Objective-C开发示例代码。我们已经走了好几年了,没有它。