use of org.apache.poi.ss.usermodel.Font in project poi by apache.
the class BaseTestCellUtil method setFontFromDifferentWorkbook.
@Test
public void setFontFromDifferentWorkbook() throws IOException {
Workbook wb1 = _testDataProvider.createWorkbook();
Workbook wb2 = _testDataProvider.createWorkbook();
Font font1 = wb1.createFont();
Font font2 = wb2.createFont();
// do something to make font1 and font2 different
// so they are not same or equal.
font1.setItalic(true);
Cell A1 = wb1.createSheet().createRow(0).createCell(0);
// okay
CellUtil.setFont(A1, font1);
// font belongs to different workbook
try {
CellUtil.setFont(A1, font2);
fail("setFont not allowed if font belongs to a different workbook");
} catch (final IllegalArgumentException e) {
// one specific message is expected
if (!e.getMessage().startsWith("Font does not belong to this workbook")) {
throw e;
}
} finally {
wb1.close();
wb2.close();
}
}
use of org.apache.poi.ss.usermodel.Font in project poi by apache.
the class BaseTestCellUtil method setFont.
@Test
public void setFont() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sh = wb.createSheet();
Row row = sh.createRow(0);
Cell A1 = row.createCell(0);
Cell B1 = row.createCell(1);
final short defaultFontIndex = 0;
Font font = wb.createFont();
font.setItalic(true);
final short customFontIndex = font.getIndex();
// Assumptions
assertNotEquals(defaultFontIndex, customFontIndex);
assertEquals(A1.getCellStyle(), B1.getCellStyle());
// should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
// HSSFCellStyle wraps an underlying style record, and the underlying
// style record is the same between multiple getCellStyle() calls.
assertEquals(defaultFontIndex, A1.getCellStyle().getFontIndex());
assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());
// get/set alignment modifies the cell's style
CellUtil.setFont(A1, font);
assertEquals(customFontIndex, A1.getCellStyle().getFontIndex());
// get/set alignment doesn't affect the style of cells with
// the same style prior to modifying the style
assertNotEquals(A1.getCellStyle(), B1.getCellStyle());
assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());
wb.close();
}
use of org.apache.poi.ss.usermodel.Font in project poi by apache.
the class TestXSSFCell method testCopyCellFrom_CellCopyPolicy_copyHyperlink.
@Test
public final void testCopyCellFrom_CellCopyPolicy_copyHyperlink() throws IOException {
setUp_testCopyCellFrom_CellCopyPolicy();
final Workbook wb = srcCell.getSheet().getWorkbook();
final CreationHelper createHelper = wb.getCreationHelper();
srcCell.setCellValue("URL LINK");
Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
link.setAddress("http://poi.apache.org/");
srcCell.setHyperlink(link);
// Set link cell style (optional)
CellStyle hlinkStyle = wb.createCellStyle();
Font hlinkFont = wb.createFont();
hlinkFont.setUnderline(Font.U_SINGLE);
hlinkFont.setColor(IndexedColors.BLUE.getIndex());
hlinkStyle.setFont(hlinkFont);
srcCell.setCellStyle(hlinkStyle);
// Copy hyperlink
final CellCopyPolicy policy = new CellCopyPolicy.Builder().copyHyperlink(true).mergeHyperlink(false).build();
destCell.copyCellFrom(srcCell, policy);
assertNotNull(destCell.getHyperlink());
assertSame("unit test assumes srcCell and destCell are on the same sheet", srcCell.getSheet(), destCell.getSheet());
final List<XSSFHyperlink> links = srcCell.getSheet().getHyperlinkList();
assertEquals("number of hyperlinks on sheet", 2, links.size());
assertEquals("source hyperlink", new CellReference(srcCell).formatAsString(), links.get(0).getCellRef());
assertEquals("destination hyperlink", new CellReference(destCell).formatAsString(), links.get(1).getCellRef());
wb.close();
}
use of org.apache.poi.ss.usermodel.Font in project poi by apache.
the class SheetUtil method getCellWidth.
/**
* Compute width of a single cell
*
* @param cell the cell whose width is to be calculated
* @param defaultCharWidth the width of a single character
* @param formatter formatter used to prepare the text to be measured
* @param useMergedCells whether to use merged cells
* @return the width in pixels or -1 if cell is empty
*/
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
Sheet sheet = cell.getSheet();
Workbook wb = sheet.getWorkbook();
Row row = cell.getRow();
int column = cell.getColumnIndex();
// FIXME: this looks very similar to getCellWithMerges below. Consider consolidating.
// We should only be checking merged regions if useMergedCells is true. Why are we doing this for-loop?
int colspan = 1;
for (CellRangeAddress region : sheet.getMergedRegions()) {
if (region.isInRange(row.getRowNum(), column)) {
if (!useMergedCells) {
// If we're not using merged cells, skip this one and move on to the next.
return -1;
}
cell = row.getCell(region.getFirstColumn());
colspan = 1 + region.getLastColumn() - region.getFirstColumn();
}
}
CellStyle style = cell.getCellStyle();
CellType cellType = cell.getCellTypeEnum();
// for formula cells we compute the cell width for the cached formula result
if (cellType == CellType.FORMULA)
cellType = cell.getCachedFormulaResultTypeEnum();
Font font = wb.getFontAt(style.getFontIndex());
double width = -1;
if (cellType == CellType.STRING) {
RichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (String line : lines) {
String txt = line + defaultChar;
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
if (rt.numFormattingRuns() > 0) {
// TODO: support rich text fragments
}
width = getCellWidth(defaultCharWidth, colspan, style, width, str);
}
} else {
String sval = null;
if (cellType == CellType.NUMERIC) {
// Try to get it formatted to look the same as excel
try {
sval = formatter.formatCellValue(cell, dummyEvaluator);
} catch (Exception e) {
sval = String.valueOf(cell.getNumericCellValue());
}
} else if (cellType == CellType.BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
}
if (sval != null) {
String txt = sval + defaultChar;
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
width = getCellWidth(defaultCharWidth, colspan, style, width, str);
}
}
return width;
}
use of org.apache.poi.ss.usermodel.Font in project ddf by codice.
the class RrdMetricsRetriever method createSheet.
/**
* Creates an Excel worksheet containing the metric's data (timestamps and values) for the
* specified time range. This worksheet is titled with the trhe metric's name and added to the
* specified Workbook.
*
* @param wb the workbook to add this worksheet to
* @param metricName the name of the metric whose data is being rendered in this worksheet
* @param rrdFilename the name of the RRD file to retrieve the metric's data from
* @param startTime start time, in seconds since Unix epoch, to fetch metric's data
* @param endTime end time, in seconds since Unix epoch, to fetch metric's data
* @throws IOException
* @throws MetricsGraphException
*/
private void createSheet(Workbook wb, String metricName, String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException {
LOGGER.trace("ENTERING: createSheet");
MetricData metricData = getMetricData(rrdFilename, startTime, endTime);
String displayableMetricName = convertCamelCase(metricName);
String title = displayableMetricName + " for " + getCalendarTime(startTime) + " to " + getCalendarTime(endTime);
Sheet sheet = wb.createSheet(displayableMetricName);
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle columnHeadingsStyle = wb.createCellStyle();
columnHeadingsStyle.setFont(headerFont);
CellStyle bannerStyle = wb.createCellStyle();
bannerStyle.setFont(headerFont);
bannerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
bannerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
int rowCount = 0;
Row row = sheet.createRow((short) rowCount);
Cell cell = row.createCell(0);
cell.setCellValue(title);
cell.setCellStyle(bannerStyle);
rowCount++;
// Blank row for spacing/readability
row = sheet.createRow((short) rowCount);
cell = row.createCell(0);
cell.setCellValue("");
rowCount++;
row = sheet.createRow((short) rowCount);
cell = row.createCell(0);
cell.setCellValue("Timestamp");
cell.setCellStyle(columnHeadingsStyle);
cell = row.createCell(1);
cell.setCellValue("Value");
cell.setCellStyle(columnHeadingsStyle);
rowCount++;
List<Long> timestamps = metricData.getTimestamps();
List<Double> values = metricData.getValues();
for (int i = 0; i < timestamps.size(); i++) {
String timestamp = getCalendarTime(timestamps.get(i));
row = sheet.createRow((short) rowCount);
row.createCell(0).setCellValue(timestamp);
row.createCell(1).setCellValue(values.get(i));
rowCount++;
}
if (metricData.hasTotalCount()) {
// Blank row for spacing/readability
row = sheet.createRow((short) rowCount);
cell = row.createCell(0);
cell.setCellValue("");
rowCount++;
row = sheet.createRow((short) rowCount);
cell = row.createCell(0);
cell.setCellValue("Total Count: ");
cell.setCellStyle(columnHeadingsStyle);
row.createCell(1).setCellValue(metricData.getTotalCount());
}
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
LOGGER.trace("EXITING: createSheet");
}
Aggregations