List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook write
@SuppressWarnings("resource") public final void write(OutputStream stream) throws IOException
From source file:packtest.WorkbookProperties.java
License:Apache License
public static void main(String[] args) throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); workbook.createSheet("Workbook Properties"); POIXMLProperties props = workbook.getProperties(); /**// www.j a va 2 s . c o m * Extended properties are a predefined set of metadata properties * that are specifically applicable to Office Open XML documents. * Extended properties consist of 24 simple properties and 3 complex properties stored in the * part targeted by the relationship of type */ POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties(); ext.getUnderlyingProperties().setCompany("Apache Software Foundation"); ext.getUnderlyingProperties().setTemplate("XSSF"); /** * Custom properties enable users to define custom metadata properties. */ POIXMLProperties.CustomProperties cust = props.getCustomProperties(); cust.addProperty("Author", "John Smith"); cust.addProperty("Year", 2009); cust.addProperty("Price", 45.50); cust.addProperty("Available", true); FileOutputStream out = new FileOutputStream(Utils.getPath("workbook.xlsx")); workbook.write(out); out.close(); }
From source file:packtest.WorkingWithRichText.java
License:Apache License
public static void main(String[] args) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); try {//from ww w .j a va 2 s .c om XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow((short) 2); XSSFCell cell = row.createCell(1); XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox"); XSSFFont font1 = wb.createFont(); font1.setBold(true); font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0))); rt.applyFont(0, 10, font1); XSSFFont font2 = wb.createFont(); font2.setItalic(true); font2.setUnderline(XSSFFont.U_DOUBLE); font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0))); rt.applyFont(10, 19, font2); XSSFFont font3 = wb.createFont(); font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255))); rt.append(" Jumped over the lazy dog", font3); cell.setCellValue(rt); // Write the output to a file OutputStream fileOut = new FileOutputStream(Utils.getPath("xssf-richtext.xlsx")); try { wb.write(fileOut); } finally { fileOut.close(); } } finally { wb.close(); } }
From source file:poi.xslf.usermodel.PieChartDemo.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 2) { usage();/* www . j a va 2 s. c o m*/ return; } BufferedReader modelReader = new BufferedReader(new FileReader(args[1])); String chartTitle = modelReader.readLine(); // first line is chart title XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0])); XSLFSlide slide = pptx.getSlides()[0]; // find chart in the slide XSLFChart chart = null; for (POIXMLDocumentPart part : slide.getRelations()) { if (part instanceof XSLFChart) { chart = (XSLFChart) part; break; } } if (chart == null) throw new IllegalStateException("chart not found in the template"); // embedded Excel workbook that holds the chart data POIXMLDocumentPart xlsPart = chart.getRelations().get(0); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); CTChart ctChart = chart.getCTChart(); CTPlotArea plotArea = ctChart.getPlotArea(); CTPieChart pieChart = plotArea.getPieChartArray(0); //Pie Chart Series CTPieSer ser = pieChart.getSerArray(0); // Series Text CTSerTx tx = ser.getTx(); tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle); sheet.createRow(0).createCell(1).setCellValue(chartTitle); String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString(); tx.getStrRef().setF(titleRef); // Category Axis Data CTAxDataSource cat = ser.getCat(); CTStrData strData = cat.getStrRef().getStrCache(); // Values CTNumDataSource val = ser.getVal(); CTNumData numData = val.getNumRef().getNumCache(); strData.setPtArray(null); // unset old axis text numData.setPtArray(null); // unset old values // set model int idx = 0; int rownum = 1; String ln; while ((ln = modelReader.readLine()) != null) { String[] vals = ln.split("\\s+"); CTNumVal numVal = numData.addNewPt(); numVal.setIdx(idx); numVal.setV(vals[1]); CTStrVal sVal = strData.addNewPt(); sVal.setIdx(idx); sVal.setV(vals[0]); idx++; XSSFRow row = sheet.createRow(rownum++); row.createCell(0).setCellValue(vals[0]); row.createCell(1).setCellValue(Double.valueOf(vals[1])); } numData.getPtCount().setVal(idx); strData.getPtCount().setVal(idx); String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true); val.getNumRef().setF(numDataRange); String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true); cat.getStrRef().setF(axisDataRange); // updated the embedded workbook with the data OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream(); wb.write(xlsOut); xlsOut.close(); // save the result FileOutputStream out = new FileOutputStream("pie-chart-demo-output.pptx"); pptx.write(out); out.close(); }
From source file:poi.xssf.usermodel.examples.AligningCells.java
License:Apache License
public static void main(String[] args) throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet();//from ww w.ja v a 2 s.co m XSSFRow row = sheet.createRow((short) 2); row.setHeightInPoints(30); for (int i = 0; i < 8; i++) { //column width is set in units of 1/256th of a character width sheet.setColumnWidth(i, 256 * 15); } createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM); createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM); createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER); createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER); createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY); createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP); createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP); //center text over B4, C4, D4 row = sheet.createRow((short) 3); centerAcrossSelection(wb, row, (short) 1, (short) 3, XSSFCellStyle.VERTICAL_CENTER); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:poi.xssf.usermodel.examples.CalendarDemo.java
License:Apache License
public static void main(String[] args) throws Exception { Calendar calendar = Calendar.getInstance(); if (args.length > 0) calendar.set(Calendar.YEAR, Integer.parseInt(args[0])); int year = calendar.get(Calendar.YEAR); XSSFWorkbook wb = new XSSFWorkbook(); Map<String, XSSFCellStyle> styles = createStyles(wb); for (int month = 0; month < 12; month++) { calendar.set(Calendar.MONTH, month); calendar.set(Calendar.DAY_OF_MONTH, 1); //create a sheet for each month XSSFSheet sheet = wb.createSheet(months[month]); //turn off gridlines sheet.setDisplayGridlines(false); sheet.setPrintGridlines(false);//from ww w . jav a 2 s. c o m XSSFPrintSetup printSetup = sheet.getPrintSetup(); printSetup.setOrientation(PrintOrientation.LANDSCAPE); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); //the header row: centered text in 48pt font XSSFRow headerRow = sheet.createRow(0); headerRow.setHeightInPoints(80); XSSFCell titleCell = headerRow.createCell(0); titleCell.setCellValue(months[month] + " " + year); titleCell.setCellStyle(styles.get("title")); sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1")); //header with month titles XSSFRow monthRow = sheet.createRow(1); for (int i = 0; i < days.length; i++) { //for compatibility with HSSF we have to set column width in units of 1/256th of a character width sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1)); XSSFCell monthCell = monthRow.createCell(i * 2); monthCell.setCellValue(days[i]); monthCell.setCellStyle(styles.get("month")); } int cnt = 1, day = 1; int rownum = 2; for (int j = 0; j < 6; j++) { XSSFRow row = sheet.createRow(rownum++); row.setHeightInPoints(100); for (int i = 0; i < days.length; i++) { XSSFCell dayCell_1 = row.createCell(i * 2); XSSFCell dayCell_2 = row.createCell(i * 2 + 1); int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) { dayCell_1.setCellValue(day); calendar.set(Calendar.DAY_OF_MONTH, ++day); if (i == 0 || i == days.length - 1) { dayCell_1.setCellStyle(styles.get("weekend_left")); dayCell_2.setCellStyle(styles.get("weekend_right")); } else { dayCell_1.setCellStyle(styles.get("workday_left")); dayCell_2.setCellStyle(styles.get("workday_right")); } } else { dayCell_1.setCellStyle(styles.get("grey_left")); dayCell_2.setCellStyle(styles.get("grey_right")); } cnt++; } if (calendar.get(Calendar.MONTH) > month) break; } } // Write the output to a file FileOutputStream out = new FileOutputStream("calendar-" + year + ".xlsx"); wb.write(out); out.close(); }
From source file:poi.xssf.usermodel.examples.WorkbookProperties.java
License:Apache License
public static void main(String[] args) throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); workbook.createSheet("Workbook Properties"); POIXMLProperties props = workbook.getProperties(); /**/*from ww w . j ava 2 s . c o m*/ * Extended properties are a predefined set of metadata properties * that are specifically applicable to Office Open XML documents. * Extended properties consist of 24 simple properties and 3 complex properties stored in the * part targeted by the relationship of type */ POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties(); ext.getUnderlyingProperties().setCompany("Apache Software Foundation"); ext.getUnderlyingProperties().setTemplate("XSSF"); /** * Custom properties enable users to define custom metadata properties. */ POIXMLProperties.CustomProperties cust = props.getCustomProperties(); cust.addProperty("Author", "John Smith"); cust.addProperty("Year", 2009); cust.addProperty("Price", 45.50); cust.addProperty("Available", true); FileOutputStream out = new FileOutputStream("workbook.xlsx"); workbook.write(out); out.close(); }
From source file:poi.xssf.usermodel.examples.WorkingWithRichText.java
License:Apache License
public static void main(String[] args) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); XSSFSheet sheet = wb.createSheet();/*from w w w . j ava 2 s. c o m*/ XSSFRow row = sheet.createRow((short) 2); XSSFCell cell = row.createCell(1); XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox"); XSSFFont font1 = wb.createFont(); font1.setBold(true); font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0))); rt.applyFont(0, 10, font1); XSSFFont font2 = wb.createFont(); font2.setItalic(true); font2.setUnderline(XSSFFont.U_DOUBLE); font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0))); rt.applyFont(10, 19, font2); XSSFFont font3 = wb.createFont(); font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255))); rt.append(" Jumped over the lazy dog", font3); cell.setCellValue(rt); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:poisample2.POIsample2.java
/** * @param args the command line arguments *//*w w w .java 2s .c o m*/ public static void main(String[] args) { // TODO code application logic here try { XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hi there"); OutputStream out; out = new FileOutputStream(new File("testout.xlsx")); wb.write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:preprocessing.EnemyInfoGenerator.java
public static void createEnemyInfoExcel(String level, List<EnemyInfo> enemyList, HashMap<String, List<String[]>> skillMap, HashMap<String, List<String[]>> skillRoleMap, HashMap<String, String[]> enemyAiOrderMap, HashMap<String, String[]> enemyLvUpMap) { try {//from w w w. ja va2 s .c o m File f = new File("./EnemyInfo_" + enemyList.get(0).getName() + " " + level + ".xlsx"); FileOutputStream out; XSSFWorkbook book = new XSSFWorkbook(); writeToSheet(book, enemyList, skillMap, skillRoleMap, enemyAiOrderMap, enemyLvUpMap); out = new FileOutputStream(f); book.write(out); out.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:preprocessing.SkillTextGenerator.java
private static void loadExcel() { try {//from w w w .java 2s.c o m File f = new File(".\\organized.xlsx"); File fnew = new File(".\\organized_new.xlsx"); FileOutputStream out; try (FileInputStream is = new FileInputStream(f)) { XSSFWorkbook book = new XSSFWorkbook(is); writeToSheet(book); createCSV(book); createSphereCSV(); out = new FileOutputStream(fnew); book.write(out); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } }