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();
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!
List having data in this format
[{Row ID=7565.0, Product ID=test11223, Postal Code=98103.0}, {Row ID=7567.0, Product ID=test11213, Postal Code=98101.0}] Having more than 100 row ID record like this, I want to store that data in Excel in below format
Row Id Order ID Postal Code
7565 test11233 98103
7567 test11233 98101
Please help to share the code to write a above list data in excel. thnx
With Apache POI you can write any kind of MS Office files.
Here is a quick example how write cells with it.
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);
}
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();
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 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