Apache poi with Spring AbstractXlsView style border work only for a number of rows












0














I am currently working on a task where I have to export an xls report. Since the project I am working on it uses Spring, for the impl I have used the abstract Class called “AbstractXlsView” provided by Spring. Everything works fine, the files gets exported etc, up to the point of styling the xls file.



One of the requirements is to centre, add borders etc. The problem is that the style (i.e center text, border) is being applied to the xls table only up to "row 43". I used the debugger and the createTableBorders method is being called after row 43 up to the end of my list. But the style is not being applied after row 43 and I don't get it. See also the img attached. enter image description here



Anyone has any idea what is going on or what I am doing wrong?



Component
public class ExportTemplateExcelView extends AbstractXlsView {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {

List<SampleScenarioAndTask> scenarioAndTasks = (List<SampleScenarioAndTask>) model.get("templateToExport");
Sheet sheet = workbook.createSheet(scenarioAndTasks.get(0).getScenarioName());
sheet.setFitToPage(true);

int rowCount = 0;
Row header = sheet.createRow(rowCount++);
header.createCell(0).setCellValue("Some Header1");
header.createCell(1).setCellValue("Some Header2");
header.createCell(2).setCellValue("Some Header3");
createTableBorders(workbook,header);

Row templateRow = null;
for (SampleScenarioAndTask next : scenarioAndTasks) {
templateRow = sheet.createRow(rowCount++);
templateRow.createCell(0).setCellValue(next.getId());
templateRow.createCell(1).setCellValue(next.getContent());
templateRow.createCell(2).setCellValue(next.getType());
createTableBorders(workbook,templateRow);
}
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
}

public static void createTableBorders(Workbook wb, Row row){
CellStyle style = getTableStyle(wb);
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}

public static CellStyle getTableStyle(Workbook wb){
CellStyle style;
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//style.setDataFormat(doubleDataFormat);
return style;
}

}









share|improve this question






















  • Re-use your cell styles! There is a hard limit on how many the file format supports
    – Gagravarr
    Nov 20 '18 at 14:09










  • @ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
    – user2342259
    Nov 20 '18 at 14:20










  • You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
    – Gagravarr
    Nov 20 '18 at 16:07
















0














I am currently working on a task where I have to export an xls report. Since the project I am working on it uses Spring, for the impl I have used the abstract Class called “AbstractXlsView” provided by Spring. Everything works fine, the files gets exported etc, up to the point of styling the xls file.



One of the requirements is to centre, add borders etc. The problem is that the style (i.e center text, border) is being applied to the xls table only up to "row 43". I used the debugger and the createTableBorders method is being called after row 43 up to the end of my list. But the style is not being applied after row 43 and I don't get it. See also the img attached. enter image description here



Anyone has any idea what is going on or what I am doing wrong?



Component
public class ExportTemplateExcelView extends AbstractXlsView {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {

List<SampleScenarioAndTask> scenarioAndTasks = (List<SampleScenarioAndTask>) model.get("templateToExport");
Sheet sheet = workbook.createSheet(scenarioAndTasks.get(0).getScenarioName());
sheet.setFitToPage(true);

int rowCount = 0;
Row header = sheet.createRow(rowCount++);
header.createCell(0).setCellValue("Some Header1");
header.createCell(1).setCellValue("Some Header2");
header.createCell(2).setCellValue("Some Header3");
createTableBorders(workbook,header);

Row templateRow = null;
for (SampleScenarioAndTask next : scenarioAndTasks) {
templateRow = sheet.createRow(rowCount++);
templateRow.createCell(0).setCellValue(next.getId());
templateRow.createCell(1).setCellValue(next.getContent());
templateRow.createCell(2).setCellValue(next.getType());
createTableBorders(workbook,templateRow);
}
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
}

public static void createTableBorders(Workbook wb, Row row){
CellStyle style = getTableStyle(wb);
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}

public static CellStyle getTableStyle(Workbook wb){
CellStyle style;
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//style.setDataFormat(doubleDataFormat);
return style;
}

}









share|improve this question






















  • Re-use your cell styles! There is a hard limit on how many the file format supports
    – Gagravarr
    Nov 20 '18 at 14:09










  • @ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
    – user2342259
    Nov 20 '18 at 14:20










  • You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
    – Gagravarr
    Nov 20 '18 at 16:07














0












0








0







I am currently working on a task where I have to export an xls report. Since the project I am working on it uses Spring, for the impl I have used the abstract Class called “AbstractXlsView” provided by Spring. Everything works fine, the files gets exported etc, up to the point of styling the xls file.



One of the requirements is to centre, add borders etc. The problem is that the style (i.e center text, border) is being applied to the xls table only up to "row 43". I used the debugger and the createTableBorders method is being called after row 43 up to the end of my list. But the style is not being applied after row 43 and I don't get it. See also the img attached. enter image description here



