Workbook wb = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow((short)0); // Create a cell and put a value in it. Cell cell = row.createCell(0); cell.setCellValue(1); // Or do it on one line. row.createCell(1).setCellValue(1.2); row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string")); row.createCell(3).setCellValue(true); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();
try { String filename = "C:/NewExcelFile.xls" ; HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("FirstSheet"); HSSFRow rowhead = sheet.createRow((short)0); rowhead.createCell(0).setCellValue("No."); rowhead.createCell(1).setCellValue("Name"); rowhead.createCell(2).setCellValue("Address"); rowhead.createCell(3).setCellValue("Email"); HSSFRow row = sheet.createRow((short)1); row.createCell(0).setCellValue("1"); row.createCell(1).setCellValue("Sankumarsingh"); row.createCell(3).setCellValue("sankumarsingh@gmail.com"); FileOutputStream fileOut = new FileOutputStream(filename); workbook.write(fileOut); fileOut.close(); System.out.println("Your excel file has been generated!");
} else { System.out.print("reading " + filename + "..."); FileInputStream is = new FileInputStream(filename); HSSFWorkbook wb = new HSSFWorkbook(is); try { System.out.println("done"); for(int i = 0; i < wb.getNumberOfSheets(); i++){ HSSFSheet sheet = wb.getSheetAt(i); if(initDrawing) { /*HSSFPatriarch dg =*/ sheet.getDrawingPatriarch(); String outputFile = filename.replace(".xls", "-saved.xls"); System.out.print("saving to " + outputFile + "..."); os = new FileOutputStream(outputFile); wb.write(os); } finally { os.close(); wb.close(); is.close();
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet realSheet = workbook.createSheet("Sheet xls"); HSSFSheet hidden = workbook.createSheet("hidden"); for (int i = 0, length= countryName.length; i < length; i++) { String name = countryName[i]; HSSFRow row = hidden.createRow(i); HSSFCell cell = row.createCell(0); cell.setCellValue(name); } Name namedCell = workbook.createName(); namedCell.setNameName("hidden"); namedCell.setRefersToFormula("hidden!$A$1:$A$" + countryName.length); DVConstraint constraint = DVConstraint.createFormulaListConstraint("hidden"); CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); HSSFDataValidation validation = new HSSFDataValidation(addressList, constraint); workbook.setSheetHidden(1, true); realSheet.addValidationData(validation); FileOutputStream stream = new FileOutputStream("c:\\range.xls"); workbook.write(stream); stream.close();
HSSFWorkbook hwb = new HSSFWorkbook(); HSSFPalette palette = hwb.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(255, 0, 0); // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here style.setFillForegroundColor(palIndex);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("c:/input.xls")); HSSFSheet sheet = workbook.getSheet("Sheet1"); copyRow(workbook, sheet, 0, 1); FileOutputStream out = new FileOutputStream("c:/output.xls"); workbook.write(out); out.close(); HSSFRow newRow = worksheet.getRow(destinationRowNum); HSSFRow sourceRow = worksheet.getRow(sourceRowNum); worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1); } else { newRow = worksheet.createRow(destinationRowNum); for (int i = 0; i < sourceRow.getLastCellNum(); i++) { HSSFCell oldCell = sourceRow.getCell(i); HSSFCell newCell = newRow.createCell(i); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); newCell.setCellStyle(newCellStyle); if (oldCell.getCellComment() != null) { newCell.setCellComment(oldCell.getCellComment());
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet s = wb.getSheetAt(0); wb.setActiveSheet(0); s.showInPane(0, 0); FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close();
public static void main(String[] args) throws IOException { try (FileInputStream fileIn = new FileInputStream("workbook.xls")) { POIFSFileSystem fs = new POIFSFileSystem(fileIn); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(2); if (row == null) row = sheet.createRow(2); HSSFCell cell = row.getCell(3); if (cell == null) cell = row.createCell(3); cell.setCellType(CellType.STRING); cell.setCellValue("a test"); // Write the output to a file try (FileOutputStream fileOut = new FileOutputStream("workbookout.xls")) { wb.write(fileOut); } } } }
try(FileInputStream fileInputStream = new FileInputStream(uploadFile); HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream); FileOutputStream out = new FileOutputStream(uploadFile)) { HSSFSheet excelSheet = workbook.getSheetAt(0); HSSFCell cell = excelSheet.createRow(0).createCell(0); cell.setCellValue("Imported"); workbook.write(out); } catch(Exception ex) { ex.getCause().printStackTrace(); }
try { FileInputStream fileInput = new FileInputStream("sample.xls"); BufferedInputStream bufferInput = new BufferedInputStream(fileInput); POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput); Biff8EncryptionKey.setCurrentUserPassword("password"); HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true); HSSFSheet sheet = workbook.getSheetAt(0); HSSFRow row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello World"); FileOutputStream fileOut = new FileOutputStream(fname); workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), ""); workbook.write(fileOut); } catch (Exception ex) { }
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet s = wb.getSheetAt(0); s.setActive(true); HSSFRow row = s.getRow(0); HSSFCell cell = row.getCell(0); cell.setAsActiveCell(); FileOutputStream out = new FileOutputStream(file);
File file = new File(....); FileInputStream fis = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet sheet = wb.getSheetAt(0); sheet.shiftRows(3, 3, -1); File outWB = new File(.....); OutputStream out = new FileOutputStream(outWB); wb.write(out); out.flush(); out.close();
try{ FileOutputStream out = new FileOutputStream ("dateFormat.xls"); HSSFWorkbook hssfworkbook = new HSSFWorkbook(); HSSFSheet sheet = hssfworkbook.createSheet ("new sheet"); HSSFCellStyle cs = hssfworkbook.createCellStyle(); HSSFDataFormat df = hssfworkbook. createDataFormat(); cs.setDataFormat(df.getFormat("#,##0.0")); HSSFRow row = sheet.createRow((short)0); HSSFCell cell = row.createCell((short)0); cell.setCellValue(11111.1); cell.setCellStyle(cs); hssfworkbook.write(out); out.close(); }catch(Exception e){} }
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("foo.xls")); HSSFSheet s = wb.getSheetAt(0); HSSFRow r = s.getRow(0); System.out.println("Cell A1 is " + r.getCell(0));
HSSFWorkbook myWorkBook = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream("E:/Project/SpringHibernet/MultiplexTicketBookingNew/web/excelSheets/Country.xlsx"))); HSSFSheet mySheet = myWorkBook.getSheetAt(0); Iterator rowIter = mySheet.rowIterator(); System.out.println(mySheet.getRow(1).getCell(0));
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row; HSSFCell cell; rows = sheet.getPhysicalNumberOfRows(); row = sheet.getRow(i); if(row != null) { tmp = sheet.getRow(i).getPhysicalNumberOfCells(); if(tmp > cols) cols = tmp; if(row != null) { for(int c = 0; c < cols; c++) { cell = row.getCell((short)c); if(cell != null) {
FileInputStream fis = new FileInputStream("/somepath/test.xls"); Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls") Sheet sheet = wb.getSheetAt(0); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); Cell cell = row.getCell(cellReference.getCol()); System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK:
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow((short) 0); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("Default Palette"); HSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = wb.createFont(); font.setColor(HSSFColor.RED.index); style.setFont(font); cell.setCellStyle(style); FileOutputStream out = new FileOutputStream("default_palette.xls"); wb.write(out); out.close(); cell.setCellValue("Modified Palette"); HSSFPalette palette = wb.getCustomPalette(); out = new FileOutputStream("modified_palette.xls"); wb.write(out); out.close();
HSSFWorkbook workBook = new HSSFWorkbook (); HSSFSheet sheet1 = workBook.createSheet(); HSSFRow row1 = sheet1.createRow(10); HSSFCell cell1 = row1.createCell(0); cell1.setCellValue("text: The new line which should be locked"); // SETTING INITIAL VALUE CellRangeAddressList displayNameCellRange = new CellRangeAddressList(displayNameCell.getRowIndex(),displayNameCell.getRowIndex(),displayNameCell.getColumnIndex(),displayNameCell.getColumnIndex()); FileOutputStream fileOut1 = new FileOutputStream("D:\\book.xls"); workBook.write(fileOut1); fileOut1.close();
is = System.in; } else { is = new FileInputStream(cmdArgs.getInputFile()); HSSFWorkbook wb = new HSSFWorkbook(is); is.close(); System.out.println(extractor.getText()); extractor.close(); wb.close();