Search code examples
javaexcelxssf

applying styles to a complete row using xssfworkbook in java


Here,I am applying styles to excel,if I apply styles to individual cell it is working but takes lot of time to copy content and apply styles.Here we have option for applying styles to entire row at once using "row.setRowStyle(Style);" but styles are not applicable to excel.
Here is the sample code that I am using

public class WriteDataToExcel {
        public static void main(String[] args) throws IOException {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet("writetoexcel");
               XSSFFont font = wb.createFont();
                font.setBoldweight((short) 700);
                XSSFCellStyle Style = wb.createCellStyle();
                Style.setFillForegroundColor(new XSSFColor(java.awt.Color.LIGHT_GRAY));
                Style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
                Style.setFont(font);
                Style.setWrapText(true);
                Style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            for (int r=0;r < 1000; r++ )
            {
                XSSFRow row = sheet.createRow(r);
                //iterating c number of columns
                for (int c=0;c < 25; c++ )
                {
                    XSSFCell cell = row.createCell(c);
                    cell.setCellValue("Hello"); 
                    cell.setCellStyle(Style);
                }
                //row.setRowStyle(Style);
            }
            FileOutputStream fileOut = new FileOutputStream("E:" + File.separator + "WriteToExcel.xlsx");
            wb.write(fileOut);
            fileOut.flush();
            fileOut.close();
        }
}

Solution

  • I just used SXSSFWorkbook instead of XSSFWorkbook

    SXSSFWorkbook wb = new SXSSFWorkbook();

    SXSSFSheet sheet = (SXSSFSheet) wb.createSheet("writetoexcel");