use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.
the class TestTextObjectRecord method testWrite.
public void testWrite() {
HSSFRichTextString str = new HSSFRichTextString("Hello, World!");
TextObjectRecord record = new TextObjectRecord();
record.setStr(str);
record.setHorizontalTextAlignment(TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED);
record.setVerticalTextAlignment(TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_TOP);
record.setTextLocked(true);
record.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_NONE);
byte[] ser = record.serialize();
assertEquals(ser.length, simpleData.length);
assertArrayEquals(simpleData, ser);
//read again
RecordInputStream is = TestcaseRecordInputStream.create(simpleData);
record = new TextObjectRecord(is);
}
use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.
the class TestTextObjectBaseRecord method testStore.
public void testStore() {
TextObjectRecord record = new TextObjectRecord();
HSSFRichTextString str = new HSSFRichTextString("AB");
str.applyFont(0, 2, (short) 0x0018);
str.applyFont(2, 2, (short) 0x0320);
record.setHorizontalTextAlignment(TextObjectRecord.HORIZONTAL_TEXT_ALIGNMENT_CENTERED);
record.setVerticalTextAlignment(TextObjectRecord.VERTICAL_TEXT_ALIGNMENT_JUSTIFY);
record.setTextLocked(true);
record.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_ROT_RIGHT);
record.setStr(str);
byte[] recordBytes = record.serialize();
assertEquals(recordBytes.length, data.length);
for (int i = 0; i < data.length; i++) assertEquals("At offset " + i, data[i], recordBytes[i]);
}
use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.
the class NumberRenderingSpreadsheetGenerator method writeHeaderCell.
private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(new HSSFRichTextString(text));
cell.setCellStyle(style);
}
use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.
the class NumberRenderingSpreadsheetGenerator method writeDataRow.
static void writeDataRow(HSSFSheet sheet, int rowIx, long rawLongBits, String expectedExcelRendering) {
double d = Double.longBitsToDouble(rawLongBits);
HSSFRow row = sheet.createRow(rowIx);
int rowNum = rowIx + 1;
String cel0ref = "A" + rowNum;
String rawBitsText = formatLongAsHex(rawLongBits);
String jmExpr = "'ec(" + rawBitsText + ", ''\" & C" + rowNum + " & \"'', ''\" & D" + rowNum + " & \"''),'";
// The 'Match' column will contain 'OK' if the metadata (from NumberToTextConversionExamples)
// matches Excel's rendering.
String matchExpr = "if(D" + rowNum + "=E" + rowNum + ", \"OK\", \"ERROR\")";
row.createCell(0).setCellValue(d);
row.createCell(1).setCellValue(new HSSFRichTextString(rawBitsText));
row.createCell(2).setCellValue(new HSSFRichTextString(Double.toString(d)));
row.createCell(3).setCellFormula("\"\" & " + cel0ref);
row.createCell(4).setCellValue(new HSSFRichTextString(expectedExcelRendering));
row.createCell(5).setCellFormula(matchExpr);
row.createCell(6).setCellFormula(jmExpr.replaceAll("'", "\""));
// if (false) {
// // for observing arithmetic near numeric range boundaries
// row.createCell(7).setCellFormula(cel0ref + " * 1.0001");
// row.createCell(8).setCellFormula(cel0ref + " / 1.0001");
// }
}
use of org.apache.poi.hssf.usermodel.HSSFRichTextString in project poi by apache.
the class CellComments method main.
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
// Create the drawing patriarch. This is the top level container for all shapes including cell comments.
HSSFPatriarch patr = sheet.createDrawingPatriarch();
//create a cell in row 3
HSSFCell cell1 = sheet.createRow(3).createCell(1);
cell1.setCellValue(new HSSFRichTextString("Hello, World"));
//anchor defines size and position of the comment in worksheet
HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
// set text in the comment
comment1.setString(new HSSFRichTextString("We can set comments in POI"));
//set comment author.
//you can see it in the status bar when moving mouse over the commented cell
comment1.setAuthor("Apache Software Foundation");
// The first way to assign comment to a cell is via HSSFCell.setCellComment method
cell1.setCellComment(comment1);
//create another cell in row 6
HSSFCell cell2 = sheet.createRow(6).createCell(1);
cell2.setCellValue(36.6);
HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 8, (short) 6, 11));
//modify background color of the comment
comment2.setFillColor(204, 236, 255);
HSSFRichTextString string = new HSSFRichTextString("Normal body temperature");
//apply custom font to the text in the comment
HSSFFont font = wb.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 10);
font.setBold(true);
font.setColor(HSSFColorPredefined.RED.getIndex());
string.applyFont(font);
comment2.setString(string);
//by default comments are hidden. This one is always visible.
comment2.setVisible(true);
comment2.setAuthor("Bill Gates");
/**
* The second way to assign comment to a cell is to implicitly specify its row and column.
* Note, it is possible to set row and column of a non-existing cell.
* It works, the comment is visible.
*/
comment2.setRow(6);
comment2.setColumn(1);
FileOutputStream out = new FileOutputStream("poi_comment.xls");
wb.write(out);
out.close();
} finally {
wb.close();
}
}
Aggregations