single cell read only in excel using java - java

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.

Related

How to lock an Excel cell using Apache POI

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 ?

Lock excel work book using apache poi api java

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);

Apache POI encoding

I use Apache POI 3.13 to generate excel in cyrillic.
HSSFSheet sheet = workbook.createSheet("Лист");
I also tried:
HSSFSheet sheet = workbook.createSheet(
new String("Лист".getBytes(Charset.forName("UTF-8")))
);
But i get this error:
Use XSSFSheet instead of HSSHSheet like this.
XSSFSheet sheet = workbook.createSheet("Лист");
For this use poi-ooxml 3.9.
When saving the java file, there will be a alert message. Click Save as UTF-8.
Here is the image of alert message: screenshot

CellStyle dataformat for XLSX

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.

Out of Memory Error - Java Heap Space while writing to Excel

I have a data of almost 100,000 records and I am trying to write the data to .xlsx file using XSSFWorkbook through Java code. I am able to fetch all the data from database to an ArrayList. By iterating the ArryList, I am writing the data to .xlsx file cell by cell.
As it reaches to 8000 rows, java code throws Out of Memory Heap Space Error.
I have read somewhere that SXSSFWorkbook will be lighter when compared to XSSFWorkbook, so I tried using SXSSFWorkbook. But still I am facing the same problem.
So is there anything that I am missing with the Workbooks or with my Java Code??
Initially, when I have 60,000 records data, I had used .xls file. The same java code is able to generate the .xls file with HSSFWorkbook.
Increasing the Java Heap Space is not at all an option as my data will be increased tremendously in future.
Any help will be greatly appreciated.
Small piece of code, the way I am writing the data to Excel.
int rowNum = sheet.getLastRowNum();
Row lastRow = null ;
Cell cell = null;
ReportingHelperVo reportingHelperVo = null;
for (ReportingVo reportingVo : reportingVos) {
rowNum++;
lastRow = sheet.createRow(rowNum);
reportingHelperVo = reportingVo.reportingHelperVo;
cell = lastRow.createCell(0);
cell.setCellValue(reportingHelperVo.getLocation());
cell.setCellStyle(style);
cell = lastRow.createCell(1);
cell.setCellValue(reportingHelperVo.getCity());
cell.setCellStyle(style);
cell = lastRow.createCell(2);
cell.setCellValue(reportingHelperVo.getCountry());
cell.setCellStyle(style);
}
SXSSFWorkbook is not like light weight,but there is a advantage with this.
If you declare as
SXSSFWorkbook workbook= new SXSSFWorkbook(200);
then for every 200 rows written on the workbook, memory will be flushed to diskspace so there will be no burden in heapspace.
XSSFWorkbook - creates an object representation for all Excel documents (should work like DOM).
SXSSFWorkbook - should require constant memory. When is OOM thrown by JMV? What type of ResultSet did you use? Try to use FORWARD_ONLY to restrict caching data by JDBC driver retrieved from DB.
BTW best weay to fix OutOfMemoryError is to analyze heap dump.
Use -XX:+HeapDumpOnOutOfMemoryError parameter and MAT to understand how your application works.
I am writing the data to .xlsx file cell by cell. As it reaches to
8000 rows, java code throws Out of Memory Heap Space Error.
Re-use exsiting java objects, instead of creating new ones each iteration.
And/or use a csv file instead of excel.
Workbook workBook = new SXSSFWorkbook();
You can export more than 1 lakh (100000) records.
Page results from the database rather than reading them all in one go.
I had similar problems long time ago attempting to write from R to an excel file (but using XLConnection).
In the end, I solved by using write.csv and then opening it with Excel and using the botton "Text to column".
It is increadibly fast and reliable.
I have got the same issue when my excel was reaching 3000 lines. In my case the main memory related issue with POI Excel generation happens with the style sheet. Following are the things which I removed from my code.
Try to use style sheet setting in a row level.
If at all you need to set the style sheet for every cell.. avoid setting border for each and every cell.
Hi Use latest Apache POI JAR//And Use SXSSF for streaming or downloading
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
workbook.setCompressTempFiles(true);
Sheet sh = workbook.createSheet();
((SXSSFSheet) sh).setRandomAccessWindowSize(100);
//write your logic
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;
filename="+filename+".xlsx");
workbook.write(response.getOutputStream());
workbook.close();
workbook.dispose();
I had an out of memory issues writing a XSSFWorkbook to a file.
The Suggestions above helped a lot.
See http://poi.apache.org/components/spreadsheet/how-to.html#xssf_sax_api
Changing XSSFWorkbook wb = new XSSFWorkbook();
to SXSSFWorkbook wb = new SXSSFWorkbook(-1);
SXSSFSheet sh = ... to SXSSFSheet sh = ...
XSSFRow to SXSSFRow
XSSFCell to SXSSFCell
inside the for loop USE sh.flushRows(100); for every 100th row
after wb.write(out);
ADD wb.dispose();

Categories

Resources