I try to get the width of each cell and then calculate their greatest common divisor to get the colspan of each cell, but how do I get the cell Rowspan? I tried using the method TableRow.getRowHeight(), but no matter what the table looks like, it's always going to be 0.
I am trying to use this method to convert to HTML, if you have a better way to convert complex tables to HTML, please help me, thanks!
public static Integer getRowspan(TableCell cell, int rowNum, int colNum, Table table) {
int rowSpan = 0;
if (cell.isVerticallyMerged()) {
if (cell.isFirstVerticallyMerged()) {
for (int i = rowNum + 1; i < table.numRows(); i++) {
TableCell tableCell = table.getRow(i).getCell(colNum);
if (tableCell.isFirstVerticallyMerged() || !tableCell.isVerticallyMerged()) {
rowSpan = i - rowNum;
break;
} else if (i == table.numRows() - 1) {
rowSpan = i - rowNum + 1;
}
}
}
}
return rowSpan;
}
public static Integer getColspan(TableCell cell, int rowNum, int colNum, Table table) {
int colSpan = 0;
if (cell.isMerged()) {
if (cell.isFirstMerged()) {
for (int i = colNum + 1; i < table.numRows(); i++) {
TableCell tableCell = table.getRow(rowNum).getCell(i);
if (tableCell.isFirstMerged() || !tableCell.isMerged()) {
colSpan = i - colNum;
break;
} else if (i == table.numRows() - 1) {
colSpan = i - colNum + 1;
}
}
}
}
return colSpan;
}
Related
I don't know how to delete row without leaving an empty row.
I am using Apache POI 3.9 and I am getting an error using the following code:
public List<MeterInfo> addToList(String patternt) throws ParseException, IOException {
List<Object> data = new ArrayList<Object>();
int lastRowNum = sheet.getLastRowNum();
Row row;
for(int i = 0; i < lastRowNum; i++){
row = sheet.getRow(i);
if(patternt.equals(getCurrentString(row))){
data.add(getDataFromRow(row));
sheet.removeRow(row);
sheet.shiftRows(row.getRowNum() + 1, row.getRowNum() + 1, -1);
}
}
saveWorkbook(new File("blabla.xlsx"));
return data;
}
I've found a solution in https://stackoverflow.com/a/3554129/6812826, but I am getting a NullPointerException because I am decreasing lastRowNum by each deleted row.
Here is the new version:
public List<Object> addToList(String pattern) throws ParseException, IOException {
List<Object> data= new ArrayList<Object>();
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (pattern.equals(getCurrentString(row))) {
data.add(getMeterInfo(row));
deleteRow(row);
}
}
saveWorkbook(new File("blabla.xlsx"));
return data;
}
private void deleteRow(Row row) {
int lastRowNum = sheet.getLastRowNum();
int rowIndex = row.getRowNum();
if(rowIndex >= 0 && rowIndex < lastRowNum){
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if(rowIndex == lastRowNum){
Row removingRow = sheet.getRow(rowIndex);
if(removingRow != null){
sheet.removeRow(removingRow);
}
}
}
Try this code, it should work:
for(int i = 0; i < sheet.getLastRowNum(); i++)
{
row = sheet.getRow(i);
if(patternt.equals(getCurrentString(row)))
{
data.add(getDataFromRow(row));
// sheet.removeRow(row); NO NEED FOR THIS LINE
sheet.shiftRows(row.getRowNum() + 1, sheet.getLastRowNum() + 1, -1);
i--;
}
}
You need to decrease i by one every time you delete one row. And get the last row number again by using getLastRowNum().
I have created an excel sheet, where I am inserting the value to cell and merging cells vertically at the same time and I am able to achieve that but the problem is that, I am not able to align text vertically center in the merged cell.Currently, text is showing at the bottom of vertically merged cell.
here's my code,
CellStyle cs = null;
cs = wb.createCellStyle();
cs.setWrapText(true);
//cs.setAlignment(CellStyle.ALIGN_CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFont(f);
Row row[]=new Row[pageListAcrToDept.size()];
for (int i = 0; i < pageListAcrToDept.size(); i++) {
rowIndex = 8 + i ;
row[i]=sheet.createRow(rowIndex);
}
List<List<String>> datas=new ArrayList<>();
datas.add(pageListAcrToDept);
datas.add(notesListAcrToDept);
datas.add(treatmentListAcrToDept);
datas.add(upcListAcrToDept);
datas.add(itemCodeListAcrToDept);
datas.add(descListAcrToDept);
datas.add(subDeptListAcrToDept);
datas.add(limitListAcrToDept);
datas.add(XforListAcrToDept);
datas.add(priceListAcrToDept);
datas.add(couponListAcrToDept);
datas.add(adzoneListAcrToDept);
datas.add(promoDescListAcrToDept);
for (int column = 0; column < 13; column++) {
List <String> list=datas.get(column);
int index=0;
for (int i = 0, prev = -1; i < list.size(); i++) {
if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
//System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);
for(int pos=0;pos<i - prev;pos++){
int posi=index+pos;
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}
int startrowpos=index+8;
int endrowpos=index+8+(i - prev)-1;
if(startrowpos==endrowpos){
LOG.info("don't merge");
}else{
CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
column, column);
sheet.addMergedRegion(cellRangeAddress);
}
index=index+(i - prev);
prev = i;
}
}
}
Finally, I resolved this problem. I am posting here the changes,because it may help other.
for (int column = 0; column < 13; column++) {
List <String> list=datas.get(column);
int index=0;
for (int i = 0, prev = -1; i < list.size(); i++) {
if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
//System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);
int posi=0;
for(int pos=0;pos<i - prev;pos++){
if(pos==0){
posi=index+pos;
}
}
int startrowpos=index+8;
int endrowpos=index+8+(i - prev)-1;
if(startrowpos==endrowpos){
LOG.info("don't merge");
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}else{
CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
column, column);
sheet.addMergedRegion(cellRangeAddress);
Cell cell= row[posi].createCell(column);
cell.setCellStyle((CellStyle) cs);
cell.setCellValue(list.get(i));
}
index=index+(i - prev);
prev = i;
}
}
}
I am trying to create the Game of Life with different classes, and I have gotten into trouble.
I want to check if there's an alive individual in the box with index row, col.
Here's my code so far:
public class LifeBoard {
private int rows;
private int cols;
private int generation;
/**Creates a playing field with rows as rows and cols as columns.
The counter for generations is 1.*/
public LifeBoard(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.generation = 1;
}
/**Checks if there's a living individual in the box row, col*/
public boolean get(int row, int col) {
if(this.board[row][col] == null){
return false;
} else {
return true;
}
}
}
I don't know what to do. How do I check this?
It depends on how you've modeled the game. This code assumes a boolean[][] grid.
There are up to 8 cells that surround a cell. Since we have to check for the boundaries of the grid, there might not be 8 cells surrounding every cell.
public synchronized void cycleGrid() {
this.generationCount++;
for (int i = 0; i < GRID_WIDTH; i++) {
for (int j = 0; j < GRID_WIDTH; j++) {
int count = countCells(i, j);
if (count == 3) grid[i][j] = true;
if (grid[i][j] && count < 2) grid[i][j] = false;
if (grid[i][j] && count > 3) grid[i][j] = false;
}
}
}
private int countCells(int i, int j) {
int count = 0;
int iminus = i - 1;
int jminus = j - 1;
int iplus = i + 1;
int jplus = j + 1;
if (iminus >= 0) {
if (jminus >= 0) {
if (grid[iminus][jminus]) count++;
}
if (grid[iminus][j]) count++;
if (jplus < GRID_WIDTH) {
if (grid[iminus][jplus]) count++;
}
}
if (jminus >= 0) {
if (grid[i][jminus]) count++;
}
if (jplus < GRID_WIDTH) {
if (grid[i][jplus]) count++;
}
if (iplus < GRID_WIDTH) {
if (jminus >= 0) {
if (grid[iplus][jminus]) count++;
}
if (grid[iplus][j]) count++;
if (jplus < GRID_WIDTH) {
if (grid[iplus][jplus]) count++;
}
}
return count;
}
Take a look at my article, John Conway’s Game of Life in Java Swing, for more hints.
Two things from what I have understood.
You haven't set things to NULL and you are making a check for it which wouldn't work.
You can set a check for !=NULL to return true and see if that helps.
I try to get a cell's coordinates, right now I write next code:
for (Row row : sheet) {
for(Cell cell : row){
// how to get a cell coordinates?
}
}
How can I get a cell's coordinates (dx1, dx2, dy1, dy2) ?
the shortest way to get cell address is:
cell.getAddress().formatAsString()
to get sheet name do the following
cell.getSheet().getSheetName()
The obvious solution was suggested by user2310289, but that won't work as the iterators skip over blank rows and cells.
So, one option is to iterate manually, taking care of blank cells, and then you'll always know where you cell is, with code something like:
// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
CellReference cr = new CellReference(c);
System.out.println("Cell at " + rowNum + "," + cn + " found, that's " + cr.formatAsString());
}
}
}
Or, if you do want to use the easy iterators, you need to look up the row and cell index from a cell, eg
for (Row r : sheet) {
for (Cell c : r) {
int columnNumber = c.getColumnIndex();
int rowNumber = c.getRow().getRowNum();
CellReference cr = new CellReference(c);
System.out.println("Cell at " + rowNum + "," + columnNumber + " found, that's " + cr.formatAsString());
}
}
I have done following code for getting coordinates of the excel cell. Hope this will help you.
public static void searchSheet(String searchText, XSSFSheet sheet)
{
int flag=0;
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++)
{
XSSFRow row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++)
{
XSSFCell cell = row.getCell(j);
String CellData=cell.toString();
if(searchText.equals(CellData))
{
flag=1;
CellData=row.getCell(j+1).toString();
System.out.println(CellData);
System.out.println("String Found At Row="+ i +" and At Column="+j);
}
}
break;
}
if(flag==0)
{
System.out.println("String Not Found");
}
}
I am new to Apache-poi and using 3.8 version of poi. While writing value in the Excel,i check for the column names if they matched, i will write some value in that column and finish it, and again i will start writing on header. The problem is if write three column values only last column values are add or saved and first two values are not saved in the column. Can anyone tell what went wrong.
(sorry, in case any mistake)
Code:
int i = 0;
Row row = sheet.createRow(i);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
String validate_header = null;
while (iterator.hasNext()) {
if (eiterator.hasNext()) {
validate_header = eiterator.next();
}
Cell cell = row.createCell(i);
String col_heading = iterator.next();
cell.setCellValue(col_heading);
if(col_heading.equalsIgnoreCase("Assigned Date"))
{
Add_value(i, col_heading, row, sheet);
row=sheet.getRow(0);
cell=row.getCell(i);
}
else if(col_heading.startsWith("Review"))
{
int count=-1;
int n=Col_values.get("Defect Summary").size();
for (int j = 0; j < n; j++) {
row = sheet.createRow(count);
cell = row.createCell(i);
String s="External QC Defect ";
cell.setCellValue(s);
count++;
}
row=sheet.getRow(0);
cell=row.getCell(i);
}
sheet.autoSizeColumn(i);
i++;
}
private static Sheet Add_value(int k,String name,Row row, Sheet sheet) {
System.out.println("Inside the add method");
if(name.equalsIgnoreCase("Assigned Date")||name.equalsIgnoreCase("Reported Date") )
{
vector = Col_values.get("TargetDate");
int count = 1;
System.out.println("IF Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
else
{
vector = Col_values.get("Defect Summary");
int count = 1;
System.out.println("ELSE Size of the vector " + vector.size());
for (int j = 0; j < vector.size(); j++) {
row = sheet.createRow(count);
cell = row.createCell(k);
String s = (String) vector.get(j);
System.out.println(s);
cell.setCellValue(s);
count++;
}
}
return sheet;
}
'
Can u tell what went Wrong?
It seems that Add_value starts creating rows from top. Therefore on the second call the old rows are removed.
Replace
row = sheet.createRow(count);
with
row = k == 0 ? sheet.createRow(count) : sheet.getRow(count);