Menu

Export Excel : Value start with '+'

Help
boly38
2006-10-03
2012-10-09
  • boly38

    boly38 - 2006-10-03

    Hi everybody,

    I want to export from Java/JSP/Displaytag to Excel format. Some values of my array start with '+' (or other Excel restricted characters like '=', ...).

    When i do it, Excel replace the value by "#NAME?".

    When i try to add the "'" before my string value, all rows got a "'" (it's not what i want).

    When i try to add the '"' before and after my string value, all rows have it like this : "<my value>" (it's not what i want).

    How can we do it ?

    I search in the documentation but dont found it.
    Thanks for your answer.
    Boly

     
    • boly38

      boly38 - 2006-10-04

      it's OK, i found an alternate way to display string values starting with a special char :

      => by replacing <value> to ="<value>".

      In JSP, i declare a decorator like this :

      &lt;display:column title=&quot;MyColumn&quot;  property=&quot;val&quot; decorator=&quot;com.blahblahblah.util.StringExcelExportDisplayTagWrapper&quot;/&gt;
      

      And this is the source of my decorator (StringExcelExportDisplayTagWrapper.java):

      import java.util.regex.Pattern;

      import javax.servlet.jsp.PageContext;

      import org.apache.log4j.Logger;
      import org.displaytag.decorator.DisplaytagColumnDecorator;
      import org.displaytag.exception.DecoratorException;
      import org.displaytag.properties.MediaTypeEnum;

      public class StringExcelExportDisplayTagWrapper implements DisplaytagColumnDecorator {
      final static Logger log = Logger.getLogger(StringExcelExportDisplayTagWrapper.class);

      public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) throws DecoratorException {
          String s = (String) columnValue;
          if ((s != null) &amp;&amp; s.length()&gt;0) {
              if (!Pattern.matches(&quot;[a-zA-Z]&quot;, s.substring(0,1))) {
                  log.debug(&quot;decorate(...) : Cell [&quot; + s + &quot;] contain special char =&gt; replaced by [=\&quot;&quot; + s + &quot;\&quot;]&quot;);
                  s = (Object) &quot;=\&quot;&quot; + s + &quot;\&quot;&quot;;
              }
          } 
          return s;
      }
      

      }

      Regards

      Boly

       

Log in to post a comment.

MongoDB Logo MongoDB