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:net.benhowell.core.Store.java
License:Open Source License
public void generateXlsx(File file) { try {/* w w w . jav a 2 s. co m*/ FileOutputStream out = new FileOutputStream(file.getAbsoluteFile()); XSSFWorkbook wb = new XSSFWorkbook(); Sheet shDetails = wb.createSheet("Details"); Sheet shLanguages = wb.createSheet("Languages"); Sheet shResults = wb.createSheet("Results"); Row row = shDetails.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("sex"); cell = row.createCell(1); cell.setCellValue("age"); cell = row.createCell(2); cell.setCellValue("years in Australia"); cell = row.createCell(3); cell.setCellValue("months in Australia"); cell = row.createCell(4); cell.setCellValue("first language"); row = shDetails.createRow(1); cell = row.createCell(0); cell.setCellValue(details.get("sex")); cell = row.createCell(1); cell.setCellValue(details.get("age")); cell = row.createCell(2); cell.setCellValue(details.get("yearsInAustralia")); cell = row.createCell(3); cell.setCellValue(details.get("monthsInAustralia")); cell = row.createCell(4); cell.setCellValue(details.get("firstLanguage")); int i = 1; row = shLanguages.createRow(0); cell = row.createCell(0); cell.setCellValue("language"); cell = row.createCell(1); cell.setCellValue("fluency"); for (Map.Entry<String, String> e : languages.entrySet()) { row = shLanguages.createRow(i); cell = row.createCell(0); cell.setCellValue(e.getKey()); cell = row.createCell(1); cell.setCellValue(e.getValue()); i++; } i = 1; row = shResults.createRow(0); cell = row.createCell(0); cell.setCellValue("trial_id"); cell = row.createCell(1); cell.setCellValue("category"); cell = row.createCell(2); cell.setCellValue("type"); cell = row.createCell(3); cell.setCellValue("image"); cell = row.createCell(4); cell.setCellValue("text"); cell = row.createCell(5); cell.setCellValue("name"); cell = row.createCell(6); cell.setCellValue("result"); for (Map.Entry<String, Map<String, String>> e : trials.entrySet()) { row = shResults.createRow(i); cell = row.createCell(0); cell.setCellValue(e.getValue().get("id")); cell = row.createCell(1); cell.setCellValue(e.getValue().get("category")); cell = row.createCell(2); cell.setCellValue(e.getValue().get("type")); cell = row.createCell(3); cell.setCellValue(e.getValue().get("image")); cell = row.createCell(4); cell.setCellValue(e.getValue().get("text")); cell = row.createCell(5); cell.setCellValue(e.getValue().get("name")); cell = row.createCell(6); cell.setCellValue(e.getValue().get("result")); i++; } wb.write(out); out.close(); } catch (IOException e) { System.out.println("Exception: " + e); } }
From source file:net.clementlevallois.gradingpics.io.Excel.java
public void writeFGradeForOneStudent(String name, String grade) throws FileNotFoundException, IOException { XSSFWorkbook workbook; FileInputStream file = null;//from w w w . j a v a 2 s .c o m if (Files.exists(Paths.get("grades_2018.xlsx"))) { file = new FileInputStream(new File("grades_2018.xlsx")); workbook = new XSSFWorkbook(file); } else { workbook = new XSSFWorkbook(); } while (workbook.getNumberOfSheets() < 4) { workbook.createSheet(String.valueOf(String.valueOf(workbook.getNumberOfSheets() + 1))); } XSSFSheet sheet = workbook.getSheetAt(1); int lastRowNumber = sheet.getLastRowNum(); //Create a new row in current sheet XSSFRow row = sheet.createRow(lastRowNumber + 1); //Create a new cell in current row XSSFCell cellName = row.createCell(0); //Set value to new value cellName.setCellValue(name); XSSFCell cellGrade = row.createCell(1); cellGrade.setCellValue(grade); //close the excel file when done if (file != null) { file.close(); } FileOutputStream fos = new FileOutputStream(new File("grades_2018.xlsx")); workbook.write(fos); fos.close(); }
From source file:net.openchrom.msd.converter.supplier.excel.io.ChromatogramWriter.java
License:Open Source License
@Override public void writeChromatogram(File file, IChromatogramMSD chromatogram, IProgressMonitor monitor) throws FileNotFoundException, FileIsNotWriteableException, IOException { XSSFWorkbook excelWorkbook = new XSSFWorkbook(); XSSFSheet excelSheet = excelWorkbook.createSheet(chromatogram.getName()); Map<String, XSSFCellStyle> styles = createStyles(excelWorkbook); String excelSheetReferenceName = excelSheet.getPackagePart().getPartName().getName(); /*/*www . ja va 2s . c o m*/ * Excel Template */ File excelTemplate = new File(PathHelper.getStoragePath() + File.separator + "exceltemplace.xlsx"); FileOutputStream outputStream = new FileOutputStream(excelTemplate); excelWorkbook.write(outputStream); outputStream.close(); /* * Temporary XML file */ File xmlDataFile = new File(PathHelper.getStoragePath() + File.separator + "datafile.xml"); Writer writer = new OutputStreamWriter(new FileOutputStream(xmlDataFile), XML_ENCODING); try { writeChromatogram(writer, styles, chromatogram, monitor); } catch (Exception e) { throw new IOException("There has something gone wrong writing the temporary data file."); } writer.close(); /* * Merge the template and the data file. */ FileOutputStream resultOutputStream = new FileOutputStream(file); mergeTemplateAndData(excelTemplate, xmlDataFile, excelSheetReferenceName.substring(1), resultOutputStream); resultOutputStream.close(); /* * Delete the temporary files. */ excelTemplate.delete(); xmlDataFile.delete(); }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.jobs.SaveXlsxExcelJob.java
License:Open Source License
private void saveExcel(String save) { XSSFWorkbook wb = new XSSFWorkbook(); // or new HSSFWorkbook(); sheet = wb.createSheet("Sheet1"); if (grid != null) { Display display = PlatformUI.getWorkbench().getDisplay(); display.syncExec(new Runnable() { public void run() { for (int j = 0; j < grid.getItemCount(); j++) { rowa = sheet.createRow(j); for (int k = 0; k < grid.getColumnCount(); k++) { cella = rowa.createCell(k); String s = grid.getItem(j).getText(k); // Font f=grid.getItem(j).getFont(); try { double d = Double.parseDouble(s); cella.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cella.setCellValue(d); } catch (NumberFormatException e1) { cella.setCellType(XSSFCell.CELL_TYPE_STRING); cella.setCellValue(s); }/*from ww w . j av a2 s. co m*/ } } } }); } FileOutputStream fileOut; try { fileOut = new FileOutputStream(save); try { wb.write(fileOut); fileOut.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } wb = null; sheet = null; }
From source file:net.sf.mzmine.util.io.XSSFExcelWriterReader.java
License:Open Source License
/** * Saves the given workbook to a file. The file has to end with .xlsx * * @param file the path and name (with suffix (.xlsx)) the file will be saved to (*.xlsx) * @param wb the workbook with data// www .j av a 2 s. c om * @return * @see */ public boolean saveWbToFile(File file, XSSFWorkbook wb) { try { if (FileAndPathUtil.createDirectory(file.getParentFile())) { FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); return true; } else return false; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } }
From source file:nl.architolk.ldt.processors.ExcelSerializer.java
License:Open Source License
protected void readInput(final PipelineContext pipelineContext, final ProcessorInput input, Config config, OutputStream outputStream) { try {/*from w ww .j av a2s . c om*/ // Read the input as a DOM final Document domDocument = readInputAsDOM(pipelineContext, input); // create workbook (xlsx) XSSFWorkbook wb = new XSSFWorkbook(); //iterate through sheets; NodeList sheetNodes = domDocument.getElementsByTagName("sheet"); if (sheetNodes.getLength() == 0) { throw new OXFException("At least one sheet should be present"); } for (short i = 0; i < sheetNodes.getLength(); i++) { Node sheetNode = sheetNodes.item(i); if (sheetNode.getNodeType() == Node.ELEMENT_NODE) { Element sheetElement = (Element) sheetNode; XSSFSheet sheet = wb.createSheet(sheetElement.getAttribute("name")); //iterate through rows; NodeList rowNodes = sheetNode.getChildNodes(); short rownr = 0; for (short r = 0; r < rowNodes.getLength(); r++) { Node rowNode = rowNodes.item(r); if (rowNode.getNodeType() == Node.ELEMENT_NODE) { XSSFRow row = sheet.createRow(rownr++); //iterate through columns; NodeList columnNodes = rowNode.getChildNodes(); short colnr = 0; for (short c = 0; c < columnNodes.getLength(); c++) { Node columnNode = columnNodes.item(c); if (columnNode.getNodeType() == Node.ELEMENT_NODE) { XSSFCell cell = row.createCell(colnr++); cell.setCellValue(columnNode.getTextContent()); } } } } } } // write workbook to stream wb.write(outputStream); outputStream.close(); } catch (Exception e) { throw new OXFException(e); } }
From source file:nl.b3p.viewer.admin.stripes.ServiceUsageMatrixActionBean.java
License:Open Source License
@DefaultHandler public Resolution view() throws JSONException, TransformerConfigurationException, TransformerException, Exception { List<Application> applications = Stripersist.getEntityManager() .createQuery("FROM Application order by name,version").getResultList(); JSONArray jsonApps = new JSONArray(); for (Application app : applications) { JSONObject json = new JSONObject(app.toJSON(this.context.getRequest(), true, true)); jsonApps.put(json);/*from w w w . j av a 2s . co m*/ } //add the featureSources to the JSON. List<FeatureSource> featureSources; if (this.featureSource == null) { featureSources = Stripersist.getEntityManager().createQuery("FROM FeatureSource").getResultList(); } else { featureSources = new ArrayList<FeatureSource>(); featureSources.add(this.featureSource); } JSONArray featureSourcesJson = new JSONArray(); for (FeatureSource fs : featureSources) { JSONObject fsJson = fs.toJSONObject(); featureSourcesJson.put(fsJson); JSONObject featuretypesRoot = new JSONObject(); JSONArray ftJsonArray = new JSONArray(); featuretypesRoot.put("featureType", ftJsonArray); fsJson.put("featuretypes", featuretypesRoot); List<SimpleFeatureType> featureTypes = fs.getFeatureTypes(); for (SimpleFeatureType sft : featureTypes) { JSONObject ftJson = new JSONObject(); ftJson.put("id", sft.getId()); ftJson.put("name", sft.getTypeName()); ftJson.put("description", sft.getDescription()); ftJsonArray.put(ftJson); } } //format a json for the xml output. JSONObject fs = new JSONObject(); fs.put("featureSource", featureSourcesJson); JSONObject appl = new JSONObject(); appl.put("application", jsonApps); //make root elements for applicatons and featuresources JSONObject firstChild = new JSONObject(); firstChild.put("applications", appl); firstChild.put("featureSources", fs); JSONObject root = new JSONObject(); root.put("root", firstChild); //make xml String rawXml = org.json.XML.toString(root); this.xml = transformXml(rawXml); if (output_format != null || "XLS".equalsIgnoreCase(output_format)) { final XSSFWorkbook workbook = createWorkBook(this.xml); return new StreamingResolution("application/vnd.ms-excel") { public void stream(final HttpServletResponse response) { try { workbook.write(response.getOutputStream()); } catch (IOException ioe) { log.error("Error while writing workbook", ioe); } } }; } return new ForwardResolution(JSP); }
From source file:nl.detoren.ijc.io.OutputExcel.java
License:Open Source License
/** * Create the Excel version of the sheet Original Empty file is stored in * Empty.xlsx Create version with round matches is stored in Indeling.xlsx * * @param wedstrijden//from www. j a v a 2 s . com * The round to store in the Excel file */ public boolean export(Wedstrijden wedstrijden) { try { logger.log(Level.INFO, "Wedstrijden wegschrijven naar Excel"); int periode = wedstrijden.getPeriode(); int ronde = wedstrijden.getRonde(); String rpString = "Periode " + periode + ", Ronde " + ronde; String datum = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(Calendar.getInstance().getTime()); // Open the empty schedule file, matches are stored in the // second sheet (id = 1) FileInputStream file = new FileInputStream("Template.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(file); ArrayList<Groepswedstrijden> gws = wedstrijden.getGroepswedstrijden(); for (Groepswedstrijden gw : gws) { int groepID = gw.getNiveau(); int nrSeries = gw.getSeries().size(); // Open sheet voor deze groep XSSFSheet sheet = workbook.getSheetAt(groepID); workbook.setSheetName(groepID, Groep.geefNaam(groepID)); updateCell(sheet, 2, 8, rpString); updateCell(sheet, 2, 4, datum); // Export Series int currentRow = 5; for (int s = 0; s < nrSeries; ++s) { // For each serie Serie serie = gw.getSerie(s); updateCell(sheet, currentRow, 2, "Serie " + (s + 1)); borderLeft(getCell(sheet, currentRow, 1)); borderLeft(getCell(sheet, currentRow + 1, 1)); borderRight(getCell(sheet, currentRow, 9)); borderRight(getCell(sheet, currentRow + 1, 9)); currentRow += 2; for (Wedstrijd w : serie.getWedstrijden()) { exportWedstrijd(sheet, w, currentRow); borderLeft(getCell(sheet, currentRow, 1)); borderLeft(getCell(sheet, currentRow + 1, 1)); borderRight(getCell(sheet, currentRow, 9)); borderRight(getCell(sheet, currentRow + 1, 9)); currentRow += 2; } } // Export trio ArrayList<Wedstrijd> trio = gw.getTriowedstrijden(); if (trio != null && trio.size() > 0) { updateCell(sheet, currentRow, 2, "Trio"); borderLeft(getCell(sheet, currentRow, 1)); borderLeft(getCell(sheet, currentRow + 1, 1)); borderRight(getCell(sheet, currentRow, 9)); borderRight(getCell(sheet, currentRow + 1, 9)); currentRow += 2; for (Wedstrijd w : trio) { exportWedstrijd(sheet, w, currentRow); borderLeft(getCell(sheet, currentRow, 1)); borderLeft(getCell(sheet, currentRow + 1, 1)); borderRight(getCell(sheet, currentRow, 9)); borderRight(getCell(sheet, currentRow + 1, 9)); currentRow += 2; } } currentRow--; for (int j = 2; j <= 8; j++) borderBottom(getCell(sheet, currentRow, j)); borderLeftBottom(getCell(sheet, currentRow, 1)); borderRightBottom(getCell(sheet, currentRow, 9)); } // Close input file file.close(); // Store Excel to new file String dirName = "R" + periode + "-" + ronde; new File(dirName).mkdirs(); String filename = dirName + File.separator + "Indeling " + periode + "-" + ronde + ".xlsx"; File outputFile = new File(filename); FileOutputStream outFile = new FileOutputStream(outputFile); workbook.write(outFile); // Close output file workbook.close(); outFile.close(); // And open it in the system editor Desktop.getDesktop().open(outputFile); return true; } catch (Exception e) { logger.log(Level.WARNING, "Error writing output: " + e.toString()); FoutMelding.melding("Fout bij opslaan Excel bestand: " + e.getMessage()); return false; } }
From source file:nl.detoren.ijsco.io.ExcelExport.java
License:Open Source License
public void exportGroepen(Groepen groepen) { String password = "abcd"; try {// w w w . j a va 2 s .c o m if (groepen == null) return; // sheetindx geeft index in Excel template op basis van groepsgrootte. -1: geen sheet voor groepsgrootte int[] sheetindx = new int[] { -1, -1, -1, -1, 5, -1, 4, -1, 3, -1, 2, -1, 1, -1, 0, -1, -1, -1 }; // columnsize geeft lengte in Excel template op basis van groepsgrootte. -1: geen sheet voor groepsgrootte int[] columnsize = new int[] { -1, -1, -1, -1, 20, -1, 35, -1, 54, -1, 77, -1, 100, -1, 127, -1, -1, -1 }; // pagelngth geeft lengte in Excel template op basis van groepsgrootte. -1: geen sheet voor groepsgrootte int[] pagelngth = new int[] { -1, -1, -1, -1, 20, -1, 35, -1, 54, -1, 77, -1, 100, -1, 127, -1, -1, -1 }; int sheet2row = 2; int sheet3row = 2; FileInputStream file = new FileInputStream("Indeling.xlsm"); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFCellStyle style1 = workbook.createCellStyle(); style1.setFillPattern(FillPatternType.SOLID_FOREGROUND); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(180, 180, 180))); XSSFCellStyle my_style = workbook.createCellStyle(); XSSFColor my_foreground = new XSSFColor(Color.ORANGE); XSSFColor my_background = new XSSFColor(Color.RED); my_style.setFillPattern(FillPatternType.SOLID_FOREGROUND); my_style.setFillForegroundColor(my_foreground); my_style.setFillBackgroundColor(my_background); XSSFSheet sheet2 = workbook.getSheet("Groepsindeling"); XSSFSheet sheet3 = workbook.getSheet("Deelnemerslijst"); updateCell(sheet3, sheet3row, 0, "Naam", style1); updateCell(sheet3, sheet3row, 1, "KNSB nr", style1); updateCell(sheet3, sheet3row, 2, "rating", style1); updateCell(sheet3, sheet3row, 3, "groep", style1); sheet3row++; for (Groep groep : groepen) { logger.log(Level.INFO, "Exporteer groep : " + groep.getNaam()); XSSFSheet sheet = workbook.cloneSheet(sheetindx[groep.getGrootte()], groep.getNaam()); updateCell(sheet, 0, 6, groep.getNaam()); updateCell(sheet2, sheet2row, 1, groep.getNaam()); sheet2row++; updateCell(sheet2, sheet2row, 0, "nr", style1); updateCell(sheet2, sheet2row, 1, "Naam", style1); updateCell(sheet2, sheet2row, 2, "KNSB nr", style1); updateCell(sheet2, sheet2row, 3, "rating", style1); sheet2row++; for (int i = 0; i < groep.getGrootte(); i++) { updateCell(sheet, 3 + i, 2, groep.getSpeler(i).getNaam()); updateCell(sheet, 3 + i, 3, groep.getSpeler(i).getKnsbnummer()); updateCell(sheet, 3 + i, 5, groep.getSpeler(i).getRating()); updateCell(sheet2, sheet2row, 0, i + 1); updateCell(sheet2, sheet2row, 1, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(2) + (4 + i), true); updateCell(sheet2, sheet2row, 2, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(3) + (4 + i), true); updateCell(sheet2, sheet2row, 3, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(5) + (4 + i), true); if (groep.getSpeler(i).getNaam() != "Bye") { updateCell(sheet3, sheet3row, 0, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(2) + (4 + i), true); updateCell(sheet3, sheet3row, 1, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(3) + (4 + i), true); updateCell(sheet3, sheet3row, 2, "'" + sheet.getSheetName() + "'!" + org.apache.poi.ss.util.CellReference.convertNumToColString(5) + (4 + i), true); updateCell(sheet3, sheet3row, 3, groep.getNaam()); } sheet2row++; sheet3row++; } sheet2row++; sheet.setForceFormulaRecalculation(true); // Set print margins XSSFPrintSetup ps = sheet.getPrintSetup(); ps.setLandscape(true); ps.setFitWidth((short) 1); sheet.setFitToPage(true); sheet.setAutobreaks(false); workbook.setPrintArea(workbook.getSheetIndex(sheet), 1, 26, 0, pagelngth[groep.getGrootte()]); sheet.setColumnBreak(18); sheet.protectSheet(password); sheet.enableLocking(); } XSSFSheet sheet4 = workbook.cloneSheet(workbook.getSheetIndex(sheet3), "Deelnemerslijst (naam)"); sortSheet(sheet4, 1, 3, 62); //XSSFSheet sheet5 = workbook.cloneSheet(workbook.getSheetIndex(sheet3), "Deelnemerslijst (rating)"); //sortSheet(sheet5, 1,4); sheet2.protectSheet(password); sheet3.protectSheet(password); sheet4.protectSheet(password); //sheet5.protectSheet(password); // Remove template sheets for (int i = 0; i < 6; i++) { workbook.removeSheetAt(0); } // Close input file file.close(); // Store Excel to new file String filename = "Indeling resultaat.xlsm"; File outputFile = new File(filename); FileOutputStream outFile = new FileOutputStream(outputFile); workbook.write(outFile); // Close output file workbook.close(); outFile.close(); // And open it in the system editor Desktop.getDesktop().open(outputFile); } catch (IOException e) { logger.log(Level.SEVERE, "Fout bij maken indeling excel : " + e.getMessage()); } }
From source file:nmap.parser.ExcelLayout.java
public void Layouting(String nmap, String folderpath) throws FileNotFoundException, IOException { MainList ml = new MainList(); ml.Mainlist(nmap, folderpath);/*from w w w .j a v a 2 s .c o m*/ // System.out.println(ml.getHeadertemp()); //System.out.println(ml.getPopulatefield()); XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet spreadsheet = workbook.createSheet(" PORT STATUS "); //Create row object XSSFRow row; //This data needs to be written (Object[]) //setHeadertemp(header); // setPopulatefield(fieldlayout); // String s = fieldlayout.get(0).toString(); //System.out.println("MainList class: "+s); Map<String, Object[]> empinfo = new TreeMap<String, Object[]>(); empinfo.put("1", ml.getHeadertemp().toArray()); int ind = 2; // empinfo.put("2", fieldlayout.get(0).toArray()); for (int h = 0; h < ml.getPopulatefield().size(); h++) { String index = Integer.toString(ind); empinfo.put(index, ml.getPopulatefield().get(h).toArray()); ind++; } Set<String> keyid = empinfo.keySet(); int rowid = 0; for (String key : keyid) { row = spreadsheet.createRow(rowid++); Object[] objectArr = empinfo.get(key); int cellid = 0; for (Object obj : objectArr) { XSSFCell cell = row.createCell(cellid++); cell.setCellValue((String) obj); } } // Write the workbook in file system String hhaha = "Kingkunta.xlsx"; FileOutputStream out = new FileOutputStream(new File(hhaha)); workbook.write(out); out.close(); System.out.println(hhaha + ".xlsx written successfully"); }