Sophie

Sophie

distrib > Mageia > 2 > i586 > by-pkgid > f4a00488d376799785b0ada5da91fdf2 > files > 13

apache-poi-manual-3.8-1.mga2.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--*** This is a generated file.  Do not edit.  ***-->
<link rel="stylesheet" href="skin/tigris.css" type="text/css">
<link rel="stylesheet" href="skin/mysite.css" type="text/css">
<link rel="stylesheet" href="skin/site.css" type="text/css">
<link media="print" rel="stylesheet" href="skin/print.css" type="text/css">
<title>Frequently Asked Questions</title>
</head>
<body bgcolor="white" class="composite">
<!--================= start Banner ==================-->
<div id="banner">
<table width="100%" cellpadding="8" cellspacing="0" summary="banner" border="0">
<tbody>
<tr>
<!--================= start Group Logo ==================-->
<td width="50%" align="left">
<div class="groupLogo">
<a href="http://poi.apache.org"><img border="0" class="logoImage" alt="Apache POI" src="resources/images/group-logo.jpg"></a>
</div>
</td>
<!--================= end Group Logo ==================-->
<!--================= start Project Logo ==================--><td width="50%" align="right">
<div align="right" class="projectLogo">
<a href="http://poi.apache.org/"><img border="0" class="logoImage" alt="POI" src="resources/images/project-logo.jpg"></a>
</div>
</td>
<!--================= end Project Logo ==================-->
</tr>
</tbody>
</table>
</div>
<!--================= end Banner ==================-->
<!--================= start Main ==================-->
<table width="100%" cellpadding="0" cellspacing="0" border="0" summary="nav" id="breadcrumbs">
<tbody>
<!--================= start Status ==================-->
<tr class="status">
<td>
<!--================= start BreadCrumb ==================--><a href="http://www.apache.org/">Apache</a> | <a href="http://poi.apache.org/">POI</a><a href=""></a>
<!--================= end BreadCrumb ==================--></td><td id="tabs">
<!--================= start Tabs ==================-->
<div class="tab">
<span class="selectedTab"><a class="base-selected" href="index.html">Home</a></span> | <script language="Javascript" type="text/javascript">
function printit() {  
if (window.print) {
    window.print() ;  
} else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH="0" HEIGHT="0" CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
}
}
</script><script language="Javascript" type="text/javascript">
var NS = (navigator.appName == "Netscape");
var VERSION = parseInt(navigator.appVersion);
if (VERSION > 3) {
    document.write('  <a title="PRINT this page OUT" href="javascript:printit()">PRINT</a>');
}
</script> | <a title="PDF file of this page" href="faq.pdf">PDF</a>
</div>
<!--================= end Tabs ==================-->
</td>
</tr>
</tbody>
</table>
<!--================= end Status ==================-->
<table id="main" width="100%" cellpadding="8" cellspacing="0" summary="" border="0">
<tbody>
<tr valign="top">
<!--================= start Menu ==================-->
<td id="leftcol">
<div id="navcolumn">
<div class="menuBar">
<div class="menu">
<span class="menuLabel">Overview</span>
        
<div class="menuItem">
<a href="index.html">Home</a>
</div>
        
<div class="menuItem">
<a href="download.html">Download</a>
</div>
        
<div class="menuItem">
<a href="overview.html">Components</a>
</div>
        
<div class="menuItem">
<a href="text-extraction.html">Text Extraction</a>
</div>
        
<div class="menuItem">
<a href="encryption.html">Encryption support</a>
</div>
        
<div class="menuItem">
<a href="casestudies.html">Case Studies</a>
</div>
        
<div class="menuItem">
<a href="legal.html">Legal</a>
</div>
    
</div>
<div class="menu">
<span class="menuLabel">Help</span>
        
<div class="menuItem">
<a href="apidocs/index.html">Javadocs</a>
</div>
        
<div class="menuItem">
<span class="menuSelected">FAQ</span>
</div>
        
<div class="menuItem">
<a href="mailinglists.html">Mailing Lists</a>
</div>
        
<div class="menuItem">
<a href="http://issues.apache.org/bugzilla/buglist.cgi?product=POI">Bug Database</a>
</div>
        
<div class="menuItem">
<a href="changes.html">Changes Log</a>
</div>
    
</div>
<div class="menu">
<span class="menuLabel">Getting Involved</span>
        
<div class="menuItem">
<a href="subversion.html">Subversion Repository</a>
</div>
        
<div class="menuItem">
<a href="howtobuild.html">How To Build</a>
</div>
        
