I have a situation where I will be reading multiple lines and after some logic I need to write the lines in an Excel Sheet. I am using Apache POI for this purpose. However, the problem that I am facing is that, only the last line (from the loop) is being written to the Excel
Can someone please help me on this or provide some code snippet?
Thanks
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();
Using newlines in cells
Related
I am working to generate excel using java apache poi
i just need to beautify it (with border)
below is the excel that i have successfuly create
and here is the excel that i wanted (see those border and currency and background color)
heres some of my code to generate the excel
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(rowIndex);
row.createCell(0).setCellValue("Product Name");
row.createCell(1).setCellValue("name");
FileOutputStream fileOut = new FileOutputStream("excel.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
I assume you'd need to break down the creation of your cell in this format first before applying any style onto it:
Cell cell1 = row.createCell(0);
cell1.setCellValue("Product Name");
Later,
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderTop((short) 1); // single line border
cellStyle.setBorderBottom((short) 1); // single line border
...//add many others here
cell1.setCellStyle(cellStyle); //apply that style to the cell
Simple way is to create a cellStyle at first and then just go ahead with numerous cell creations as per the application requirement! Next, just loop into each cell to apply the cellStyle if it is a common behavior that you need for all.
Hope that helps!
I have been testing an application where i have to write a set of results to the excel file, the values are taken from the script. But the problem is that first I have to set the column headings like "Sl No" and "Name" after that I have to print the values against the particular cells. The thing is that am not able to write the results under the particular box.
I am new to apache poi. Can anyone help me with this issue.
Code for setting the excel and headings are shown below,
String FileName = "C://Users//Desktop//filename.xlsx";
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("Distributor");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Sl No");
Cell cell2 = row.createCell(1);
cell2.setCellValue("Name");
FileOutputStream outputStream = new FileOutputStream(new File(FileName));
workBook.write(outputStream);
outputStream.close();
This question already has answers here:
Multiline text in Excel cells
(3 answers)
Closed 7 years ago.
I have got a problem to create new lines into a cell. When I create a file, the new lines aren't shown in correct way. I hope someone can help me to solve this issue.
cell.setCellValue(text);
if(text.indexOf(System.getProperty("line.separator")) != -1)
{
int countRows = text.split(System.getProperty("line.separator")).length;
style.setWrapText(true);
cell.setCellStyle(style);
row.setHeightInPoints((countRows * sheet.getDefaultRowHeightInPoints()));
sheet.autoSizeColumn((short) countRows);
}
This is my Excel-File
When I make a new Excel-File, all the cells are looking like cell A1. There isn't a new line. When I click on the cell A1 shows the description over the cell the content with the new lines.
I made a double click in cell A2 and the cell is showing the content in the correct way.
The new line tag seems to work. My issue is, that the new line isn't shown at the cell in the excel file. But it is shown at the cell description. Is there a way to show all of the cells like the cell A2 without a double click?
Thanks
\n should be \r\n for excel files.
This worked for me ,
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 0);
row.setHeight((short) (2*sheet.getDefaultRowHeight()));
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
Cell cell = row.createCell(0);
cell.setCellStyle(cs);
cell.setCellValue(
createHelper.createRichTextString("This is \n a string"));
wb.write(fileOut);
fileOut.close();
How can i make all cells in a sheet unlocked by default?
This snippet creates a sheet with all cells locked
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
cell.setCellValue("test");
This snippet causes some corrupt data when trying to open the excel file
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setLocked(false);
cell.setCellStyle(cellStyle);
cell.setCellValue("test");
This snippet doesn't cause any problems, but it also does unlock the cells. I can modify all the cells, but if i click on "format", the "Lock Cells" link is highlighted.
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(rowCounter);
Cell cell = row.createCell(counter);
cell.setCellValue("test");
cell.getCellStyle().setLocked(false);
Your first snippet does not produce a document with locked cells using POI 3.9.
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("test");
FileOutputStream fileOut = new FileOutputStream("/tmp/test.xlsx");
wb.write(fileOut);
fileOut.close();
Cells are editable in the resulting workbook, as expected.
SXSSF is the streaming POI API. It's optimized for writing huge amounts of data to XML based spreadsheets (the normal XSSF api keeps XML docs in memory and can use huge amounts of memory when building large documents). If you don't need to output large XML based documents, I'd recommend just using the XSSF api as SXSSF has several restrictions and gotchas and isn't as well documented.
See also:
SXSSF Documentation
POI Quick Start
I want to create a Excel in which only a specific column is locked(Read-only), and the rest are editable,
I am using the following approach, but that doesn't seem to work.
Create two CellStyles, one with setLocked(true) and other with setLocked(false).
Then apply the locked style for all the cells in the column which needs to be locked, and the unlocked style for all the other cells.
Protect the sheet using sheet.protectSheet("");
But when I open the created Excel in open office, I notice that all the cells are locked!
None of them are editable.
How can I achieve the above requirement?
P.S : I cant use the data validation approach.
If you do the opposite it works. Protect the whole sheet and call setLocked(false) for the cells which should be editable.
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();