How can i remove blank columns from an excel file through java? - java

I have created a testdata.xlsx which contains data in form of key-value pair. The java code that I have implemented enters a key and the corresponding value below it . In some cases the key-value will be blank , hence creating a blank column . Please suggest how can i remove the blank columns so that the existing data will be available serially without any blank columns between them .
I couldn't find any solution .
NA
NA

If you are sure that file will contain only 2 row (1-Key, 2-Value), then check first 2 cell in each column. If both are null then remove that column using Apache POI API.
If not, you have to check entire column cell for any character.

You can use sheet.shiftColumns(colno + 1, endcolno, -1);.
Or just sheet.setColumnHidden(colno); to hide the column, which is easier when
formulas are involved.
Finding blank columns you probably already did.

Related

JXLS 2.0 Conditional Formatting Forumla not updated per row

Trying to create an JXLS excel template where it should be possible to copy conditional formatting from a cell on a specific row to the next generated row.
In the template, I create my formatting. If the value in the cell is equal to "yes" the row should be red.
Template
Conditional formatting
Formula: =$B2="yes"
Applies to: $A$2:$B$2
I know this formula works on an already populated excel sheet here is an example https://trumpexcel.com/highlight-rows-based-on-cell-value/
But when I do this with my excel template and JXLS 2.0 it fails. It copies the formula as it is to each new generated row. So instead of one condition for the whole sheet, there will now be as many as there are rows. The problem here is that it will copy it as is, which means that the formula in each condition will be based on the value in cell C2. So even if cell C3 is generated with the value "no" it will be red, since it is based on the value in C2.
Output excel
Condition Formatting output excel
Any tips on how to solve this directly in the template?
Using
jxls 2.9.0
jxls-poi 2.9.0
One approach is to modify the formula in the template to acheive what we want.
Formula: =INDIRECT("$B" & ROW())="yes"
Description:
ROW() returns the current row number.
"$B" & ROW() gives the cell reference. For example, at row 5, we will get B5
Finally, using INDIRECT(...) we get the value at cell reference and check if is "yes".
Output excel:
What you are experiencing is standard Excel behaviour. In order to achieve what you want you have 2 options: using a regular Range or a dynamic table. I would use the latter.
Using a regular Range
You need to start with at least 2 rows like this:
and then only insert rows after the first row and before the last row. Never before first or after last. The new rows are picking up the same formatting because the underlying range is expanding. For example, inserting 4 rows in between results in:
Using a dynamic table
Assuming you have headers (you don't need to), you select your start range and then format it as a table:
You will have the option to choose if the table has headers or not via a checkbox in the dialog that will appear.
Then you add the same conditional formatting:
The difference now is that when you add a new row, the conditional formatting will automatically expand. The table itself automatically expands so everyting else (formatting, validation, formulas etc.) are expanding with it.
Just make sure you have the auto expanding option on for tables under File/Options/Proofing/AutoCorrect Options/AutoFormat As You Type/Include new rows and columns in table. You can do that programatically as well (I know in VBA you need to set Application.AutoCorrect.AutoExpandListRange to True). The default is True by the way.
No matter how big your table will get, you will have the formatting expanded.

Getting data from a csv file to a sqlplus table -> getting this data from sqlplus table to html table skeleton

I am a newbie in programming, so please excuse me if some of the stuff I say sounds stupid or not to the point. I am trying to explain what I have to do as best as I understand. I am looking forward to having guidance from all, and hope it solves my problem.
So the steps which I believe I have to undertake is as follows:
I have a CSV file and I have to load data from CSV file into the SQLPLUS table. The file has 17 columns. Now here comes the tricky part, there is one column which has values which correspond to my column number 2 in different rows. For examples, lets say the cell first row of second column is "FRUIT" and the FIRST SECOND and THIRD row of the 14th column are APPLE, MANGO and ORANGE. all three correspond to the fruit so must be shown in the same row when displaying in the html table.
I want to know, how should I configure my control file so that the apple mangoes and oranges must come in the same row as fruits.
I dont want it just for the display purpose because after this is done, I have to retrieve info from the the (apple orange mango column) for the current row and display it as 3 radio buttons.
I am not sure how well I explained it. But please if anyone has any idea please let me know.
This is a sample table:
double quotes mean one row. so its basically fruits then null below and null below.
"|FRUITS|APPLES|"
"| NULL |ORANGES|"
"| NULL |MANGOES|"
What I want to implement is:
|FRUITS|APPLES,ORANGES,MANGOES(displayed one below the other)|
then take the information from the cell in which apples oranges and mangoes are displayed and display it as dropdown selector.
THANKS>.
What I have tried is uploading the csv through sqloader using the str x0d . It displays the fruits in the same row as fruit, however when I am getting the data from the current cell, I cannot extract apples oranges and mangoes separately.
What I believe is I have to introduce some delimiter and then segregate based on that delimiter.

How can I resolve the printing issue for the document that is modified by apache poi

I want to ask a question which is related to apache poi and generated file printing. What I am doing is I am opening a .docx file, inserting some data and saving and printing it.
I am having a file with only 1 table which is something like:
document table
Now, if there are 'n' items, I am generating n-1 rows which copies the format of row 3.
First item is inserted in row 3, if there's another item I am adding a row in the table and saving it likewise I am creating the rows.
Now I want to make this table length = page length. For this I am counting exactly how many rows this page can incorporate and based on that I am inserting empty lines without inside top and bottom border.
The file does not have any header/ footer or any other details as of now.
While I am creating the file I am shown the file which is coming in print preview of covering the whole page of size A4.
But this document is printed only on 80% of paper. I am having 0.3 cm margins but in print the whole page is not printed.
This does not seem to be the printer issue because if other software like Google Chrome are used then it prints the complete page.
So I want to ask that how can I solve this? I tried to search about this but till now I couldn't solve it. I think of this as an issue that whenever I am using XWPFDocument, its margins and all are changed set to default size. I don't know whether this is the issue or something other is, do I need to write some commands to preserve the margins and all or there's something that I need to do.
How can I solve this thing?
Thank you.

why do I get a nullpointerexception while getting an excel cell

I'm getting this exception while getting a cell. This is what I'm doing:
if(versionToAdd.equals("15.6")) {
Cell X = wb.getSheetAt(0).getRow(28+j).getCell((short)7);
Cell Y = wb.getSheetAt(0).getRow(28+j).getCell((short)8);
X.setCellValue(x);
Y.setCellValue(y);
}
this code it's inside of a for loop (j), and every time I iterate over a List of values, i get a new cell dynamically.
The problem is that, whenever it gets to the first col:row (H:29), i get a NullPointerException.
NOTE: The cells in the xls file are in blank... could that be the problem?
I got a collection of X/Y values in java, starts reading it, but whenever it reaches a certain amount of values, I get a NullPointerException in this line:
Cell cellx = row.getCell(8);
The java list is fine.
If I put less X/Y pairs (i.e: 5 or 6 pairs), works great, but over a certain amount (let's say > 10), I get that exception.
Tried looking for the cells, everything seems to be fine
EDITED: Thanks people for your help! I started to use the XSSF/HSSF from POI. Tried to use it before, but I couldn't get it to run properly, but now I did and it works perfect by using the createRow-createCell methods :)
Both org.apache.poi.ss.usermodel.Sheet and org.apache.poi.ss.usermodel.Row are Iterable. If you use the iterator in nested for-each loops, as shown here, you should get only defined rows.
According to the POI documentation, if you ask for a row that is not defined, null is returned.
The same is true for getting a cell from a row. I would assume that either a row or a cell does not exist.
Also, notice that the short version of getCell is deprecated in favor of getCell(int)
POI Sheet
POI Row
You can use getLastRowNum to verify that the number of rows that you are expecting exists. Same it true for getLastCellNum.
If you are creating a sheet / row / cell, you need to use the create methods. Not the get methods.
Create Row
Create Cell
The first three items on the user's guide are: how to create workbook, how to create sheet, how to create cells. You should check the documentation prior to posting questions.
POI Guide

How to copy the content with line breaks from a JTable to Excel with line breaks included

I have an application where I have a JTable with line breaks(pressing Enter key while editing) included in each and every cell.
The problem appears when I copy the contents from my JTable to Excel, I am not getting the contents in same format as in my Jtable(with line breaks) to my excel that is with line breaks.
Is there any way to solve my problem.I have to get the contents from my jtable to excel in the same format with line breaks.
Thank You
Chaithu
I don't know much about DnD. I think that typically you use a delimiter for each cell data (like comma or tab) and then a newline for each row of data.
So if your cell data contains newline characters you would probably need to wrap the cell data in "...". This would mean you need to write a custom TransferHandler for the table. The old TableTransferHandler from the JDK5 tutorial might help you get started.
How are you copying the contents? Are you using the build in copy support?(ie select all contents and copy w/ ctrl-c), or did you write something yourself to do the copy and paste?
You may have to do the latter, and make sure that you insert the necessary line break characters in to the text.

Categories