List of usage examples for org.apache.poi.xssf.usermodel XSSFRow createCell
@Override public XSSFCell createCell(int columnIndex)
From source file:de.tuttas.util.ExcelUtil.java
public static XSSFWorkbook readExcel(String name, String[] sheetNames, int rows, int cols) throws IOException { FileInputStream file = null;//from w ww. j ava 2 s . com try { file = new FileInputStream(new File(name)); Log.d("Template " + name + " gefunden!"); XSSFWorkbook wb = new XSSFWorkbook(file); return wb; } catch (FileNotFoundException ex) { Log.d("Template " + name + " nicht gefunden erzeuge leeres Workbook"); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet[] sheets = new XSSFSheet[sheetNames.length]; for (int k = 0; k < sheetNames.length; k++) { sheets[k] = wb.createSheet(sheetNames[k]); for (int i = 0; i < rows; i++) { XSSFRow hr = sheets[k].createRow(i); for (int j = 0; j < cols; j++) { hr.createCell(j); } } } return wb; } finally { try { if (file != null) { file.close(); } } catch (IOException ex) { Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:dias.Save.java
public void save(Matrix matrice, String filename) throws FileNotFoundException, IOException { ///It is working String dirPath = DIAS.excelFilePath + File.separator + filepath; setupDirectory(dirPath);/* w w w.ja v a 2 s .c o m*/ String fileName = dirPath + File.separator + filename + ".xlsx"; System.out.println("Using filepath " + filepath + ", saving to address: " + fileName); try { FileOutputStream fileOut = new FileOutputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet worksheet = workbook.createSheet("POI Worksheet"); int lastvaluex = matrice.getRowDimension(); int lastvaluey = matrice.getColumnDimension(); int ih = 0; int jh = 0; while (ih < lastvaluex) { XSSFRow row = worksheet.createRow(ih); ih++; while (jh < lastvaluey) { XSSFCell cell = row.createCell(jh); jh++; cell.setCellValue(matrice.get(ih - 1, jh - 1)); } jh = 0; } workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:dias.Save.java
public void savedouble(double matrice, String filename) throws FileNotFoundException, IOException { ///It is working String dirPath = DIAS.excelFilePath + File.separator + filepath; setupDirectory(dirPath);//from w w w. j a v a 2s . c om String fileName = dirPath + File.separator + filename + ".xlsx"; try { FileOutputStream fileOut = new FileOutputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet worksheet = workbook.createSheet("POI Worksheet"); int lastvaluex = 1; int lastvaluey = 1; int ih = 0; int jh = 0; while (ih < lastvaluex) { XSSFRow row = worksheet.createRow(ih); ih++; while (jh < lastvaluey) { XSSFCell cell = row.createCell(jh); jh++; cell.setCellValue(matrice); } jh = 0; } workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:dias.Save.java
public void save3D(Matrix matrice, String filename, int kj) throws FileNotFoundException, IOException { ///It is working String dirPath = DIAS.excelFilePath + File.separator + filepath; setupDirectory(dirPath);// w w w. ja va 2 s. com String fileName = dirPath + File.separator + filename + kj + ".xlsx"; try { FileOutputStream fileOut = new FileOutputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet worksheet = workbook.createSheet("POI Worksheet"); int lastvaluex = matrice.getRowDimension(); int lastvaluey = matrice.getColumnDimension(); int ih = 0; int jh = 0; while (ih < lastvaluex) { XSSFRow row = worksheet.createRow(ih); while (jh < lastvaluey) { XSSFCell cell = row.createCell(jh); cell.setCellValue(matrice.get(ih, jh)); jh++; } ih++; jh = 0; } workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:dias.Save.java
public void saveString(String[] matrice, String filename) throws FileNotFoundException, IOException { ///It is working String dirPath = DIAS.excelFilePath + File.separator + filepath; setupDirectory(dirPath);/*from w w w.j av a 2 s . com*/ String fileName = dirPath + File.separator + filename + ".xlsx"; try { FileOutputStream fileOut = new FileOutputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet worksheet = workbook.createSheet("POI Worksheet"); int lastvaluex = matrice.length; int lastvaluey = 1; int ih = 0; int jh = 0; while (ih < lastvaluex) { XSSFRow row = worksheet.createRow(ih); while (jh < lastvaluey) { XSSFCell cell = row.createCell(jh); cell.setCellValue(matrice[ih]); jh++; } ih++; jh = 0; } workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:edu.cmu.emfta.actions.CutSet.java
License:Open Source License
public XSSFWorkbook toSingleSheetWorkbook() { XSSFWorkbook workbook = new XSSFWorkbook(); int cutSetIdentifier = 0; double cutsetProbability; XSSFSheet sheet = workbook.createSheet(); XSSFTable table = sheet.createTable(); table.setDisplayName("Cutsets"); CTTable cttable = table.getCTTable(); // Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2)); cttable.setRef(reference.formatAsString()); cttable.setId((long) 1); cttable.setName("Cutsets"); cttable.setTotalsRowCount((long) 1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount((long) 3); CTTableColumn column;// w w w . ja va2 s . com XSSFRow row; XSSFCell cell; column = columns.addNewTableColumn(); // Create row row = sheet.createRow(0); CellStyle headingCellStyle = workbook.createCellStyle(); XSSFFont headingFont = workbook.createFont(); headingFont.setBold(true); headingCellStyle.setFont(headingFont); row.setRowStyle(headingCellStyle); CellStyle normalCellStyle = workbook.createCellStyle(); XSSFFont normalFont = workbook.createFont(); normalFont.setBold(false); normalCellStyle.setFont(normalFont); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue("Identifier"); break; } case 1: { cell.setCellValue("Description"); break; } case 2: { cell.setCellValue("Probability"); break; } } } int rowId = 1; for (List<Event> events : cutset) { row = sheet.createRow(rowId++); row = sheet.createRow(rowId++); row.setRowStyle(normalCellStyle); cell = row.createCell(0); cell.setCellValue("Cutset #" + cutSetIdentifier); cutsetProbability = 1; for (int i = 0; i < events.size(); i++) { cutsetProbability = cutsetProbability * events.get(i).getProbability(); } cell = row.createCell(2); if (cutsetProbability != 1) { cell.setCellValue("" + cutsetProbability); } else { cell.setCellValue("" + cutsetProbability); } // System.out.println("[CutSet] cutset id=" + cutSetIdentifier); for (int i = 0; i < events.size(); i++) { Event e = events.get(i); // System.out.println("[CutSet] event name=" + e.getName()); // Create row row = sheet.createRow(rowId++); row.setRowStyle(normalCellStyle); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue(e.getName()); break; } case 1: { cell.setCellValue(e.getDescription()); break; } case 2: { cell.setCellValue(e.getProbability()); break; } } } } cutSetIdentifier = cutSetIdentifier + 1; } return workbook; }
From source file:edu.cmu.emfta.actions.CutSet.java
License:Open Source License
public XSSFWorkbook toMultiSheetsWorkbook() { XSSFWorkbook workbook = new XSSFWorkbook(); int cutSetIdentifier = 0; double cutsetProbability; for (List<Event> events : cutset) { cutsetProbability = 1;//from ww w . j a v a 2 s . co m for (int i = 0; i < events.size(); i++) { cutsetProbability = cutsetProbability * events.get(i).getProbability(); } // System.out.println("[CutSet] cutset id=" + cutSetIdentifier); XSSFSheet sheet = workbook.createSheet(); XSSFTable table = sheet.createTable(); table.setDisplayName("Cutset"); CTTable cttable = table.getCTTable(); // Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2)); cttable.setRef(reference.formatAsString()); cttable.setId((long) 1); cttable.setName("Cutset " + cutSetIdentifier); cttable.setTotalsRowCount((long) 1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount((long) 3); CTTableColumn column; XSSFRow row; XSSFCell cell; column = columns.addNewTableColumn(); // Create row row = sheet.createRow(0); CellStyle headingCellStyle = workbook.createCellStyle(); XSSFFont headingFont = workbook.createFont(); headingFont.setBold(true); headingCellStyle.setFont(headingFont); row.setRowStyle(headingCellStyle); CellStyle normalCellStyle = workbook.createCellStyle(); XSSFFont normalFont = workbook.createFont(); normalFont.setBold(false); normalCellStyle.setFont(normalFont); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue("Identifier"); break; } case 1: { cell.setCellValue("Description"); break; } case 2: { if (cutsetProbability == 1) { cell.setCellValue("Probability"); } else { cell.setCellValue("Probability (" + cutsetProbability + ")"); } break; } } } for (int i = 0; i < events.size(); i++) { Event e = events.get(i); System.out.println("[CutSet] event name=" + e.getName()); // Create column column = columns.addNewTableColumn(); column.setName("Column"); column.setId((long) i + 1); // Create row row = sheet.createRow(i + 1); row.setRowStyle(normalCellStyle); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue(e.getName()); break; } case 1: { cell.setCellValue(e.getDescription()); break; } case 2: { cell.setCellValue(e.getProbability()); break; } } } } cutSetIdentifier = cutSetIdentifier + 1; } return workbook; }
From source file:egovframework.rte.fdl.excel.EgovExcelXSSFServiceTest.java
License:Apache License
/** * [Flow #-3] ? ? : ?? ?(? ?, Border? ?, ? ?, )? */// w ww.j a v a2 s . c o m @Test public void testWriteExcelFileAttribute() throws Exception { try { LOGGER.debug("testWriteExcelFileAttribute start...."); short rowheight = 40 * 10; int columnwidth = 30; StringBuffer sb = new StringBuffer(); sb.append(fileLocation).append("/").append("testWriteExcelFileAttribute.xlsx"); // delete file if (EgovFileUtil.isExistsFile(sb.toString())) { EgovFileUtil.delete(new File(sb.toString())); LOGGER.debug("Delete file....{}", sb.toString()); } XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet1 = wb.createSheet("new sheet"); wb.createSheet("second sheet"); // ? ? sheet1.setDefaultRowHeight(rowheight); sheet1.setDefaultColumnWidth(columnwidth); Font f2 = wb.createFont(); XSSFCellStyle cs = wb.createCellStyle(); cs.setFont(f2); cs.setWrapText(true); // cs.setAlignment(CellStyle.ALIGN_RIGHT); cs.setFillPattern(CellStyle.DIAMONDS); // ? XSSFRow r1 = sheet1.createRow(0); r1.createCell(0); // ? ? cs.setFillForegroundColor(IndexedColors.BLUE.getIndex()); // cs.setFillBackgroundColor(IndexedColors.RED.getIndex()); // sheet1.setDefaultColumnStyle((short) 0, cs); Workbook tmp = excelService.createWorkbook(wb, sb.toString()); Sheet sheetTmp1 = tmp.getSheetAt(0); assertEquals(rowheight, sheetTmp1.getDefaultRowHeight()); assertEquals(columnwidth, sheetTmp1.getDefaultColumnWidth()); CellStyle cs1 = tmp.getCellStyleAt((short) (tmp.getNumCellStyles() - 1)); LOGGER.debug("getAlignment : {}", cs1.getAlignment()); assertEquals(XSSFCellStyle.ALIGN_RIGHT, cs1.getAlignment()); LOGGER.debug("getFillPattern : {}", cs1.getFillPattern()); assertEquals(XSSFCellStyle.DIAMONDS, cs1.getFillPattern()); LOGGER.debug("getFillForegroundColor : {}", cs1.getFillForegroundColor()); LOGGER.debug("getFillBackgroundColor : {}", cs1.getFillBackgroundColor()); LOGGER.debug( "XSSFWorkbook.getFillBackgroundColor(), XSSFColor().getIndexed() ? ? 0 ? ?"); assertEquals(IndexedColors.BLUE.getIndex(), cs1.getFillForegroundColor()); assertEquals(IndexedColors.RED.getIndex(), cs1.getFillBackgroundColor()); } catch (Exception e) { LOGGER.error(e.toString()); throw new Exception(e); } finally { LOGGER.debug("testWriteExcelFileAttribute end...."); } }
From source file:egovframework.rte.fdl.excel.util.AbstractPOIExcelView.java
License:Apache License
/** * Convenient method to obtain the cell in the given sheet, row and column. * * <p>Creates the row and the cell if they still doesn't already exist. * Thus, the column can be passed as an int, the method making the needed downcasts.</p> * * @param sheet a sheet object. The first sheet is usually obtained by workbook.getSheetAt(0) * @param row thr row number//from www . j a v a2 s . c om * @param col the column number * @return the XSSFCell */ protected XSSFCell getCell(XSSFSheet sheet, int row, int col) { XSSFRow sheetRow = sheet.getRow(row); if (sheetRow == null) { sheetRow = sheet.createRow(row); } XSSFCell cell = sheetRow.getCell((short) col); if (cell == null) { cell = sheetRow.createCell((short) col); } return cell; }
From source file:emhs.db.model.GBEntryBook.java
public GBEntryBook(File path) throws IOException, InvalidFormatException { this.path = path; guestRowIdx = staffRowIdx = substituteRowIdx = 1; if (!path.exists()) { workbook = new XSSFWorkbook(); workbook.createSheet("Guests"); workbook.createSheet("Staff"); workbook.createSheet("Substitutes"); guestSheet = workbook.getSheet("Guests"); staffSheet = workbook.getSheet("Staff"); substituteSheet = workbook.getSheet("Substitutes"); guestSheet.setColumnWidth(0, 4096); guestSheet.setColumnWidth(1, 4096); guestSheet.setColumnWidth(2, 4096); guestSheet.setColumnWidth(3, 6144); guestSheet.setColumnWidth(4, 6144); staffSheet.setColumnWidth(0, 4096); staffSheet.setColumnWidth(1, 4096); staffSheet.setColumnWidth(2, 6144); staffSheet.setColumnWidth(3, 6144); substituteSheet.setColumnWidth(0, 4096); substituteSheet.setColumnWidth(1, 4096); substituteSheet.setColumnWidth(2, 4096); substituteSheet.setColumnWidth(3, 4096); substituteSheet.setColumnWidth(4, 4096); substituteSheet.setColumnWidth(5, 4096); substituteSheet.setColumnWidth(6, 4096); flush();/*from w w w . j av a2s . com*/ } else { FileInputStream fileIn = new FileInputStream(path); workbook = new XSSFWorkbook(fileIn); fileIn.close(); guestSheet = workbook.getSheet("Guests"); staffSheet = workbook.getSheet("Staff"); substituteSheet = workbook.getSheet("Substitutes"); guestRowIdx = guestSheet.getPhysicalNumberOfRows(); staffRowIdx = staffSheet.getPhysicalNumberOfRows(); substituteRowIdx = substituteSheet.getPhysicalNumberOfRows(); } XSSFRow guestRow = guestSheet.createRow(0); guestRow.createCell(0).setCellValue("Full Name"); guestRow.createCell(1).setCellValue("Vehicle License"); guestRow.createCell(2).setCellValue("Reason"); guestRow.createCell(3).setCellValue("Sign In Time"); guestRow.createCell(4).setCellValue("Sign Out Time"); XSSFRow staffRow = staffSheet.createRow(0); staffRow.createCell(0).setCellValue("Full Name"); staffRow.createCell(1).setCellValue("Reason"); staffRow.createCell(2).setCellValue("Sign Out Time"); staffRow.createCell(3).setCellValue("Sign In Time"); XSSFRow subRow = substituteSheet.createRow(0); subRow.createCell(0).setCellValue("Full Name"); subRow.createCell(1).setCellValue("Absent Teacher"); subRow.createCell(2).setCellValue("Replacement Time"); subRow.createCell(3).setCellValue("Phone Number"); subRow.createCell(4).setCellValue("Job Number"); subRow.createCell(5).setCellValue("Vehicle License"); subRow.createCell(6).setCellValue("Sign In Time"); }