List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt
@Override public HSSFSheet getSheetAt(int index)
From source file:egovframework.rte.fdl.excel.EgovExcelSXSSFServiceTest.java
License:Apache License
/** * Data type // w w w . j a v a 2 s . c o m * ? ? Null ? ? */ @Test public void testCellDataFormat() throws Exception { StringBuffer sb = new StringBuffer(); sb.append(fileLocation).append("/").append("testDataFormat.xls"); HSSFWorkbook wb = excelService.loadWorkbook(sb.toString()); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(7); HSSFCell cell = row.getCell(0); assertEquals("2009/04/01", EgovExcelUtil.getValue(cell)); row = sheet.getRow(8); cell = row.getCell(0); assertEquals("2009/04/02", EgovExcelUtil.getValue(cell)); }
From source file:endrov.customData.ImportTable.java
License:BSD License
/** * Import Excel file/*from www .ja va2 s . c o m*/ */ public void importExcel(String filename) throws Exception { rows.clear(); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); HSSFWorkbook wb = new HSSFWorkbook(fs); //Take first sheet HSSFSheet sheet = wb.getSheetAt(0); for (int rowi = 0; sheet.getRow(rowi) != null; rowi++) { HSSFRow row = sheet.getRow(rowi); List<String> a = new LinkedList<String>(); for (int coli = 0; row.getCell((short) coli) != null; coli++) { HSSFCell c = row.getCell((short) coli); if (c.getCellType() == HSSFCell.CELL_TYPE_STRING) a.add(c.getRichStringCellValue().getString()); else if (c.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) a.add("" + c.getNumericCellValue()); } rows.add(a); } }
From source file:endrov.util.io.EvSpreadsheetImporter.java
License:BSD License
/** * Import Excel file/* w ww . j a v a 2 s . c o m*/ */ public void importExcel(String filename) throws Exception { rows.clear(); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); HSSFWorkbook wb = new HSSFWorkbook(fs); //Take first sheet HSSFSheet sheet = wb.getSheetAt(0); int lastCapacity = 0; for (int rowi = 0; sheet.getRow(rowi) != null; rowi++) { HSSFRow row = sheet.getRow(rowi); ArrayList<String> a = new ArrayList<String>(lastCapacity); for (int coli = 0; row.getCell((short) coli) != null; coli++) { HSSFCell c = row.getCell((short) coli); if (c.getCellType() == HSSFCell.CELL_TYPE_STRING) a.add(c.getRichStringCellValue().getString()); else if (c.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) a.add("" + c.getNumericCellValue()); } rows.add(a); lastCapacity = a.size(); } }
From source file:eu.cassandra.training.entities.Installation.java
License:Apache License
/** * This is the parser for the measurement file. It parses through the file and * creates the arrays of the active and reactive power consumptions. */// www . j av a 2 s . co m public void parseMeasurementsFile() throws IOException { ArrayList<Double> temp = new ArrayList<Double>(); ArrayList<Double> temp2 = new ArrayList<Double>(); String extension = measurementsFile.substring(measurementsFile.length() - 3, measurementsFile.length()); switch (extension) { case "csv": boolean startFlag = true; File file = new File(measurementsFile); Scanner scanner = new Scanner(file); int counter = 0; while (scanner.hasNext()) { String line = scanner.nextLine(); // System.out.println(line); if (startFlag) { if (line.split(",")[0].equalsIgnoreCase("1")) { startDate = new DateTime(2012, 01, 01, 00, 00); } else { int year = Integer.parseInt(line.split(",")[0].substring(0, 4)); int month = Integer.parseInt(line.split(",")[0].substring(4, 6)); int day = Integer.parseInt(line.split(",")[0].substring(6, 8)); int hour = 0; int minute = 0; startDate = new DateTime(year, month, day, hour, minute); } // System.out.println(startDate.toString()); startFlag = false; } temp.add(Double.parseDouble(line.split(",")[1])); if (!activeOnly) temp2.add(Double.parseDouble(line.split(",")[2])); counter++; } endDate = startDate.plusMinutes(counter); // System.out.println(endDate.toString()); scanner.close(); break; case "xls": HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(measurementsFile)); // Get the first sheet. HSSFSheet sheet = workbook.getSheetAt(0); for (int i = 0; i < sheet.getLastRowNum(); i++) { // Set value of the first cell. HSSFRow row = sheet.getRow(i + 1); temp.add(row.getCell(1).getNumericCellValue()); if (!activeOnly) temp2.add(row.getCell(2).getNumericCellValue()); } break; } activePower = new double[temp.size()]; for (int i = 0; i < temp.size(); i++) activePower[i] = temp.get(i); if (!activeOnly) { reactivePower = new double[temp2.size()]; for (int i = 0; i < temp2.size(); i++) reactivePower[i] = temp2.get(i); } }
From source file:eu.cassandra.training.utils.Utils.java
License:Apache License
/** * This is the parser for the measurement file. It parses through the file and * checks for errors. It can parse through .csv and .xls file and uses * different libraries for each file type. * // w ww. j a v a2 s.c om * @param measurementsFile * The file name of the measurements file. * @param power * The type of data sets contained within the file (only active or * active and reactive power) * @return the line of error or -1 if no error is found. * @throws IOException */ public static int parseMeasurementsFile(String measurementsFile, boolean power) throws IOException { int result = -1; String extension = measurementsFile.substring(measurementsFile.length() - 3, measurementsFile.length()); switch (extension) { case "csv": File file = new File(measurementsFile); Scanner scanner = new Scanner(file); scanner.nextLine(); int counter = 2; while (scanner.hasNext()) { String line = scanner.nextLine(); String[] testString = line.split(","); if (power) { if (testString.length != 2) result = counter; try { Double.parseDouble(testString[1]); } catch (NumberFormatException e) { result = counter; } } else { if (testString.length != 3) result = counter; try { Double.parseDouble(testString[1]); Double.parseDouble(testString[2]); } catch (NumberFormatException e) { result = counter; } if (result != -1) break; } counter++; } scanner.close(); System.out.println("Your csv file has been read!"); break; case "xls": HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(measurementsFile)); // Get the first sheet. HSSFSheet sheet = workbook.getSheetAt(0); for (int i = 0; i < sheet.getLastRowNum(); i++) { // Set value of the first cell. HSSFRow row = sheet.getRow(i + 1); if (power) { if (row.getCell(2) != null) result = i + 2; try { Double.parseDouble(row.getCell(1).toString()); } catch (NumberFormatException e) { result = i + 2; } } else { if (row.getCell(3) != null) result = i + 2; try { Double.parseDouble(row.getCell(1).toString()); Double.parseDouble(row.getCell(2).toString()); } catch (NumberFormatException e) { result = i + 2; } } if (result != -1) break; } System.out.println("Your excel file has been read!"); break; } return result; }
From source file:eu.itesla_project.entsoe.util.BoundaryPointXlsParser.java
License:Mozilla Public License
public Map<String, BoundaryPoint> parse(InputStream is) throws IOException { Map<String, BoundaryPoint> boundaryPoints = new HashMap<>(); HSSFWorkbook workbook = new HSSFWorkbook(is); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); rowIterator.next();// w w w . ja va2s. c om rowIterator.next(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Cell boundaryPointNameCell = row.getCell(13); Cell borderFromCell = row.getCell(14); Cell borderToCell = row.getCell(15); String boundaryPointName = boundaryPointNameCell.getStringCellValue(); if (boundaryPointName.equals("-")) { continue; } Country borderFrom = toCountry(borderFromCell.getStringCellValue()); Country borderTo = toCountry(borderToCell.getStringCellValue()); boundaryPoints.put(boundaryPointName, new BoundaryPoint(boundaryPointName, borderFrom, borderTo)); } return boundaryPoints; }
From source file:excel.FileExcel.java
public void get_daily_data(String pathfile) { File excelfile = new File(pathfile); updStockData = new ArrayList<>(); if (excelfile.exists()) { try {/*from w w w . j a v a 2 s. c om*/ FileInputStream fis = new FileInputStream(excelfile); HSSFWorkbook workbook = new HSSFWorkbook(fis); HSSFSheet sheet = workbook.getSheetAt(0); int flagfound; for (int i = 10; i < (sheet.getLastRowNum() - 1); i++) { if (!sheet.getRow(i).getCell(13).getStringCellValue().isEmpty()) { if (Integer.valueOf( (int) Math.round(sheet.getRow(i).getCell(18).getNumericCellValue())) != 0) { // if price 0 flagfound = 0; for (int j = 0; j < updStockData.size(); j++) { // in case there are many incoming data for same item if (updStockData.get(j).kode_barang .equals(sheet.getRow(i).getCell(13).getStringCellValue())) { updStockData.get(j).jumlah_barang += Integer.valueOf( (int) Math.round(sheet.getRow(i).getCell(19).getNumericCellValue())); flagfound = 1; break; } } if (flagfound == 0) { // if already found, doesn't need to create new entry StockData constructor = new StockData(); constructor.kode_barang = sheet.getRow(i).getCell(13).getStringCellValue(); constructor.jumlah_barang = Integer.valueOf( (int) Math.round(sheet.getRow(i).getCell(19).getNumericCellValue())); updStockData.add(constructor); } } } } } catch (Exception e) { System.out.println("error : " + e); } } else { System.out.println("error file doesn't exist"); } }
From source file:Excel.LeerExcel.java
public void leerExcel1(String fileName) throws SQLException { tra = new ConeccionLocal(); List cellDataList = new ArrayList(); try {//from ww w . j a v a 2s . c o m /** * Create a new instance for FileInputStream class */ FileInputStream fileInputStream = new FileInputStream(fileName); /** * Create a new instance for POIFSFileSystem class */ POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream); /* * Create a new instance for HSSFWorkBook Class */ HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem); HSSFSheet hssfSheet = workBook.getSheetAt(0); /** * Iterate the rows and cells of the spreadsheet * to get all the datas. */ Iterator rowIterator = hssfSheet.rowIterator(); while (rowIterator.hasNext()) { HSSFRow hssfRow = (HSSFRow) rowIterator.next(); Iterator iterator = hssfRow.cellIterator(); List cellTempList = new ArrayList(); while (iterator.hasNext()) { HSSFCell hssfCell = (HSSFCell) iterator.next(); cellTempList.add(hssfCell); } cellDataList.add(cellTempList); } } catch (Exception e) { e.printStackTrace(); } /** * Call the printToConsole method to print the cell data in the * console. */ printToConsole(cellDataList); }
From source file:excel.PoiWriteExcelFile.java
public static int generarReporte() { //Calendar cal=Calendar.getInstance(); Calendar cal = WorkMonitorUI.instante; try {// w w w . jav a2 s. c om FileOutputStream fileOut = new FileOutputStream("HH_" + instante.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault()) .toUpperCase() + "_" + persona.getNombre().toUpperCase().charAt(0) + "." + persona.getApellido().toUpperCase() + "_" + instante.get(Calendar.YEAR) + ".xls"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet worksheet = workbook.createSheet( cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault()).toUpperCase() + "-" + cal.get(Calendar.YEAR)); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 12); font.setFontName("Calibri"); font.setItalic(false); font.setBold(true); font.setColor(HSSFColor.BLACK.index); cellStyle.setFont(font); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCellStyle diasStyle = workbook.createCellStyle(); diasStyle.setFillForegroundColor(HSSFColor.SEA_GREEN.index); diasStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); font = workbook.createFont(); font.setFontHeightInPoints((short) 11); font.setFontName("Calibri"); font.setItalic(false); font.setBold(true); font.setColor(HSSFColor.WHITE.index); diasStyle.setFont(font); diasStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); diasStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); diasStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); diasStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); diasStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); diasStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCellStyle schedStyle = workbook.createCellStyle(); schedStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); schedStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font3 = workbook.createFont(); font3.setFontHeightInPoints((short) 11); font3.setFontName("Calibri"); font3.setItalic(false); font3.setColor(HSSFColor.BLACK.index); schedStyle.setFont(font3); schedStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); schedStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); schedStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); schedStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); schedStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); schedStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCellStyle workdayStyle = workbook.createCellStyle(); //workdayStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); workdayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); workdayStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); workdayStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); workdayStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); workdayStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); workdayStyle.setWrapText(true); HSSFFont font2 = workbook.createFont(); font2.setFontHeightInPoints((short) 8); font2.setFontName("Serif"); font2.setItalic(false); //font2.setColor(HSSFColor.YELLOW.index); workdayStyle.setFont(font2); workdayStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCellStyle weekendStyle = workbook.createCellStyle(); weekendStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); weekendStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); weekendStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); weekendStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); weekendStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); weekendStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); weekendStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); weekendStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCellStyle horarioStyle = workbook.createCellStyle(); horarioStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); horarioStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); horarioStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); horarioStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); horarioStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); horarioStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); horarioStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); horarioStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font4 = workbook.createFont(); font4.setFontHeightInPoints((short) 10); font4.setFontName("Serif"); font4.setItalic(false); font4.setBold(true); //font2.setColor(HSSFColor.YELLOW.index); horarioStyle.setFont(font4); // index from 0,0... cell A1 is cell(0,0) HSSFRow row1 = worksheet.createRow((short) 0); row1.setHeight((short) 500); //System.out.println("cal.get(Calendar.YEAR)="+cal.get(Calendar.YEAR)); HSSFCell cellA1 = row1.createCell((short) 0); cellA1.setCellValue( cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault()).toUpperCase() + "-" + cal.get(Calendar.YEAR)); cellA1.setCellStyle(cellStyle); HSSFRow row2 = worksheet.createRow((short) 1); HSSFCell cellA4 = row2.createCell((short) 0); cellA4.setCellValue("Horario"); cellA4.setCellStyle(horarioStyle); //row2.setHeight((short)500); HSSFRow row3 = worksheet.createRow((short) 2); HSSFCell cellA3 = row3.createCell((short) 0); cellA3.setCellValue("Inicio - Trmino"); cellA3.setCellStyle(diasStyle); Calendar hora = Calendar.getInstance(); hora.set(Calendar.HOUR_OF_DAY, 9); hora.set(Calendar.MINUTE, 0); hora.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); HSSFCell cellXn; for (int i = 0; i < 29; ++i) { HSSFRow row = worksheet.createRow((short) i + 3); row.setHeight((short) 500); cellXn = row.createCell((short) 0); String horaIni = sdf.format(hora.getTime()); hora.add(Calendar.MINUTE, 30); String horaFin = sdf.format(hora.getTime()); cellXn.setCellValue(horaIni + " - " + horaFin); cellXn.setCellStyle(schedStyle); } System.out.println("cal.get(Calendar.MONTH)1=" + cal.get(Calendar.MONTH)); cal.add(Calendar.DAY_OF_MONTH, -cal.get(Calendar.DAY_OF_MONTH) + 1); int diasMes = cal.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println("cal.get(Calendar.MONTH)2=" + cal.get(Calendar.MONTH)); sdf = new SimpleDateFormat("EEEE d"); System.out.println( "cal.getActualMaximum(Calendar.DAY_OF_MONTH)1=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH)); for (int i = 0; i < diasMes; ++i) { cellXn = row2.createCell((short) i + 1); String dia = sdf.format(cal.getTime()); dia = Character.toUpperCase(dia.charAt(0)) + dia.substring(1); cellXn.setCellValue(dia); cellXn.setCellStyle(horarioStyle); //System.out.println("cal.get(Calendar.DAY_OF_MONTH)="+cal.get(Calendar.DAY_OF_MONTH)); cal.add(Calendar.DAY_OF_MONTH, 1); } for (int i = 0; i < diasMes; ++i) { cellXn = row3.createCell((short) i + 1); cellXn.setCellValue("Descripcin"); cellXn.setCellStyle(diasStyle); } System.out.println( "cal.getActualMaximum(Calendar.DAY_OF_MONTH)2=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH)); // Retroceder mes para que quede como estaba cal.add(Calendar.MONTH, -1); //cal.add(Calendar.DAY_OF_MONTH, -1); System.out.println( "cal.getActualMaximum(Calendar.DAY_OF_MONTH)3=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH)); HhDao hhDao = new HhDao(); Object[][] hh = new Object[29][cal.getActualMaximum(Calendar.DAY_OF_MONTH)]; hh = hhDao.getByMes(WorkMonitorUI.persona.getId(), cal.getTime()); cal.set(Calendar.DAY_OF_MONTH, 1); Sheet sheet = workbook.getSheetAt(0); sdf = new SimpleDateFormat("EEEE"); HSSFPatriarch _drawing = (HSSFPatriarch) sheet.createDrawingPatriarch(); CreationHelper factory = workbook.getCreationHelper(); for (int i = 0; i < 29; ++i) { Row r = sheet.getRow(i + 3); for (int j = 0; j < diasMes; ++j) { if (hh[i][j].toString() != "") { cellXn = (HSSFCell) r.createCell((short) j + 1); Hh _hh = (Hh) hh[i][j]; cellXn.setCellValue( _hh.getTarea().getNombre().trim() + ": " + _hh.getActividad().getNombre().trim()); HSSFAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5); org.apache.poi.ss.usermodel.Comment comment = _drawing.createComment(anchor); String comentario = _hh.getTarea().getComentario().toLowerCase(); if (_hh.getComentario() != null) comentario = comentario + _hh.getComentario().toLowerCase(); RichTextString str = factory.createRichTextString(comentario); comment.setString(str); cellXn.setCellComment(comment); } else { cellXn = (HSSFCell) r.createCell((short) j + 1); cellXn.setCellValue(""); } //System.out.println("sdf.format(cal.getTime())="+sdf.format(cal.getTime())); if (Arrays.asList("sbado", "domingo").contains(sdf.format(cal.getTime()))) cellXn.setCellStyle(weekendStyle); else cellXn.setCellStyle(workdayStyle); sheet.setColumnWidth(j, 5000); cal.add(Calendar.DAY_OF_MONTH, 1); //sheet.autoSizeColumn(j); } // Retroceder mes para que quede como estaba cal.add(Calendar.MONTH, -1); System.out.println("cal.get(Calendar.MONTH)3=" + cal.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, 1); } sheet.setColumnWidth(diasMes, 5000); WorkMonitorUI.instante = Calendar.getInstance(); sheet.setColumnWidth(0, 5000); sheet.createFreezePane(1, 3); // Freeze just one row //sheet.createFreezePane( 0, 1, 0, 1 ); workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return -1; } catch (IOException e) { e.printStackTrace(); return -2; } return 1; }
From source file:excel.Taxonomy.java
/** * Reads the excel document in the file path and sets the extracted values * in the items of the report./*from w ww. j av a 2s. c o m*/ * * @param filePath * @param report */ private List<Description> readFromExcel(String filePath, String[] gaapItems) { List<Description> descriptions = null; try { HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath)); HSSFSheet sheet = wb.getSheetAt(0); ExcelReader reader = new ExcelReader(); descriptions = reader.readDescription(sheet, gaapItems); } catch (IOException ex) { ex.printStackTrace(); } return descriptions; }