<div class="menuItem">
<a href="guidelines.html">Contribution Guidelines</a>
</div>
        
<div class="menuItem">
<a href="who.html">Who We Are</a>
</div>
    
</div>
<div class="menu">
<span class="menuLabel">Component APIs</span>
        
<div class="menuItem">
<a href="spreadsheet/index.html">Excel (SS=HSSF+XSSF)</a>
</div>
        
<div class="menuItem">
<a href="hwpf/index.html">Word (HWPF+XWPF)</a>
</div>
        
<div class="menuItem">
<a href="slideshow/index.html">PowerPoint (HSLF+XSLF)</a>
</div>
        
<div class="menuItem">
<a href="oxml4j/index.html">OpenXML4J (OOXML)</a>
</div>
        
<div class="menuItem">
<a href="poifs/index.html">OLE2 Filesystem (POIFS)</a>
</div>
        
<div class="menuItem">
<a href="hpsf/index.html">OLE2 Document Props (HPSF)</a>
</div>
        
<div class="menuItem">
<a href="hsmf/index.html">Outlook (HSMF)</a>
</div>
        
<div class="menuItem">
<a href="hdgf/index.html">Visio (HDGF)</a>
</div>
        
<div class="menuItem">
<a href="hmef/index.html">TNEF (HMEF)</a>
</div>
        
<div class="menuItem">
<a href="hpbf/index.html">Publisher (HPBF)</a>
</div>
    
</div>
<div class="menu">
<span class="menuLabel">Apache Wide</span>
        
<div class="menuItem">
<a href="http://www.apache.org/">Apache Software Foundation</a>
</div>
        
<div class="menuItem">
<a href="http://www.apache.org/licenses/">License</a>
</div>
        
<div class="menuItem">
<a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a>
</div>
        
<div class="menuItem">
<a href="http://www.apache.org/foundation/thanks.html">Thanks</a>
</div>
        
<div class="menuItem">
<a href="http://www.apache.org/security/">Security</a>
</div>
    
</div>
</div>
</div>
<form target="_blank" action="http://www.google.com/search" method="get">
<table summary="search" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img height="1" width="1" alt="" src="skin/images/spacer.gif" class="spacer"></td><td nowrap="nowrap">
                          Search Apache POI<br>
<input value="poi.apache.org" name="sitesearch" type="hidden"><input size="10" name="q" id="query" type="text"><img height="1" width="5" alt="" src="skin/images/spacer.gif" class="spacer"><input name="Search" value="GO" type="submit"></td><td><img height="1" width="1" alt="" src="skin/images/spacer.gif" class="spacer"></td>
</tr>
<tr>
<td colspan="3"><img height="7" width="1" alt="" src="skin/images/spacer.gif" class="spacer"></td>
</tr>
<tr>
<td class="bottom-left-thick"></td><td bgcolor="#a5b6c6"><img height="1" width="1" alt="" src="skin/images/spacer.gif" class="spacer"></td><td class="bottom-right-thick"></td>
</tr>
</table>
</form>
</td>
<!--================= end Menu ==================-->
<!--================= start Content ==================--><td>
<div id="bodycol">
<div class="app">
<div align="center">
<h1>Frequently Asked Questions</h1>
</div>
<div class="h3">
<a name="Questions"></a>
<div class="h3">
<h3>Questions</h3>
</div>
<ol>
<li>
<a name="faq-N10006-menu"></a><a href="#faq-N10006">
      My code uses some new feature, compiles fine but fails when live with a "MethodNotFoundException"
    </a>
</li>
<li>
<a name="faq-N10019-menu"></a><a href="#faq-N10019">
      My code uses the scratchpad, compiles fine but fails to run with a "MethodNotFoundException"
    </a>
