I want to lock an Excel cell (none-editable cell) using Apache POI. I'm using HSSFWorkbook and here what I've tried so far.
First I created a style.
Font mystyle = workbook.createFont();
headerFont.setFontHeightInPoints((short) 11);
style = workbook.createCellStyle();
style.setLocked(true);
Then I added it like below.
resultCell.setCellValue("mystyle");
resultCell.setCellStyle((CellStyle) formatter.styles.get("mystyle"));
But it doesn't lock the cell, still I can edit the cell. Is there any other way to achieve this ?
Related
I am trying to parse an excel file which is having some columns which are in Custom format.I have to convert them into Text before parsing.I am trying to implement a condition that if the celltype is blank and custom it will be converted to text format ,
I am trying this code but not working
Workbook workbook = excelManager.getWorkbook();
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(fmt.getFormat("#"));
But I think this # will not work for all custom cells. Can anyone please help me? I am using poi 3.16 and version can not be changed.
I have to lock a workbook while reading and writing a row.
Is there way to accomplish this using Apache POI API without protecting and password option?.
As a suggestion, paste some of your code that could help others to understand and solve the problem.
As far as I know you can't "lock" the hole Workbook but one thing you can try is to apply a CellStyle to cells you want to protect while reading or writing.
Workbook wb = new XSSFWorkbook();
CellStyle lockedCellStyle = wb.createCellStyle();
lockedCellStyle.setLocked(true);
Sheet sheet = wb.createSheet();
// .... Create rows and cells as needed
// When Writing or reading
Cell cell = getCellsToLockWithAnyMethod();
cell.setCellStyle(lockedCellStyle);
I have some code like :
CellStyle cs2 = wb.createCellStyle();
CellStyle cs4 = wb.createCellStyle();
cs4.setDataFormat(HSSFDataFormat.getBuiltinFormat("CELL_TYPE_NUMERIC"));
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
this is for creating xls reports. How do i change this code to create XLSX reports ?
will the following work ?
XSSFDataFormat format = (XSSFDataFormat) wb.createDataFormat();
cs2.setDataFormat(format.getFormat("text"));
Please help.
Thanks
Yes, built-in formats will still work with .xlsx spreadsheets in Apache POI.
Even if the Javadocs for XSSFDataFormat#getFormat(String) don't mention it, the source code tells all:
public short getFormat(String format) {
int idx = BuiltinFormats.getBuiltinFormat(format);
if(idx == -1) idx = stylesSource.putNumberFormat(format);
return (short)idx;
}
It will look up the data format in the BuiltinFormats object first, and if not found, it will create a new one.
I've tested this on creating a .xlsx spreadsheet, and using "text" works. I created numeric cells, and I set those cells to a CellStyle with a XSSFDataFormat created with "text", and the cells are "text"-styled in the resultant spreadsheet.
I am creating an excel using Apache POI class. I have a column with data as 07898.
When I opened the excel the data is showing properly with 07898 with a warning message. When I click the mouse inside the excel is considering it as number and removing the 0 automatically.
But if I change the column type as text in Microsoft excel, it is not removing the 0. But I am not able to set the column type as Text. It is always Generic.
Any suggestion please let me know. Thank you.
I am setting the type as String, but it is not working properly.
SXSSFWorkbook hwb = new SXSSFWorkbook(100);
Sheet sheet = hwb.createSheet(strReportName);
Cell excelheadercell = excelheader.createCell((short) icolumns);
excelheadercell.setCellValue("07898");
CellStyle cellstyle = hwb.createCellStyle();
Font font = hwb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
excelheadercell.setCellStyle(cellstyle);
**excelheadercell.setCellType(Cell.CELL_TYPE_STRING);**
How can I make read only cell in Excel using Java ?
You may use Apache POI library to achieve this. Here is a simple code sample:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Test");
Row row = sheet.createRow(0);
CellStyle style = wb.createCellStyle();
style.setLocked(true);
cell = row.createCell(0);
cell.setCellStyle(style);
// this is important as locking is pnly activated if sheet is protected
sheet.protectSheet("");
Maybe you can try jExcel. It is quite simple and easy to use.
Take a look at the Apache POI project, specifically the HSSF & XSSF subprojects, that provides a Java library to manipulate Excel documents.