/** * Create a new Cell style and add it to the workbook's style table * * @return the new Cell Style object */ @Override public CellStyle createCellStyle() { return _wb.createCellStyle(); }
protected XSSFCellStyle createStyle(XSSFColor color) { XSSFCellStyle style = workbook.createCellStyle(); if (color != null) { style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(color); } style.setWrapText(true); style.setVerticalAlignment(VerticalAlignment.CENTER); return style; }
import org.apache.poi.xssf.usermodel.*; //import needed XSSFWorkbook wb = new XSSFWorkbook (); // Create workbook XSSFSheet sheet = wb.createSheet(); // Create spreadsheet in workbook XSSFRow row = sheet.createRow(rowIndex); // Create the row in the spreadsheet //1. Create the date cell style XSSFCreationHelper createHelper = wb.getCreationHelper(); XSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( createHelper.createDataFormat().getFormat("MMMM dd, yyyy")); //2. Apply the Date cell style to a cell //This example sets the first cell in the row using the date cell style cell = row.createCell(0); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle);
public class SO{ public static void main(String[] args) { try { FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx")); XSSFWorkbook wb = new XSSFWorkbook(is); String header = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"; Sheet sheet = wb.getSheet("Sheet1"); sheet.setColumnWidth(0, 18000); Row row = sheet.createRow(0); Cell cell = row.createCell(0); if(header.length() > 50){ //Length of String for my test sheet.setColumnWidth(0, 18000); //Set column width, you'll probably want to tweak the second int CellStyle style = wb.createCellStyle(); //Create new style style.setWrapText(true); //Set wordwrap cell.setCellStyle(style); //Apply style to cell cell.setCellValue(header); //Write header } wb.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx"))); } catch (IOException e) { e.printStackTrace(); } } }
@Override public void reset(XSSFWorkbook workbook) { style = workbook.createCellStyle(); style.setDataFormat(workbook.getCreationHelper() .createDataFormat().getFormat(formatString)); }
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell( 0); cell.setCellValue("custom XSSF colors"); XSSFCellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
@Override public void reset(XSSFWorkbook workbook) { this.workbook = workbook; style = workbook.createCellStyle(); Font hlinkFont = workbook.createFont(); hlinkFont.setUnderline(Font.U_SINGLE); hlinkFont.setColor(IndexedColors.BLUE.getIndex()); style.setFont(hlinkFont); links = 0; }
XSSFWorkbook wb = new XSSFWorkbook(); CellStyle borderStyle = wb.createCellStyle(); borderStyle.setBorderBottom(CellStyle.BORDER_THIN); borderStyle.setBorderLeft(CellStyle.BORDER_THIN); borderStyle.setBorderRight(CellStyle.BORDER_THIN); borderStyle.setBorderTop(CellStyle.BORDER_THIN); borderStyle.setAlignment(CellStyle.ALIGN_CENTER); Sheet sheet = wb.createSheet("Test Sheet"); Row row = sheet.createRow(1); for (int i = 1; i <= 5; ++i) { Cell cell = row.createCell(i); cell.setCellStyle(borderStyle); if (i == 1) { cell.setCellValue("Centred Text"); } } sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));
/** * Create a new Cell style and add it to the workbook's style table * * @return the new Cell Style object */ @Override public CellStyle createCellStyle() { return _wb.createCellStyle(); }
public XSSFCellStyle getStyle(String name) { if (!styles.containsKey(name)) { styles.put(name, workbook.createCellStyle()); } return styles.get(name); }
private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {//用到 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); return cellStyle; }
private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {// 用到 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); return cellStyle; }
XSSFWorkbook wb = new XSSFWorkbook(); XSSFCellStyle cellStyle = wb.createCellStyle(); byte[] rgb = new byte[3]; rgb[0] = (byte) 242; // red rgb[1] = (byte) 220; // green rgb[2] = (byte) 219; // blue XSSFColor myColor = new XSSFColor(rgb); // #f2dcdb cellStyle.setFillForegroundColor(myColor); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
private CellStyle getCellDataFormat(int scale, XSSFWorkbook workbook) { CellStyle style = workbook.createCellStyle(); if(scale > 0){ String dataFormat = "0."; for(int i = 0; i < scale; i++){ dataFormat = dataFormat+"0"; } style.setDataFormat(workbook.createDataFormat().getFormat(dataFormat)); } return style; }
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell( 0); cell.setCellValue("custom XSSF colors"); XSSFCellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
protected XSSFCellStyle createStyle(XSSFColor color) { XSSFCellStyle style = workbook.createCellStyle(); if (color != null) { style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(color); } style.setWrapText(true); style.setVerticalAlignment(VerticalAlignment.CENTER); return style; }
private CellStyle createHyperLinkStyle(final XSSFWorkbook workbook) { final CellStyle hlink_style = workbook.createCellStyle(); final Font hlink_font = workbook.createFont(); hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(IndexedColors.BLUE.getIndex()); hlink_style.setFont(hlink_font); return hlink_style; } }
@Override public void prepareColumn(TableModel tableModel, XSSFSheet sheet, int col) { XSSFWorkbook wb = sheet.getWorkbook(); XSSFDataFormat df = wb.createDataFormat(); cellStyle = wb.createCellStyle(); XSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.BLACK.getIndex()); cellStyle.setFont(f); cellStyle.setDataFormat(df.getFormat("m/d/yy h:mm")); }
@Override public void prepareColumn(TableModel tableModel, XSSFSheet sheet, int col) { XSSFWorkbook wb = sheet.getWorkbook(); XSSFDataFormat df = wb.createDataFormat(); cellStyle = wb.createCellStyle(); XSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.BLACK.getIndex()); cellStyle.setFont(f); cellStyle.setDataFormat(df.getFormat("text")); }
@Override public void prepareColumn(TableModel tableModel, XSSFSheet sheet, int col) { XSSFWorkbook wb = sheet.getWorkbook(); XSSFDataFormat df = wb.createDataFormat(); cellStyle = wb.createCellStyle(); XSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.BLACK.getIndex()); cellStyle.setFont(f); cellStyle.setDataFormat(df.getFormat("m/d/yy")); }