Domanda

I am having the code for shifting the rows from '0'th row to the '4'th row.

But my problem is,I need to insert a header in the '0'th row. How can I able to do that ?

Here is my code

  HSSFWorkbook wb = (HSSFWorkbook) document;
            HSSFSheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "report_lists"); // set sheet name
             sheet.shiftRows(0, sheet.getLastRowNum(), 4); //shifitng happens here.
//In the generated excel sheet I have four empty rows.But i need to enter some header in the top 4 rows NOT IN THE SHIFTED ROWS.


    //Now, I am getting the first row.
        HSSFRow firstrow= sheet.getRow(0);
        firstrow.getcell(0).setcellvalue("Actual"); 
        //I am assingning the value in for the cell 
        //but here is where I am getting NULL POINTER Exception while running the code



                HSSFRow header = sheet.getRow(4);
                HSSFCellStyle cellStyle = wb.createCellStyle();
                cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
                cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

                HSSFFont fontHeader = (HSSFFont) wb.createFont();
                fontHeader.setFontName("Calibri");
                cellStyle.setFont(fontHeader);
                for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
                    HSSFCell cell = header.getCell(i);
                    cell.setCellStyle(cellStyle);

                }
            }
È stato utile?

Soluzione

instead of

HSSFRow firstrow= sheet.getRow(0);
firstrow.getcell(0).setcellvalue("Actual");

you should use

HSSFRow firstrow= sheet.getRow(0); // or sheet.createRow(0) if you didn't have row at 0(null pointer here)
firstrow.createCell(0).setcellvalue("Actual");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top