</li>
<li>
<a name="faq-N10025-menu"></a><a href="#faq-N10025">
      I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/*something*"
    </a>
</li>
<li>
<a name="faq-N1005A-menu"></a><a href="#faq-N1005A">
      Why is reading a simple sheet taking so long?
    </a>
</li>
<li>
<a name="faq-N10066-menu"></a><a href="#faq-N10066">
      What is the HSSF "eventmodel"?
    </a>
</li>
<li>
<a name="faq-N10072-menu"></a><a href="#faq-N10072">
      Why can't read the document I created using Star Office 5.1?
    </a>
</li>
<li>
<a name="faq-N1007E-menu"></a><a href="#faq-N1007E">
      Why am I getting an exception each time I attempt to read my spreadsheet?
    </a>
</li>
<li>
<a name="faq-N1008D-menu"></a><a href="#faq-N1008D">
      How do you tell if a spreadsheet cell contains a date?
    </a>
</li>
<li>
<a name="faq-N1009D-menu"></a><a href="#faq-N1009D">
      I'm trying to stream an XLS file from a servlet and I'm having some trouble.  What's the problem?
    </a>
</li>
<li>
<a name="faq-N100C7-menu"></a><a href="#faq-N100C7">
      I want to set a cell format (Data format of a cell) of a excel sheet as ###,###,###.#### or ###,###,###.0000.    Is it possible using POI ?
    </a>
</li>
<li>
<a name="faq-N100D7-menu"></a><a href="#faq-N100D7">
      I want to set a cell format (Data format of a cell) of a excel sheet as text.    Is it possible using POI ?
    </a>
</li>
<li>
<a name="faq-N100E3-menu"></a><a href="#faq-N100E3">
      How do I add a border around a merged cell?
    </a>
</li>
<li>
<a name="faq-N100EF-menu"></a><a href="#faq-N100EF">
      I am using styles when creating a workbook in POI, but Excel refuses to open the file, complaining about "Too Many Styles".
    </a>
</li>
</ol>
<a name="Answers"></a>
<div class="h3">
<h3>Answers</h3>
</div>
<a name="faq-N10006"></a>
<div class="h4">
<h4>1. 
      My code uses some new feature, compiles fine but fails when live with a "MethodNotFoundException"
    </h4>
</div>
      
<p>You almost certainly have an older version of POI 
	on your classpath. Quite a few runtimes and other packages 
	will ship an older version of POI, so this is an easy problem
	to hit without your realising.</p>
      
<p>The best way to identify the offending earlier jar file is
	with a few lines of java. These will load one of the core POI
	classes, and report where it came from.</p>
      
<pre class="code">
ClassLoader classloader =
   org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
             "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("Core POI came from " + path);
      </pre>
    
<a name="faq-N10019"></a>
<div class="h4">
<h4>2. 
      My code uses the scratchpad, compiles fine but fails to run with a "MethodNotFoundException"
    </h4>
</div>
      
<p>You almost certainly have an older version earlier on your
	classpath. See the prior answer.</p>
    
<a name="faq-N10025"></a>
<div class="h4">
<h4>3. 
      I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/*something*"
    </h4>
</div>
      
<p>To use the new OOXML file formats, POI requires a jar containing
       the file format XSDs, as compiled by 
       <a href="http://xmlbeans.apache.org/">XMLBeans</a>. These
       XSDs, once compiled into Java classes, live in the
       <em>org.openxmlformats.schemas</em> namespace.</p>
      
<p>There are two jar files available, as described in
       <a href="/overview.html">the components overview section</a>.
       The <em>full jar of all of the schemas is ooxml-schemas-1.1.jar</em>, 
       and it is currently around 15mb. The <em>smaller poi-ooxml-schemas 
       jar</em> is only about 4mb. This latter jar file only contains the 
       typically used parts though.</p>
      
<p>Many users choose to use the smaller poi-ooxml-schemas jar to save
       space. However, the poi-ooxml-schemas jar only contains the XSDs and
       classes that are typically used, as identified by the unit tests.
       Every so often, you may try to use part of the file format which
       isn't included in the minimal poi-ooxml-schemas jar. In this case,
       you should switch to the full ooxml-schemas-1.1.jar. Longer term,
       you may also wish to submit a new unit test which uses the extra
       parts of the XSDs, so that a future poi-ooxml-schemas jar will 
       include them.</p>
      
<p>There are a number of ways to get the full ooxml-schemas-1.1.jar.
       If you are a maven user, see the 
       <a href="/overview.html">the components overview section</a>
       for the artifact details to have maven download it for you.
       If you download the source release of POI, and/or checkout the
       source code from <a href="/subversion.html">subversion</a>,
       then you can run the ant task "compile-ooxml-xsds" to have the
       OOXML schemas downloaded and compiled for you (This will also
       give you the XMLBeans generated source code, in case you wish to
       look at this). Finally, you can download the jar by hand from the
       <a href="http://www.ibiblio.org/maven/org.apache.poi/jars/">POI
       Maven Repository</a>.</p>
      
<p>Note that for POI 3.5 and 3.6, the full ooxml schemas jar was
       named ooxml-schemas-1.0.jar. For POI 3.7, the filename was bumped
       to ooxml-schemas-1.1.jar when generics support was added. You can
       use ooxml-schemas-1.1.jar with POI 3.5 and 3.6 if you wish, but
       POI 3.7 won't wokr with ooxml-schemas-1.0.jar (it needs thew newer
       one).</p>
    
<a name="faq-N1005A"></a>
<div class="h4">
<h4>4. 
      Why is reading a simple sheet taking so long?
    </h4>
</div>
      
<p>You've probably enabled logging.  Logging is intended only for
        autopsy style debugging.  Having it enabled will reduce performance
        by a factor of at least 100.  Logging is helpful for understanding
        why POI can't read some file or developing POI itself.  Important
        errors are thrown as exceptions, which means you probably don't need
        logging.</p>
    
<a name="faq-N10066"></a>
<div class="h4">
<h4>5. 
      What is the HSSF "eventmodel"?
    </h4>
</div>
      
<p>The SS eventmodel package is an API for reading Excel files without loading the whole spreadsheet into memory.  It does
        require more knowledge on the part of the user, but reduces memory consumption by more than
        tenfold.  It is based on the AWT event model in combination with SAX.  If you need read-only
        access, this is the best way to do it.</p>
    
<a name="faq-N10072"></a>
<div class="h4">
<h4>6. 
      Why can't read the document I created using Star Office 5.1?
    </h4>
</div>
      
<p>Star Office 5.1 writes some records using the older BIFF standard.  This causes some problems
        with POI which supports only BIFF8.</p>
    
<a name="faq-N1007E"></a>
<div class="h4">
<h4>7. 
      Why am I getting an exception each time I attempt to read my spreadsheet?
    </h4>
</div>
      
<p>It's possible your spreadsheet contains a feature that is not currently supported by POI.
        If you encounter this then please create the simplest file that demonstrates the trouble and submit it to
	<a href="http://issues.apache.org/bugzilla/buglist.cgi?product=POI">Bugzilla.</a>
</p>
    
<a name="faq-N1008D"></a>
<div class="h4">
<h4>8. 
      How do you tell if a spreadsheet cell contains a date?
    </h4>
</div>
      
<p>Excel stores dates as numbers therefore the only way to determine if a cell is
        actually stored as a date is to look at the formatting. There is a helper method
        in HSSFDateUtil that checks for this.
        Thanks to Jason Hoffman for providing the solution.</p>
      
<pre class="code">
   case HSSFCell.CELL_TYPE_NUMERIC:
         double d = cell.getNumericCellValue();
         // test if a date!
         if (HSSFDateUtil.isCellDateFormatted(cell)) {
           // format in form of M/D/YY
           cal.setTime(HSSFDateUtil.getJavaDate(d));
           cellText =
             (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
           cellText = cal.get(Calendar.MONTH)+1 + "/" +
                      cal.get(Calendar.DAY_OF_MONTH) + "/" +
                      cellText;
         }
      </pre>
    
<a name="faq-N1009D"></a>
<div class="h4">
<h4>9. 
      I'm trying to stream an XLS file from a servlet and I'm having some trouble.  What's the problem?
    </h4>
</div>
      
<p>
        The problem usually manifests itself as the junk characters being shown on
        screen. The problem persists even though you have set the correct mime type.
      </p>
      
<p>
        The short answer is, don't depend on IE to display a binary file type properly if you stream it via a
        servlet. Every minor version of IE has different bugs on this issue.
      </p>
      
<p>
        The problem in most versions of IE is that it does not use the mime type on
        the HTTP response to determine the file type; rather it uses the file extension
        on the request. Thus you might want to add a
        <strong>.xls</strong> to your request
        string. For example
        <em>http://yourserver.com/myServelet.xls?param1=xx</em>. This is
        easily accomplished through URL mapping in any servlet container. Sometimes
        a request like
        <em>http://yourserver.com/myServelet?param1=xx&amp;dummy=file.xls</em> is also
        known to work.
      </p>
      
<p>
        To guarantee opening the file properly in Excel from IE, write out your file to a
        temporary file under your web root from your servelet. Then send an http response
        to the browser to do a client side redirection to your temp file. (Note that using a 
        server side redirect using RequestDispatcher will not be effective in this case)
      </p>
      
<p>
        Note also that when you request a document that is opened with an
        external handler, IE sometimes makes two requests to the webserver. So if your
        generating process is heavy, it makes sense to write out to a temporary file, so that multiple
        requests happen for a static file.
      </p>
      
<p>
        None of this is particular to Excel. The same problem arises when you try to
        generate any binary file dynamically to an IE client. For example, if you generate
        pdf files using
        <a href="http://xml.apache.org/fop">FOP</a>, you will come across many of the same issues.
      </p>
      
    
<a name="faq-N100C7"></a>
<div class="h4">
<h4>10. 
      I want to set a cell format (Data format of a cell) of a excel sheet as ###,###,###.#### or ###,###,###.0000.    Is it possible using POI ?
    </h4>
</div>
      
<p>
	Yes.  You first need to get a DataFormat object from the workbook and call getFormat with the desired format.  Some examples are <a href="spreadsheet/quick-guide.html#DataFormats">here</a>.
      </p>
    
<a name="faq-N100D7"></a>
<div class="h4">
<h4>11. 
      I want to set a cell format (Data format of a cell) of a excel sheet as text.    Is it possible using POI ?
    </h4>
</div>
      
<p>
	Yes.  This is a built-in format for excel that you can get from DataFormat object using the format string "@".  Also, the string "text" will alias this format.
      </p>
    
<a name="faq-N100E3"></a>
<div class="h4">
<h4>12. 
      How do I add a border around a merged cell?
    </h4>
</div>
      
<p>Add blank cells around where the cells normally would have been and set the borders individually for each cell.
        We will probably enhance HSSF in the future to make this process easier.</p>
    
<a name="faq-N100EF"></a>
<div class="h4">
<h4>13. 
      I am using styles when creating a workbook in POI, but Excel refuses to open the file, complaining about "Too Many Styles".
    </h4>
</div>
      
<p>You just create the styles OUTSIDE of the loop in which you create cells.</p>
      
<p>GOOD:</p>
      
<pre class="code">
 HSSFWorkbook wb = new HSSFWorkbook();
 HSSFSheet sheet = wb.createSheet("new sheet");
 HSSFRow row = null;

 // Aqua background
 HSSFCellStyle style = wb.createCellStyle();
 style.setFillBackgroundColor(HSSFColor.AQUA.index);
 style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
 HSSFCell cell = row.createCell((short) 1);
 cell.setCellValue("X");
 cell.setCellStyle(style);

 // Orange "foreground",
 //     foreground being the fill foreground not the font color.
 style = wb.createCellStyle();
 style.setFillForegroundColor(HSSFColor.ORANGE.index);
 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

 for (int x = 0; x &lt; 1000; x++) {

 // Create a row and put some cells in it. Rows are 0 based.
	 row = sheet.createRow((short) k);

	 for (int y = 0; y &lt; 100; y++) {
		cell = row.createCell((short) k);
 		cell.setCellValue("X");
		cell.setCellStyle(style);
	 }
 }

 // Write the output to a file
 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
 wb.write(fileOut);
 fileOut.close();
      </pre>
      
<p>BAD:</p>
      
<pre class="code">
 HSSFWorkbook wb = new HSSFWorkbook();
 HSSFSheet sheet = wb.createSheet("new sheet");
 HSSFRow row = null;

 for (int x = 0; x &lt; 1000; x++) {
    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.AQUA.index);
    style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("X");
    cell.setCellStyle(style);

    // Orange "foreground",
    //       foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // Create a row and put some cells in it. Rows are 0 based.
    row = sheet.createRow((short) k);

    for (int y = 0; y &lt; 100; y++) {
       cell = row.createCell((short) k);
       cell.setCellValue("X");
       cell.setCellStyle(style);
    }
 }

 // Write the output to a file
 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
 wb.write(fileOut);
 fileOut.close();
      </pre>
    
</div>
</div>
</div>
</td>
<!--================= end Content ==================-->
</tr>
</tbody>
</table>
<!--================= end Main ==================-->
<!--================= start Footer ==================-->
<div id="footer">
<table summary="footer" cellspacing="0" cellpadding="4" width="100%" border="0">
<tbody>
<tr>
<!--================= start Copyright ==================-->
<td colspan="2">
<div align="center">
<div class="copyright">
              Copyright &copy; 2002-2011&nbsp;The Apache Software Foundation. All rights reserved.<br>
              Apache POI, POI, Apache, the Apache feather logo, and the Apache 
              POI project logo are trademarks of The Apache Software Foundation.
            </div>
</div>
</td>
<!--================= end Copyright ==================-->
</tr>
<tr>
<td align="left">
<!--================= start Host ==================-->
<!--================= end Host ==================--></td><td align="right">
<!--================= start Credits ==================-->
<div align="right">
<div class="credit"></div>
</div>
<!--================= end Credits ==================-->
</td>
</tr>
</tbody>
</table>
</div>
<!--================= end Footer ==================-->
</body>
</html>