⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testworkbook.java

📁 Office格式转换代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and *    "Apache POI" must not be used to endorse or promote products *    derived from this software without prior written permission. For *    written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    "Apache POI", nor may "Apache" appear in their name, without *    prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.poi.hssf.usermodel;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Iterator;import junit.framework.TestCase;import org.apache.poi.hssf.model.Workbook;import org.apache.poi.hssf.record.BackupRecord;import org.apache.poi.hssf.record.LabelSSTRecord;import org.apache.poi.hssf.record.Record;import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;import org.apache.poi.hssf.util.Region;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * Class to test Workbook functionality * * @author Andrew C. Oliver * @author Greg Merrill * @author Siggi Cherem */public class TestWorkbook    extends TestCase{    private static final String LAST_NAME_KEY        = "lastName";    private static final String FIRST_NAME_KEY       = "firstName";    private static final String SSN_KEY              = "ssn";    private static final String REPLACE_ME           = "replaceMe";    private static final String REPLACED             = "replaced";    private static final String DO_NOT_REPLACE       = "doNotReplace";    private static final String EMPLOYEE_INFORMATION = "Employee Info";    private static final String LAST_NAME_VALUE      = "Bush";    private static final String FIRST_NAME_VALUE     = "George";    private static final String SSN_VALUE            = "555555555";    private SanityChecker sanityChecker = new SanityChecker();    /**     * Constructor TestWorkbook     *     * @param name     */    public TestWorkbook(String name)    {        super(name);    }    /**     * TEST NAME:  Test Write Sheet Simple <P>     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values.<P>     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects     *             Last row, first row is tested against the correct values (99,0).<P>     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.     *             HSSFSheet last row or first row is incorrect.             <P>     *     */    public void testWriteSheetSimple()        throws IOException    {        File             file = File.createTempFile("testWriteSheetSimple",                                                    ".xls");        FileOutputStream out  = new FileOutputStream(file);        HSSFWorkbook     wb   = new HSSFWorkbook();        HSSFSheet        s    = wb.createSheet();        HSSFRow          r    = null;        HSSFCell         c    = null;        for (short rownum = ( short ) 0; rownum < 100; rownum++)        {            r = s.createRow(rownum);            // r.setRowNum(( short ) rownum);            for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)            {                c = r.createCell(cellnum);                c.setCellValue(rownum * 10000 + cellnum                               + ((( double ) rownum / 1000)                                  + (( double ) cellnum / 10000)));                c = r.createCell(( short ) (cellnum + 1));                c.setCellValue("TEST");            }        }        wb.write(out);        out.close();        sanityChecker.checkHSSFWorkbook(wb);        assertEquals("LAST ROW == 99", 99, s.getLastRowNum());        assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());        // assert((s.getLastRowNum() == 99));    }    /**     * TEST NAME:  Test Write/Modify Sheet Simple <P>     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values,     *             remove some rows, yet still have a valid file/data.<P>     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects     *             Last row, first row is tested against the correct values (74,25).<P>     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.     *             HSSFSheet last row or first row is incorrect.             <P>     *     */    public void testWriteModifySheetSimple()        throws IOException    {        File             file = File.createTempFile("testWriteSheetSimple",                                                    ".xls");        FileOutputStream out  = new FileOutputStream(file);        HSSFWorkbook     wb   = new HSSFWorkbook();        HSSFSheet        s    = wb.createSheet();        HSSFRow          r    = null;        HSSFCell         c    = null;        for (short rownum = ( short ) 0; rownum < 100; rownum++)        {            r = s.createRow(rownum);            // r.setRowNum(( short ) rownum);            for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2)            {                c = r.createCell(cellnum);                c.setCellValue(rownum * 10000 + cellnum                               + ((( double ) rownum / 1000)                                  + (( double ) cellnum / 10000)));                c = r.createCell(( short ) (cellnum + 1));                c.setCellValue("TEST");            }        }        for (short rownum = ( short ) 0; rownum < 25; rownum++)        {            r = s.getRow(rownum);            s.removeRow(r);        }        for (short rownum = ( short ) 75; rownum < 100; rownum++)        {            r = s.getRow(rownum);            s.removeRow(r);        }        wb.write(out);        out.close();        sanityChecker.checkHSSFWorkbook(wb);        assertEquals("LAST ROW == 74", 74, s.getLastRowNum());        assertEquals("FIRST ROW == 25", 25, s.getFirstRowNum());    }    /**     * TEST NAME:  Test Read Simple <P>     * OBJECTIVE:  Test that HSSF can read a simple spreadsheet (Simple.xls).<P>     * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions.<P>     * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values     *             in the sheet in their known positions.<P>     *     */    public void testReadSimple()        throws IOException    {        String filename = System.getProperty("HSSF.testdata.path");        filename = filename + "/Simple.xls";        FileInputStream stream   = new FileInputStream(filename);        POIFSFileSystem fs       = new POIFSFileSystem(stream);        HSSFWorkbook    workbook = new HSSFWorkbook(fs);        HSSFSheet       sheet    = workbook.getSheetAt(0);        assertEquals(REPLACE_ME,                     sheet.getRow(( short ) 0).getCell(( short ) 0)                         .getStringCellValue());        stream.close();    }    /**     * TEST NAME:  Test Read Simple w/ Data Format<P>     * OBJECTIVE:  Test that HSSF can read a simple spreadsheet (SimpleWithDataFormat.xls).<P>     * SUCCESS:    HSSF reads the sheet.  Matches values in their particular positions and format is correct<P>     * FAILURE:    HSSF does not read a sheet or excepts.  HSSF cannot identify values     *             in the sheet in their known positions.<P>     *     */    public void testReadSimpleWithDataFormat()        throws IOException    {        String filename = System.getProperty("HSSF.testdata.path");        filename = filename + "/SimpleWithDataFormat.xls";        FileInputStream stream   = new FileInputStream(filename);        POIFSFileSystem fs       = new POIFSFileSystem(stream);        HSSFWorkbook    workbook = new HSSFWorkbook(fs);        HSSFSheet       sheet    = workbook.getSheetAt(0);        HSSFDataFormat  format   = workbook.createDataFormat();	HSSFCell	cell	 =                      sheet.getRow(( short ) 0).getCell(( short ) 0);        assertEquals(1.25,cell.getNumericCellValue(), 1e-10);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -