I am using Apache POI 4.1 and the cloneSheet() method on top of an XSSFWorkbook to clone a sheet. Unfortunately, comments seem to be properly cloned but without the underlying drawings.
Any code to remove comments from cells on newSheet result in a null pointer exception due to non-existing drawing object instances.
No comments are visible on the new sheet after opening the resulting file in Excel.
final Workbook wb = sourceSheet.getWorkbook();
// duplicate base sheet
Sheet newSheet = wb.cloneSheet(wb.getSheetIndex(sourceSheet));
Related
XSSFSheet sheet = workbook.getXSSFWorkbook().getSheetAt(0);
XSSFSheet pivot_sheet = workbook.getXSSFWorkbook().createSheet("Sheet1");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(new AreaReference("B10:AJ24"), new CellReference("B2"),sheet);
I am getting following error at above line -
java.lang.NullPointerException at
org.apache.poi.xssf.usermodel.XSSFPivotCacheDefinition.createCacheFields(XSSFPivotCacheDefinition.java:145)
at
org.apache.poi.xssf.usermodel.XSSFSheet.createPivotTable(XSSFSheet.java:4065)
I didn't find solution for this using apache poi.
Workaround that I used is mentioned as follows :
Create SXSSFWorkbook as below
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("xyz.xlsm")));
workbook.removeSheetAt(0);
xyz.xlsm is macro enabled excel which holds VBA code for generating pivot
Write in workbook as usual
Pivots will get generated in excel on opening it after download
I am using Apache POI library to protect excel sheet, I am keeping some cells unlocked and locking rest of the cells, additionally I am hiding some columns. But when user select all columns using CTRL+A he/she can copy all data including hidden columns as well.
I found one way in excel which prevents user from selecting protected cells, below is the snapshot for same.
removing tick from "Select locked cell" will prevent user from selecting protected cell, but I am not able to find the method to do the same in code.
How to do this in code using Apache POI?
I am using below code to read workbook object .
Workbook workbook = WorkbookFactory.create(new File(FILE_PATH));
I am able to hide columns, protect sheet and unlock columns, Now I want to prevent user from selecting locked cells.
Instead of using Workbook object, XSSFWorkbook and XSSFSheet allows many controls which I wanted.
XSSFWorkbook workbook = (XSSFWorkbook )WorkbookFactory.create(new File(FILE_PATH));
XSSFSheet sheet = workbook.getSheet(SHEET_NAME);
sheet.enableLocking();
sheet.protectSheet(SHEET_PROTECT_PASSWORD);
sheet.lockSelectLockedCells(true);
Problem: How to copy multiple Excel sheets of different workbook into a single workbook not by cell by cell copying as it makes performance issues as I have large data in sheets.Is there any option to copy whole sheets without iterating over every cell using Java. Just copy the whole sheets into other.
This below will copy every worksheet from a workbook you set. I will copy it and paste it at the end of the list of sheets on your current workbook
Sub CopyWorkbook()
Dim sh as Worksheet, wb as workbook
Set wb = workbooks("Target workbook")
For Each sh in workbooks("source workbook").Worksheets
sh.Copy After:=wb.Sheets(wb.sheets.count)
Next sh
End Sub
I need to check the text format of a cell in .xlsx file (Microsoft Excel file) is strike through or not using Apache POI libraries.
Look following Image
Please Check this image !
I need to check whether B3 Cell text is strike through or not.
Finally I found a way to do this, code as follow
//Using workbook
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("abc.xlsx")));
//Getting first sheet
XSSFSheet sheet = workbook.getSheetAt(0);
//Checking A1 cell that strikethrough or not
boolean strikeOutStatus=sheet.getRow(0).getCell(0).getCellStyle().getFont().getStrikeout();
System.out.println(strikeOutStatus);
Like this:
Get the XSSFRichTextString from the cell using XSSFCell#getRichStringCellValue()
Get the XSSFFont at a specific position using XSSFRichTextString#getFontAtIndex(int index)
Check the font using XSSFFont#getStrikeout()
Not sure how you get a reference to it but there's HSSFont.getStrikeout() and XSSFFont.getStrikeout()
How to read Excel ListBox value in Java using Apache POI ?
On web this is the only thread that discussed abou this problem.
http://apache-poi.1045710.n5.nabble.com/sample-code-to-read-excel-listbox-values-td2308018.html
But this code does not work.
It uses this code
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
HSSFDataValidation dataValidation = null;
try {
inputStream = new java.io.FileInputStream(new java.io.File("C:/temp/data validation.xls"));
workbook = new HSSFWorkbook(inputStream);
sheet = workbook.getSheetAt(0);
validationRecords = sheet.getDVRecords();
But in HSSF version which I am using "getDVRecords" method is not available in HSSFSheet.
Is there any better and working code ?
EDIT:
I already have code to read values of normal cell or dropdown. I am specifically looking for code to read listbox. List box is where you can select more than one values. ListBox are not tied to any specific cell. They appear as components overlaid on the sheet
Please refer this link to see how to add listbox. This will help better understand my question.
http://office.microsoft.com/en-in/excel-help/add-a-list-box-or-combo-box-to-a-worksheet-HP010236681.aspx
ever tried to use the jExcel API? It uses Apache POI and is really easy to handle:
net.sourceforge.jexcelapi:jxl:2.6.12
you can try it somehow like this:
WorkSheet sheet;
Cell comboBox = sheet.getCell(x,y);
String value= comboBox.getContents();
greetings