Anyone has any idea what is going on or what I am doing wrong?



Component
public class ExportTemplateExcelView extends AbstractXlsView {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {

List<SampleScenarioAndTask> scenarioAndTasks = (List<SampleScenarioAndTask>) model.get("templateToExport");
Sheet sheet = workbook.createSheet(scenarioAndTasks.get(0).getScenarioName());
sheet.setFitToPage(true);

int rowCount = 0;
Row header = sheet.createRow(rowCount++);
header.createCell(0).setCellValue("Some Header1");
header.createCell(1).setCellValue("Some Header2");
header.createCell(2).setCellValue("Some Header3");
createTableBorders(workbook,header);

Row templateRow = null;
for (SampleScenarioAndTask next : scenarioAndTasks) {
templateRow = sheet.createRow(rowCount++);
templateRow.createCell(0).setCellValue(next.getId());
templateRow.createCell(1).setCellValue(next.getContent());
templateRow.createCell(2).setCellValue(next.getType());
createTableBorders(workbook,templateRow);
}
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
}

public static void createTableBorders(Workbook wb, Row row){
CellStyle style = getTableStyle(wb);
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}

public static CellStyle getTableStyle(Workbook wb){
CellStyle style;
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//style.setDataFormat(doubleDataFormat);
return style;
}

}









share|improve this question













I am currently working on a task where I have to export an xls report. Since the project I am working on it uses Spring, for the impl I have used the abstract Class called “AbstractXlsView” provided by Spring. Everything works fine, the files gets exported etc, up to the point of styling the xls file.



One of the requirements is to centre, add borders etc. The problem is that the style (i.e center text, border) is being applied to the xls table only up to "row 43". I used the debugger and the createTableBorders method is being called after row 43 up to the end of my list. But the style is not being applied after row 43 and I don't get it. See also the img attached. enter image description here



Anyone has any idea what is going on or what I am doing wrong?



Component
public class ExportTemplateExcelView extends AbstractXlsView {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {

List<SampleScenarioAndTask> scenarioAndTasks = (List<SampleScenarioAndTask>) model.get("templateToExport");
Sheet sheet = workbook.createSheet(scenarioAndTasks.get(0).getScenarioName());
sheet.setFitToPage(true);

int rowCount = 0;
Row header = sheet.createRow(rowCount++);
header.createCell(0).setCellValue("Some Header1");
header.createCell(1).setCellValue("Some Header2");
header.createCell(2).setCellValue("Some Header3");
createTableBorders(workbook,header);

Row templateRow = null;
for (SampleScenarioAndTask next : scenarioAndTasks) {
templateRow = sheet.createRow(rowCount++);
templateRow.createCell(0).setCellValue(next.getId());
templateRow.createCell(1).setCellValue(next.getContent());
templateRow.createCell(2).setCellValue(next.getType());
createTableBorders(workbook,templateRow);
}
response.setHeader("Content-Disposition", "attachment; filename=template.xls");
}

public static void createTableBorders(Workbook wb, Row row){
CellStyle style = getTableStyle(wb);
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}

public static CellStyle getTableStyle(Workbook wb){
CellStyle style;
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//style.setDataFormat(doubleDataFormat);
return style;
}

}






java excel spring-mvc apache-poi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 12:22









user2342259

1022314




1022314












  • Re-use your cell styles! There is a hard limit on how many the file format supports
    – Gagravarr
    Nov 20 '18 at 14:09










  • @ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
    – user2342259
    Nov 20 '18 at 14:20










  • You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
    – Gagravarr
    Nov 20 '18 at 16:07


















  • Re-use your cell styles! There is a hard limit on how many the file format supports
    – Gagravarr
    Nov 20 '18 at 14:09










  • @ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
    – user2342259
    Nov 20 '18 at 14:20










  • You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
    – Gagravarr
    Nov 20 '18 at 16:07
















Re-use your cell styles! There is a hard limit on how many the file format supports
– Gagravarr
Nov 20 '18 at 14:09




Re-use your cell styles! There is a hard limit on how many the file format supports
– Gagravarr
Nov 20 '18 at 14:09












@ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
– user2342259
Nov 20 '18 at 14:20




@ Gagravarr Sorry but what you mean by reusing ? I mean, I have only one method getTableStyle which return the style applied to the cells..
– user2342259
Nov 20 '18 at 14:20












You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
– Gagravarr
Nov 20 '18 at 16:07




You seem to be creating a new cell style for each row. Don't! Create it once and re-use for every row
– Gagravarr
Nov 20 '18 at 16:07












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392908%2fapache-poi-with-spring-abstractxlsview-style-border-work-only-for-a-number-of-ro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392908%2fapache-poi-with-spring-abstractxlsview-style-border-work-only-for-a-number-of-ro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out