📄 how-to.xml.orig
字号:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.1//EN" "../dtd/document-v11.dtd"><document> <header> <title>The New Halloween Document</title> <authors> <person email="acoliver2@users.sourceforge.net" name="Andrew C. Oliver" id="AO"/> <person email="glens@apache.org" name="Glen Stampoultzis" id="GJS"/> <person email="sergeikozello@mail.ru" name="Sergei Kozello" id="SK"/> </authors> </header> <body> <section><title>How to use the HSSF prototype API</title> <section><title>Capabilities</title> <p>This release of the how-to outlines functionality for the CVS HEAD. Those looking for information on previous releases should look in the documentation distributed with that release.</p> <p> This release allows numeric and string cell values to be written to or read from an XLS file as well as reading and writing dates. Also in this release is row and column sizing, cell styling (bold, italics, borders,etc), and support for both built-in and user defined data formats. New to this release is an event-based API for reading XLS files. It differs greatly from the read/write API and is intended for intermediate developers who need a smaller memory footprint. It will also serve as the basis for the HSSF Generator.</p> </section> <section><title>General Use</title> <section><title>User API</title> <section><title>Writing a new one</title> <p>The high level API (package: org.apache.poi.hssf.usermodel) is what most people should use. Usage is very simple. </p> <p>Workbooks are created by creating an instance of org.apache.poi.hssf.usermodel.HSSFWorkbook. </p> <p>Sheets are created by calling createSheet() from an existing instance of HSSFWorkbook, the created sheet is automatically added in sequence to the workbook. Sheets do not in themselves have a sheet name (the tab at the bottom); you set the name associated with a sheet by calling HSSFWorkbook.setSheetName(sheetindex,"SheetName",encoding). The name may be in 8bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default encoding is 8bit per char. </p> <p>Rows are created by calling createRow(rowNumber) from an existing instance of HSSFSheet. Only rows that have cell values should be added to the sheet. To set the row's height, you just call setRowHeight(height) on the row object. The height must be given in twips, or 1/20th of a point. If you prefer, there is also a setRowHeightInPoints method. </p> <p>Cells are created by calling createCell(column, type) from an existing HSSFRow. Only cells that have values should be added to the row. Cells should have their cell type set to either HSSFCell.CELL_TYPE_NUMERIC or HSSFCell.CELL_TYPE_STRING depending on whether they contain a numeric or textual value. Cells must also have a value set. Set the value by calling setCellValue with either a String or double as a parameter. Individual cells do not have a width; you must call setColumnWidth(colindex, width) (use units of 1/256th of a character) on the HSSFSheet object. (You can't do it on an individual basis in the GUI either).</p> <p>Cells are styled with HSSFCellStyle objects which in turn contain a reference to an HSSFFont object. These are created via the HSSFWorkbook object by calling createCellStyle() and createFont(). Once you create the object you must set its parameters (colors, borders, etc). To set a font for an HSSFCellStyle call setFont(fontobj). </p> <p>Once you have generated your workbook, you can write it out by calling write(outputStream) from your instance of Workbook, passing it an OutputStream (for instance, a FileOutputStream or ServletOutputStream). You must close the OutputStream yourself. HSSF does not close it for you. </p> <p>Here is some example code (excerpted and adapted from org.apache.poi.hssf.dev.HSSF test class):</p><source><![CDATA[short rownum;// create a new fileFileOutputStream out = new FileOutputStream("workbook.xls");// create a new workbookHSSFWorkbook wb = new HSSFWorkbook();// create a new sheetHSSFSheet s = wb.createSheet();// declare a row object referenceHSSFRow r = null;// declare a cell object referenceHSSFCell c = null;// create 3 cell stylesHSSFCellStyle cs = wb.createCellStyle();HSSFCellStyle cs2 = wb.createCellStyle();HSSFCellStyle cs3 = wb.createCellStyle();HSSFDataFormat df = wb.createDataFormat();// create 2 fonts objectsHSSFFont f = wb.createFont();HSSFFont f2 = wb.createFont();//set font 1 to 12 point typef.setFontHeightInPoints((short) 12);//make it bluef.setColor( (short)0xc );// make it bold//arial is the default fontf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//set font 2 to 10 point typef2.setFontHeightInPoints((short) 10);//make it redf2.setColor( (short)HSSFFont.COLOR_RED );//make it boldf2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);f2.setStrikeout( true );//set cell stlyecs.setFont(f);//set the cell format cs.setDataFormat(df.getFormat("#,##0.0"));//set a thin bordercs2.setBorderBottom(cs2.BORDER_THIN);//fill w fg fill colorcs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);//set the cell format to text see HSSFDataFormat for a full listcs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));// set the fontcs2.setFont(f2);// set the sheet name in Unicodewb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " + "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430", HSSFWorkbook.ENCODING_UTF_16 );// in case of compressed Unicode// wb.setSheetName(0, "HSSF Test", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE );// create a sheet with 30 rows (0-29)for (rownum = (short) 0; rownum < 30; rownum++){ // create a row r = s.createRow(rownum); // on every other row if ((rownum % 2) == 0) { // make the row height bigger (in twips - 1/20 of a point) r.setHeight((short) 0x249); } //r.setRowNum(( short ) rownum); // create 10 cells (0-9) (the += 2 becomes apparent later for (short cellnum = (short) 0; cellnum < 10; cellnum += 2) { // create a numeric cell c = r.createCell(cellnum); // do some goofy math to demonstrate decimals c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000))); String cellValue; // create a string cell (see why += 2 in the c = r.createCell((short) (cellnum + 1)); // on every other row if ((rownum % 2) == 0) { // set this cell to the first cell style we defined c.setCellStyle(cs); // set the cell's string value to "Test" c.setEncoding( HSSFCell.ENCODING_COMPRESSED_UNICODE ); c.setCellValue( "Test" ); } else { c.setCellStyle(cs2); // set the cell's string value to "\u0422\u0435\u0441\u0442" c.setEncoding( HSSFCell.ENCODING_UTF_16 ); c.setCellValue( "\u0422\u0435\u0441\u0442" ); } // make this column a bit wider s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20))); }}//draw a thick black border on the row at the bottom using BLANKS// advance 2 rowsrownum++;rownum++;r = s.createRow(rownum);// define the third style to be the default// except with a thick black border at the bottomcs3.setBorderBottom(cs3.BORDER_THICK);//create 50 cellsfor (short cellnum = (short) 0; cellnum < 50; cellnum++){ //create a blank type cell (no value) c = r.createCell(cellnum); // set it to the thick black border style c.setCellStyle(cs3);}//end draw thick black border// demonstrate adding/naming and deleting a sheet// create a sheet, set its title then delete its = wb.createSheet();wb.setSheetName(1, "DeletedSheet");wb.removeSheetAt(1);//end deleted sheet// write the workbook to the output stream// close our file (don't blow out our file handleswb.write(out);out.close(); ]]></source> </section> <section><title>Reading or modifying an existing file</title><p>Reading in a file is equally simple. To read in a file, create anew instance of org.apache.poi.poifs.Filesystem, passing in an open InputStream, such as a FileInputStreamfor your XLS, to the constructor. Construct a new instance oforg.apache.poi.hssf.usermodel.HSSFWorkbook passing theFilesystem instance to the constructor. From there you have access toall of the high level model objects through their assessor methods(workbook.getSheet(sheetNum), sheet.getRow(rownum), etc).</p><p>Modifying the file you have read in is simple. You retrieve theobject via an assessor method, remove it via a parent object's removemethod (sheet.removeRow(hssfrow)) and create objects just as youwould if creating a new xls. When you are done modifying cells justcall workbook.write(outputstream) just as you did above.</p><p>An example of this can be seen in<link href="http://cvs.apache.org/viewcvs/~checkout~/jakarta-poi/src/java/org/apache/poi/hssf/dev/HSSF.java?rev=1.1">org.apache.poi.hssf.dev.HSSF</link>.</p> </section> </section> <section><title>Event API</title> <p>The event API is brand new. It is intended for intermediate developers who are willing to learn a little bit of the low level API
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -