Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void write(PreparedExcelData preparedExcelData) {
- File file = new File(preparedExcelData.getFileName());
- String sheetName = preparedExcelData.getSheetName();
- try (Workbook book = new XSSFWorkbook(); ByteArrayOutputStream bos = new ByteArrayOutputStream()){
- Sheet sheet = book.createSheet(sheetName);
- Row titleRow = sheet.createRow(0);
- RowContext headers = preparedExcelData.getHeader().getData();
- if(headers.getRowStyle() != null){
- titleRow.setRowStyle(headers.getRowStyle());
- }
- for (int i = 0; i < headers.size(); i++) {
- Cell nameColumn = titleRow.createCell(i);
- CellContext cellContext = headers.getCell(i);
- if(cellContext.getCellStyle() != null){
- nameColumn.setCellStyle(cellContext.getCellStyle());
- }
- nameColumn.setCellValue(cellContext.getValue());
- }
- List<RowContext> data = preparedExcelData.getBody().getData();
- for (int i = 0; i < data.size(); i++) {
- Row row = sheet.createRow(i + 1);
- RowContext rowContext = data.get(i);
- if(rowContext.size() != headers.size()){
- throw new IllegalArgumentException("Body size don't equals with header size");
- }
- if(rowContext.getRowStyle() != null){
- row.setRowStyle(rowContext.getRowStyle());
- }
- for (int j = 0; j < headers.size(); j++){
- Cell cell = row.createCell(j);
- CellContext cellContext = rowContext.getCell(j);
- if(cellContext.getCellStyle() != null){
- cell.setCellStyle(cellContext.getCellStyle());
- }
- cell.setCellValue(cellContext.getValue());
- }
- }
- book.write(bos);
- FileUtils.writeByteArrayToFile(file, bos.toByteArray());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement