import org.apache.poi.hssf.record.ExtendedFormatRecord;
import java.util.Date;
+import java.util.Calendar;
/**
* High level representation of a cell in a row of a spreadsheet.
setCellValue(HSSFDateUtil.getExcelDate(value));
}
+ /**
+ * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
+ * a date.
+ *
+ * @param value the date value to set this cell to. For formulas we'll set the
+ * precalculated value, for numerics we'll set its value. For othertypes we
+ * will change the cell to a numeric cell and set its value.
+ */
+ public void setCellValue(Calendar value)
+ {
+ setCellValue(value.getTime());
+ }
+
/**
* set a string value for the cell.
*
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.dev.POIFSViewable;
import org.apache.poi.poifs.property.DocumentProperty;
import org.apache.poi.poifs.property.Property;
writer.processPOIFSWriterEvent(
new POIFSWriterEvent(dstream, path, name, size));
dstream.writeFiller(countBlocks()
- * PoiFSConstants
+ * POIFSConstants
.BIG_BLOCK_SIZE, DocumentBlock
.getFillByte());
}
{
if (writer != null)
{
- rval = (size + PoiFSConstants.BIG_BLOCK_SIZE - 1)
- / PoiFSConstants.BIG_BLOCK_SIZE;
+ rval = (size + POIFSConstants.BIG_BLOCK_SIZE - 1)
+ / POIFSConstants.BIG_BLOCK_SIZE;
}
else
{
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.dev.POIFSViewable;
import org.apache.poi.poifs.property.DirectoryProperty;
import org.apache.poi.poifs.property.DocumentProperty;
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.dev.POIFSViewable;
import org.apache.poi.util.ByteField;
import org.apache.poi.util.IntegerField;
protected Property()
{
- _raw_data = new byte[ PoiFSConstants.PROPERTY_SIZE ];
+ _raw_data = new byte[ POIFSConstants.PROPERTY_SIZE ];
Arrays.fill(_raw_data, _default_fill);
_name_size = new ShortField(_name_size_offset);
_property_type =
protected Property(final int index, final byte [] array, final int offset)
{
- _raw_data = new byte[ PoiFSConstants.PROPERTY_SIZE ];
+ _raw_data = new byte[ POIFSConstants.PROPERTY_SIZE ];
System.arraycopy(array, offset, _raw_data, 0,
- PoiFSConstants.PROPERTY_SIZE);
+ POIFSConstants.PROPERTY_SIZE);
_name_size = new ShortField(_name_size_offset, _raw_data);
_property_type =
new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET, _raw_data);
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.storage.ListManagedBlock;
/**
{
byte[] data = blocks[ j ].getData();
int property_count = data.length
- / PoiFSConstants.PROPERTY_SIZE;
+ / POIFSConstants.PROPERTY_SIZE;
int offset = 0;
for (int k = 0; k < property_count; k++)
properties.add(null);
break;
}
- offset += PoiFSConstants.PROPERTY_SIZE;
+ offset += POIFSConstants.PROPERTY_SIZE;
}
}
return properties;
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.filesystem.BATManaged;
import org.apache.poi.poifs.storage.BlockWritable;
import org.apache.poi.poifs.storage.PropertyBlock;
public PropertyTable()
{
- _start_block = PoiFSConstants.END_OF_CHAIN;
+ _start_block = POIFSConstants.END_OF_CHAIN;
_properties = new ArrayList();
addProperty(new RootProperty());
_blocks = null;
final RawDataBlockList blockList)
throws IOException
{
- _start_block = PoiFSConstants.END_OF_CHAIN;
+ _start_block = POIFSConstants.END_OF_CHAIN;
_blocks = null;
_properties =
PropertyFactory
import java.io.IOException;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.storage.SmallDocumentBlock;
/**
// overrides
setNodeColor(_NODE_BLACK);
setPropertyType(PropertyConstants.ROOT_TYPE);
- setStartBlock(PoiFSConstants.END_OF_CHAIN);
+ setStartBlock(POIFSConstants.END_OF_CHAIN);
}
/**
import java.util.Arrays;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
extends BigBlock
{
private static final int _entries_per_block =
- PoiFSConstants.BIG_BLOCK_SIZE / LittleEndianConsts.INT_SIZE;
+ POIFSConstants.BIG_BLOCK_SIZE / LittleEndianConsts.INT_SIZE;
private static final int _entries_per_xbat_block = _entries_per_block
- 1;
private static final int _xbat_chain_offset =
private BATBlock()
{
- _data = new byte[ PoiFSConstants.BIG_BLOCK_SIZE ];
+ _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
Arrays.fill(_data, _default_value);
_fields = new IntegerField[ _entries_per_block ];
int offset = 0;
{
blocks[ index ].setXBATChain(startBlock + index + 1);
}
- blocks[ index ].setXBATChain(PoiFSConstants.END_OF_CHAIN);
+ blocks[ index ].setXBATChain(POIFSConstants.END_OF_CHAIN);
}
return blocks;
}
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntList;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
offset += LittleEndianConsts.INT_SIZE;
}
chain_index = LittleEndian.getInt(data, chain_index_offset);
- if (chain_index == PoiFSConstants.END_OF_CHAIN)
+ if (chain_index == POIFSConstants.END_OF_CHAIN)
{
break;
}
List blocks = new ArrayList();
int currentBlock = startBlock;
- while (currentBlock != PoiFSConstants.END_OF_CHAIN)
+ while (currentBlock != POIFSConstants.END_OF_CHAIN)
{
blocks.add(blockList.remove(currentBlock));
currentBlock = _entries.get(currentBlock);
* @param index of the current block
*
* @return index of the next block (may be
- * PoiFSConstants.END_OF_CHAIN, indicating end of chain
+ * POIFSConstants.END_OF_CHAIN, indicating end of chain
* (duh))
*
* @exception IOException if the current block is unused
{
int entry = LittleEndian.getInt(data, offset);
- if (entry == PoiFSConstants.UNUSED_BLOCK)
+ if (entry == POIFSConstants.UNUSED_BLOCK)
{
raw_blocks.zap(_entries.size());
}
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.filesystem.BATManaged;
import org.apache.poi.util.IntList;
import org.apache.poi.util.LittleEndian;
public BlockAllocationTableWriter()
{
- _start_block = PoiFSConstants.END_OF_CHAIN;
+ _start_block = POIFSConstants.END_OF_CHAIN;
_entries = new IntList();
_blocks = new BATBlock[ 0 ];
}
{
_entries.add(index++);
}
- _entries.add(PoiFSConstants.END_OF_CHAIN);
+ _entries.add(POIFSConstants.END_OF_CHAIN);
}
return startBlock;
}
import java.util.Arrays;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
private DocumentBlock()
{
- _data = new byte[ PoiFSConstants.BIG_BLOCK_SIZE ];
+ _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
Arrays.fill(_data, _default_value);
}
public boolean partiallyRead()
{
- return _bytes_read != PoiFSConstants.BIG_BLOCK_SIZE;
+ return _bytes_read != POIFSConstants.BIG_BLOCK_SIZE;
}
/**
final int size)
{
DocumentBlock[] rval =
- new DocumentBlock[ (size + PoiFSConstants.BIG_BLOCK_SIZE - 1) / PoiFSConstants.BIG_BLOCK_SIZE ];
+ new DocumentBlock[ (size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE ];
int offset = 0;
for (int k = 0; k < rval.length; k++)
rval[ k ] = new DocumentBlock();
if (offset < array.length)
{
- int length = Math.min(PoiFSConstants.BIG_BLOCK_SIZE,
+ int length = Math.min(POIFSConstants.BIG_BLOCK_SIZE,
array.length - offset);
System.arraycopy(array, offset, rval[ k ]._data, 0, length);
- if (length != PoiFSConstants.BIG_BLOCK_SIZE)
+ if (length != POIFSConstants.BIG_BLOCK_SIZE)
{
Arrays.fill(rval[ k ]._data, length,
- PoiFSConstants.BIG_BLOCK_SIZE,
+ POIFSConstants.BIG_BLOCK_SIZE,
_default_value);
}
}
{
Arrays.fill(rval[ k ]._data, _default_value);
}
- offset += PoiFSConstants.BIG_BLOCK_SIZE;
+ offset += POIFSConstants.BIG_BLOCK_SIZE;
}
return rval;
}
public static void read(final DocumentBlock [] blocks,
final byte [] buffer, final int offset)
{
- int firstBlockIndex = offset / PoiFSConstants.BIG_BLOCK_SIZE;
- int firstBlockOffset = offset % PoiFSConstants.BIG_BLOCK_SIZE;
+ int firstBlockIndex = offset / POIFSConstants.BIG_BLOCK_SIZE;
+ int firstBlockOffset = offset % POIFSConstants.BIG_BLOCK_SIZE;
int lastBlockIndex = (offset + buffer.length - 1)
- / PoiFSConstants.BIG_BLOCK_SIZE;
+ / POIFSConstants.BIG_BLOCK_SIZE;
if (firstBlockIndex == lastBlockIndex)
{
System.arraycopy(blocks[ firstBlockIndex ]._data,
firstBlockOffset, buffer, buffer_offset,
- PoiFSConstants.BIG_BLOCK_SIZE
+ POIFSConstants.BIG_BLOCK_SIZE
- firstBlockOffset);
- buffer_offset += PoiFSConstants.BIG_BLOCK_SIZE - firstBlockOffset;
+ buffer_offset += POIFSConstants.BIG_BLOCK_SIZE - firstBlockOffset;
for (int j = firstBlockIndex + 1; j < lastBlockIndex; j++)
{
System.arraycopy(blocks[ j ]._data, 0, buffer, buffer_offset,
- PoiFSConstants.BIG_BLOCK_SIZE);
- buffer_offset += PoiFSConstants.BIG_BLOCK_SIZE;
+ POIFSConstants.BIG_BLOCK_SIZE);
+ buffer_offset += POIFSConstants.BIG_BLOCK_SIZE;
}
System.arraycopy(blocks[ lastBlockIndex ]._data, 0, buffer,
buffer_offset, buffer.length - buffer_offset);
package org.apache.poi.poifs.storage;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
public static final long _signature = 0xE11AB1A1E011CFD0L;
public static final int _bat_array_offset = 0x4c;
public static final int _max_bats_in_header =
- (PoiFSConstants.BIG_BLOCK_SIZE - _bat_array_offset)
+ (POIFSConstants.BIG_BLOCK_SIZE - _bat_array_offset)
/ LittleEndianConsts.INT_SIZE;
// useful offsets
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
public HeaderBlockReader(final InputStream stream)
throws IOException
{
- _data = new byte[ PoiFSConstants.BIG_BLOCK_SIZE ];
+ _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
int byte_count = stream.read(_data);
- if (byte_count != PoiFSConstants.BIG_BLOCK_SIZE)
+ if (byte_count != POIFSConstants.BIG_BLOCK_SIZE)
{
String type = " byte" + ((byte_count == 1) ? ("")
: ("s"));
throw new IOException("Unable to read entire header; "
+ byte_count + type + " read; expected "
- + PoiFSConstants.BIG_BLOCK_SIZE + " bytes");
+ + POIFSConstants.BIG_BLOCK_SIZE + " bytes");
}
// verify signature
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LongField;
public HeaderBlockWriter()
{
- _data = new byte[ PoiFSConstants.BIG_BLOCK_SIZE ];
+ _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
Arrays.fill(_data, _default_value);
new LongField(_signature_offset, _signature, _data);
new IntegerField(0x08, 0, _data);
new IntegerField(0x28, 0, _data);
_bat_count = new IntegerField(_bat_count_offset, 0, _data);
_property_start = new IntegerField(_property_start_offset,
- PoiFSConstants.END_OF_CHAIN,
+ POIFSConstants.END_OF_CHAIN,
_data);
new IntegerField(0x34, 0, _data);
new IntegerField(0x38, 0x1000, _data);
_sbat_start = new IntegerField(_sbat_start_offset,
- PoiFSConstants.END_OF_CHAIN, _data);
+ POIFSConstants.END_OF_CHAIN, _data);
new IntegerField(0x40, 1, _data);
_xbat_start = new IntegerField(_xbat_start_offset,
- PoiFSConstants.END_OF_CHAIN, _data);
+ POIFSConstants.END_OF_CHAIN, _data);
_xbat_count = new IntegerField(_xbat_count_offset, 0, _data);
}
else
{
rvalue = BATBlock.createXBATBlocks(new int[ 0 ], 0);
- _xbat_start.set(PoiFSConstants.END_OF_CHAIN, _data);
+ _xbat_start.set(POIFSConstants.END_OF_CHAIN, _data);
}
_xbat_count.set(rvalue.length, _data);
return rvalue;
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.poifs.property.Property;
import org.apache.poi.util.IntegerField;
import org.apache.poi.util.LittleEndian;
extends BigBlock
{
private static final int _properties_per_block =
- PoiFSConstants.BIG_BLOCK_SIZE / PoiFSConstants.PROPERTY_SIZE;
+ POIFSConstants.BIG_BLOCK_SIZE / POIFSConstants.PROPERTY_SIZE;
private Property[] _properties;
/**
package org.apache.poi.poifs.storage;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import java.io.*;
public RawDataBlock(final InputStream stream)
throws IOException
{
- _data = new byte[ PoiFSConstants.BIG_BLOCK_SIZE ];
+ _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
int count = stream.read(_data);
if (count == -1)
{
_eof = true;
}
- else if (count != PoiFSConstants.BIG_BLOCK_SIZE)
+ else if (count != POIFSConstants.BIG_BLOCK_SIZE)
{
String type = " byte" + ((count == 1) ? ("")
: ("s"));
throw new IOException("Unable to read entire block; " + count
+ type + " read; expected "
- + PoiFSConstants.BIG_BLOCK_SIZE + " bytes");
+ + POIFSConstants.BIG_BLOCK_SIZE + " bytes");
}
else
{
import java.util.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
/**
* Storage for documents that are too small to use regular
private static final byte _default_fill = ( byte ) 0xff;
private static final int _block_size = 64;
private static final int _blocks_per_big_block =
- PoiFSConstants.BIG_BLOCK_SIZE / _block_size;
+ POIFSConstants.BIG_BLOCK_SIZE / _block_size;
private SmallDocumentBlock(final byte [] data, final int index)
{
}
/**
- * Tests that is creating a file with a date works correctly.
+ * Tests that is creating a file with a date or an calendar works correctly.
*/
public void testDataStyle()
throws Exception
HSSFSheet s = wb.createSheet();
HSSFCellStyle cs = wb.createCellStyle();
HSSFRow row = s.createRow((short)0);
+
+ // with Date:
HSSFCell cell = row.createCell((short)1);
cs.setDataFormat(HSSFDataFormat.getFormat("m/d/yy"));
cell.setCellStyle(cs);
cell.setCellValue(new Date());
+
+ // with Calendar:
+ cell = row.createCell((short)2);
+ cs.setDataFormat(HSSFDataFormat.getFormat("m/d/yy"));
+ cell.setCellStyle(cs);
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date());
+ cell.setCellValue(cal);
+
wb.write(out);
out.close();
import junit.framework.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
/**
* Class to test DirectoryProperty functionality
import junit.framework.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
/**
* Class to test RootProperty functionality
{
_testblock[ index ] = ( byte ) 0;
}
- _testblock[ index++ ] = ( byte ) PoiFSConstants.END_OF_CHAIN;
+ _testblock[ index++ ] = ( byte ) POIFSConstants.END_OF_CHAIN;
for (; index < 0x78; index++)
{
_testblock[ index ] = ( byte ) 0xff;
import junit.framework.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
else if (i % 256 == 255)
{
assertEquals("Verify end of chain for block " + i,
- PoiFSConstants.END_OF_CHAIN,
+ POIFSConstants.END_OF_CHAIN,
table.getNextBlockIndex(i));
}
else
import junit.framework.*;
-import org.apache.poi.poifs.common.PoiFSConstants;
+import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;
offset += LittleEndianConsts.INT_SIZE;
}
LittleEndian.putInt(expected, offset,
- PoiFSConstants.END_OF_CHAIN);
+ POIFSConstants.END_OF_CHAIN);
offset += 4;
block_index++;
}
// add BAT block indices
LittleEndian.putInt(expected, offset, block_index++);
offset += LittleEndianConsts.INT_SIZE;
- LittleEndian.putInt(expected, offset, PoiFSConstants.END_OF_CHAIN);
+ LittleEndian.putInt(expected, offset, POIFSConstants.END_OF_CHAIN);
for (int k = 0; k < expected.length; k++)
{
assertEquals("At offset " + k, expected[ k ], output[ k ]);