Apache FOP: PDF converted from POI HSSF (.xls) has no cell padding - apache-poi

I am creating HSSF Excel spreadsheet using Apache POI (version 5.2.3):
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle tableDefaultStyle = workbook.createCellStyle();
tableDefaultStyle.setBorderBottom(BorderStyle.THIN);
tableDefaultStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex());
tableDefaultStyle.setBorderTop(BorderStyle.THIN);
tableDefaultStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex());
tableDefaultStyle.setBorderRight(BorderStyle.THIN);
tableDefaultStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex());
tableDefaultStyle.setBorderLeft(BorderStyle.THIN);
tableDefaultStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex());
CellStyle rightAligned = workbook.createCellStyle();
rightAligned.cloneStyleFrom(tableDefaultStyle);
rightAligned.setAlignment(HorizontalAlignment.RIGHT);
CellStyle leftAligned = workbook.createCellStyle();
leftAligned.cloneStyleFrom(tableDefaultStyle);
leftAligned.setAlignment(HorizontalAlignment.LEFT);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell_r = row.createCell(0);
cell_r.setCellStyle(rightAligned);
cell_r.setCellValue("right aligned");
sheet.setColumnWidth(0, 3000);
Cell cell_l = row.createCell(1);
cell_l.setCellStyle(leftAligned);
cell_l.setCellValue("left aligned");
sheet.setColumnWidth(1, 3000);
This is working fine:
Then I use ExcelToFoConverter to get a FO document:
Document foDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument();
ExcelToFoConverter converter = new ExcelToFoConverter(foDocument);
converter.processWorkbook(workbook);
Finally I use Apache FOP to get PDF file:
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.getOutputStream());
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Source src = new DOMSource(foDocument);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
The problem is that the resulting PDF file is hardly readable because rendered table cells have no padding:
According to the setColumnWidth documentation there are 2 pixels padding on both left and right side of each column, which seems to be just enough.
How to preserve padding in PDF? Or how to add padding in FO? Or how to make table in PDF more readable?

Related

How to update excel cell by adding data to new line of the existing cell data using apache poi?

I need to update existing cell value by adding new lines to it during testing through selenium. I am not able to figure out code to update exising data using POI. When I tried to add using New Line Character, it is overwriting existing data. Please provide solution to add value to existing cell data on new line.
Below is the code what I have for new Line comment
try (OutputStream fileOut = new FileOutputStream("MyFile.xls")) {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("This is first line and \n this is second line");
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
sheet.autoSizeColumn(2);
wb.write(fileOut);
Cell cell2 = row.getCell(1);
cell2.setCellValue(" \n this is third line");
CellStyle cs2 = wb.createCellStyle();
cs2.setWrapText(true);
cell2.setCellStyle(cs2);
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
sheet.autoSizeColumn(2);
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}

NPOI XWPF set table column width not working

I've been trying to set the column width for my table, but it won't budge. I'm using NPOI from this git https://github.com/nissl-lab/npoi.
Currently, my code is:
FileStream stream = new FileStream(reportPath, FileMode.Open);
XWPFDocument doc = new XWPFDocument(stream);
XWPFTable table;
table = doc.CreateTable(1, 3);
table.SetColumnWidth(0, (ulong)1000);
table.SetColumnWidth(1, (ulong)2000);
table.SetColumnWidth(2, (ulong)3000);
using (var f = File.Create(reportPath)) doc.Write(f);
And the generated columns are in the same size having the smallest column size possible. Also, I'm trying to generate a DOCX file.
You have to set the table width type to make it work.
XWPFTable table1 = doc.CreateTable(1, 3);
var tblLayout1 = table1.GetCTTbl().tblPr.AddNewTblLayout();
tblLayout1.type = ST_TblLayoutType.#fixed;
table1.SetColumnWidth(0, 1000);
table1.SetColumnWidth(1, 2000);
table1.SetColumnWidth(2, 3000);
The above code works in NPOI 2.5.4

How to generate an excel file and send it in the response using spring boot

I have a list of employee records in a table, i want to generate report out of it. Is there a way to generate and send the generated report in excel format to the browser in response.FYI I'm using Spring boot and reactjs.
You probably want to look at Apache POI
There are some examples of creating XLS[X] files in their developers guide
A quick example from there
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(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
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}

Aspose Slides Table Cell Insert HTML content

I am working with Aspose slides to generate PPT in my application, I ran into a situation where I need to insert HTML text into Table Cell, I verified all blogs no one given answer to me. If any body know here please let me know. Thanks In advance.
You can use the TextFrame's paragraph associated with each cell to insert HTML using Aspose.Slides for .NET. Check the following code:
//Instantiate Presentation class that represents PPTX file
using (Presentation pres = new Presentation())
{
//Access first slide
ISlide sld = pres.Slides[0];
//Define columns with widths and rows with heights
double[] dblCols = { 250, 250};
double[] dblRows = { 150, 130, 130 };
//Add table shape to slide
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);
//Set border format for each cell
foreach (IRow row in tbl.Rows)
foreach (ICell cell in row)
{
cell.BorderTop.FillFormat.FillType = FillType.Solid;
cell.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cell.BorderTop.Width = 5;
cell.BorderBottom.FillFormat.FillType = FillType.Solid;
cell.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cell.BorderBottom.Width = 5;
cell.BorderLeft.FillFormat.FillType = FillType.Solid;
cell.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cell.BorderLeft.Width = 5;
cell.BorderRight.FillFormat.FillType = FillType.Solid;
cell.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cell.BorderRight.Width = 5;
}
//Adding html text in text frame
tbl[0, 0].TextFrame.Paragraphs.AddFromHtml(#"<html><body><p><b>This text is bold</b></p>
<p><i>This text is italic</i></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>
</body></html>");
//Write PPTX to Disk
pres.Save("d:\\data\\table_html.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
P.S. I am working as social media developer at Aspose.

Save numeric in number category using apache POI

I need to store numeric in number category (right click->Catergory=number). I have tried using the below code, but it saves in general format.
String valueAsString = "2345";
HSSFCell cellE1 = row1.createCell((short) 4);
cellE1.setCellValue(new BigDecimal(valueAsString).doubleValue());
You need to set a cell style to the cell, which formats it as you want. Something like
Worbook wb = new HSSFWorkbook();
DataFormat fmts = wb.getCreationHelper().createDataFormat();
// Cell Styles apply to the whole workbook, only create once
CellStyle numericStyle = wb.createCellStyle();
numericStyle.setDataFormat(fmts.getFormat("0")); // Format string
....
// Apply to the cells
Row r = sheet.createRow(0);
Cell c = r.createCell(0); // A1
c.setCellStyle(numericStyle);
c.setCellValue(Double.parseDouble("12345"));

Resources