Clickable link in xls with JXLS

1k views Asked by At

I`m using JXLS v1.0 for creating xls files in my ios-project, and i can not understand how to make a clickable link in some cell in the spreadsheet, is there any way to make this with JXLS, or maybe there is some other free xls framework exists

3

There are 3 answers

0
Oleksandr On BEST ANSWER

Ok, i have found the way to resolve link issue for JXLS ios framework.

The problem was in Objective-C wrapper, there is no methods to add a link in some cell in this wrapper, but there is "hyperLink" method in worksheet class, so the decision was to make a wrapper method in class JXLSWorkSheet(which you can use in your Objective-C code). Implementation looks like: JXLSWorkSheet.h: just add this method in header

    - (void)addLink:(JXLSCell *)cell link:(NSString *)link;

JXLSWorkSheet.m: add implementation body before end statement

- (void)addLink:(JXLSCell *)cell link:(NSString *)link
  {
    cell_t          *cl;
    unichar         *uniName;
    ustring         uniStr;
    size_t          len;

    len = [link length];

    uniName = (unichar *)calloc(len+1, sizeof(unichar));
    [link getCharacters:uniName];
    uniStr.assign(uniName);
    free(uniName);

    cl = (xlslib_core::cell_t *)cell.cell;

    _workSheet->hyperLink(cl, uniStr, uniName);

}

In my implementation i use the same text as label and the link, add one more parameter to this method, if you want to use some other text as label of the hyperlink in spreadsheet.

The usage is very simple: you just take the cell you want to contain the url and add link to it. See listing below.

 JXLSCell *cell;
 cell = [workSheet setCellAtRow:rowCount column:1 toString:@"http://stackoverflow.com/"];
 [workSheet addLink:cell link:@"http://stackoverflow.com/"];

I hope it will help you in hyperlink creation in your spreadsheets.

1
Leonid Vysochyn On

If possible you should migrate to Jxls-2. In Jxls-2 the creation of the hyperlink is as easy as having the following markup in Excel template

${util.hyperlink(linkAddress,linkTitle)}

linkAddress and linkTitle are the names of the variables in Jxls context containing hyperlink URL and name. Check the following XlsCommentBuilderDemo example in jxls-demo project to see it in action.

With Jxls 1.x you should use formula approach to create Hyperlink. In your markup you should have something like this

$[HYPERLINK(A1,A2)]

Where in cell A1 and A2 you should have link address and link title. If they are dynamic you can also output them with jxls. So for example in A1 you can have ${link.url} and in A2 you can have ${link.title} where link is an object in your bean context with properties url and title. Sure you can use any cells instead of A1 and A2.

0
senior_russia On

This code is more correct:

    - (void)addLink:(JXLSCell *)cell link:(NSString *)link
{
    cell_t          *cl;
    unichar         *uniName;
    ustring         uniStr;
    size_t          len;

    len = [link length];

    /*uniName = (unichar *)calloc(len+1, sizeof(unichar));
    [link getCharacters:uniName];
    uniStr.assign(uniName);
    free(uniName);*/

    @autoreleasepool {
        uniName = (unichar *)[link cStringUsingEncoding:NSUnicodeStringEncoding];
        uniStr.assign(uniName);
    }

    cl = (xlslib_core::cell_t *)cell.cell;

    _workSheet->hyperLink(cl, uniStr, uniName);

}