@Override public RockTourSolution read(File inputSolutionFile) { try (InputStream in = new BufferedInputStream(new FileInputStream(inputSolutionFile))) { XSSFWorkbook workbook = new XSSFWorkbook(in); return new RockTourXlsxReader(workbook).read(); } catch (IOException | RuntimeException e) { throw new IllegalStateException("Failed reading inputSolutionFile (" + inputSolutionFile + ").", e); } }
@Parameterized.Parameters(name = "{4}") public static Collection<Object[]> testSheetParameters() { File testFile = new File(ConferenceSchedulingScoreRulesXlsxTest.class.getResource(testFileName).getFile()); try (InputStream in = new BufferedInputStream(new FileInputStream(testFile))) { XSSFWorkbook workbook = new XSSFWorkbook(in); ConferenceSolution initialSolution = new ConferenceSchedulingXlsxFileIO(false).read(testFile); TestConferenceSchedulingScoreRulesReader reader = new TestConferenceSchedulingScoreRulesReader(workbook, initialSolution); List<Object[]> parametersList = new ArrayList<>(); for (Object[] parameters = reader.nextTestSheetParameters(); parameters != null; parameters = reader.nextTestSheetParameters()) { parametersList.add(parameters); } return parametersList; } catch (IOException | RuntimeException e) { throw new IllegalStateException("Failed reading inputSolutionFile (" + testFile.getName() + ").", e); } }
XSSFWorkbook workbook = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx")); workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
Workbook wb = WorkbookFactory.create(new File("existing.xls")); CellStyle origStyle = wb.getCellStyleAt(1); // Or from a cell Workbook newWB = new XSSFWorkbook(); Sheet sheet = newWB.createSheet(); Row r1 = sheet.createRow(0); Cell c1 = r1.createCell(0); CellStyle newStyle = newWB.createCellStyle(); newStyle.cloneStyleFrom(origStyle); c1.setCellStyle(newStyle); newWB.write(new FileOutpuStream("new.xlsx"));
String file = "c:\\poitest.xlsx"; FileOutputStream outputStream = new FileOutputStream(file); Workbook wb = new XSSFWorkbook(); CellStyle unlockedCellStyle = wb.createCellStyle(); unlockedCellStyle.setLocked(false); Sheet sheet = wb.createSheet(); sheet.protectSheet("password"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("TEST"); cell.setCellStyle(unlockedCellStyle); wb.write(outputStream); outputStream.close();
final String path = "C:/Documents and Settings/eclipse/workspace/CompareExcelV2/"; Workbook outputWorkbook; FileOutputStream outputTo; if (fileA.endsWith(".xls") { outputWorkbook = new HSSFWorkbook(); outputTo = new FileOutputStream(path + "output.xls"); } else { outputWorkbook = new XSSFWorkbook(); outputTo = new FileOutputStream(path + "output.xlsx"); } Workbook workbook1 = WorkbookFactory.create(new File(fileA)); Workbook workbook2 = WorkbookFactory.create(new File(fileB));
public static void main(String[] args) { Path myFile = Paths.get(System.getProperty("user.home"), "Desktop", "tester.xlsx"); try { XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(myFile.toFile())); XSSFSheet sheet = wb.getSheetAt(0); makeRowBold(wb, sheet.getRow(0)); wb.write(new FileOutputStream(myFile.toFile())); } catch (IOException e) { e.printStackTrace(); } } public static void makeRowBold(Workbook wb, Row row){ CellStyle style = wb.createCellStyle();//Create style Font font = wb.createFont();//Create font font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold style.setFont(font);//set it to bold for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row row.getCell(i).setCellStyle(style);//Set the sty;e } }
XSSFWorkbook wb = new XSSFWorkbook(); fileOut = new FileOutputStream("file1.xlsx"); fileOut = new FileOutputStream("file2.xlsx");...
public class SO{ public static void main(String[] args) { try { FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx")); XSSFWorkbook wb = new XSSFWorkbook(is); String header = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"; Sheet sheet = wb.getSheet("Sheet1"); sheet.setColumnWidth(0, 18000); Row row = sheet.createRow(0); Cell cell = row.createCell(0); if(header.length() > 50){ //Length of String for my test sheet.setColumnWidth(0, 18000); //Set column width, you'll probably want to tweak the second int CellStyle style = wb.createCellStyle(); //Create new style style.setWrapText(true); //Set wordwrap cell.setCellStyle(style); //Apply style to cell cell.setCellValue(header); //Write header } wb.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx"))); } catch (IOException e) { e.printStackTrace(); } } }
File file = new File("D:/temp/test.xlsx"); FileInputStream fis = new FileInputStream(file); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sh = wb.getSheet("Hola"); System.out.println(sh.getLastRowNum()); System.out.println("Name: "+sh.getSheetName()); Row row = sh.getRow(1); System.out.println(row.getRowNum()); System.out.println("Val: "+sh.getRow(1).getCell(1).getStringCellValue());
Workbook wb = null; try { wb = new XSSFWorkbook(new File("test.xlsx")); } catch (EncryptedDocumentException e) { // Password protected, try to decrypt and load }
String file = "c:\\poitest.xlsx"; FileOutputStream outputStream = new FileOutputStream(file); Workbook wb = new XSSFWorkbook(); CellStyle unlockedCellStyle = wb.createCellStyle(); unlockedCellStyle.setLocked(false); Sheet sheet = wb.createSheet(); sheet.protectSheet("password"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("TEST"); cell.setCellStyle(unlockedCellStyle); wb.write(outputStream); outputStream.close();
outFile = new File("/myfiles", "table1.xlsx"); try (FileOutputStream fileOut = new FileOutputStream(outFile)) { InputStream is = getClass().getResourceAsStream("/com/myapp/templates/template1.xlsx"); Workbook wb = new XSSFWorkbook(is); Sheet sheet = wb.getSheetAt(0); // Now populate the table here as your application requires wb.write(fileOut); }
@Override public MeetingSchedule read(File inputScheduleFile) { try (InputStream in = new BufferedInputStream(new FileInputStream(inputScheduleFile))) { XSSFWorkbook workbook = new XSSFWorkbook(in); return new MeetingSchedulingXlsxReader(workbook).read(); } catch (IOException | RuntimeException e) { throw new IllegalStateException("Failed reading inputScheduleFile (" + inputScheduleFile + ").", e); } }
//You open an OUTPUT stream into File f here fos = new FileOutputStream(f); try { //And here you try to read again wb = new XSSFWorkbook(new FileInputStream(f));
FileOutputStream out = new FileOutputStream("yourFileName.xls"); Workbook wb = new XSSFWorkbook(); //do your stuff ... wb.write(out);
void method(int i) throws InvalidFormatException, IOException { ... // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); workbook.write(fileOut); fileOut.close(); // Reload the workbook, workaround for bug 49940 // https://issues.apache.org/bugzilla/show_bug.cgi?id=49940 workbook = new XSSFWorkbook(new FileInputStream("workbook.xlsx")); }
FileInputStream file = new FileInputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(file);
Workbook wb; File f = new File("test.xlsx"); if (f.exists) { // Existing workbook, open OPCPackage pkg = OPCPackage.open(f); wb = new XSSFWorkbook(pkg); } else { // Need to create a new, empty workbook wb = new XSSFWorkbook(); }
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row = sheet.createRow(2); Cell cell = row.createCell(2); cell.setCellValue("Use \n with word wrap on to create a new line"); //to enable newlines you need set a cell styles with wrap=true CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); cell.setCellStyle(cs); //increase row height to accomodate two lines of text row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); //adjust column width to fit the content sheet.autoSizeColumn((short)2); FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); wb.write(fileOut); fileOut.close();