Any data that you enter into a spreadsheet is always stored in a cell. We use the labels of rows and columns to identify a cell. This chapter describes how to manipulate data in cells in a spreadsheet using Java programming.

Create a Cell

You need to create a row before creating a cell. A row is nothing but a collection of cells.

The following code snippet is used for creating a cell.

  1. //create new workbook
  2. XSSFWorkbook workbook = new XSSFWorkbook();
  3.  
  4. //create spreadsheet with a name
  5. XSSFSheet spreadsheet = workbook.createSheet("new sheet");
  6.  
  7. //create first row on a created spreadsheet
  8. XSSFRow row = spreadsheet.createRow(0);
  9.  
  10. //create first cell on created row
  11. XSSFCell cell = row.createCell(0);

Types of Cells

The cell type specifies whether a cell can contain strings, numeric value, or formulas. A string cell cannot hold numeric values and a numeric cell cannot hold strings. Given below are the types of cells, their values, and type syntax.

Type of cell value Type Syntax
Blank cell value XSSFCell.CELL_TYPE_BLANK
Boolean cell value XSSFCell.CELL.TYPE_BOOLEAN
Error cell value XSSFCell.CELL_TYPE_ERROR
Numeric cell value XSSFCell.CELL_TYPE_NUMERIC
String cell value XSSFCell.CELL_TYPE_STRING
Type of cell value Type Syntax
Blank cell value XSSFCell.CELL_TYPE_BLANK
Boolean cell value XSSFCell.CELL.TYPE_BOOLEAN
Error cell value XSSFCell.CELL_TYPE_ERROR
Numeric cell value XSSFCell.CELL_TYPE_NUMERIC
String cell value XSSFCell.CELL_TYPE_STRING

The following code is used to create different types of cells in a spreadsheet.

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3.  
  4. import java.util.Date;
  5.  
  6. import org.apache.poi.xssf.usermodel.XSSFCell;
  7. import org.apache.poi.xssf.usermodel.XSSFRow;
  8. import org.apache.poi.xssf.usermodel.XSSFSheet;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10.  
  11. public class TypesofCells {
  12. public static void main(String[] args)throws Exception {
  13. XSSFWorkbook workbook = new XSSFWorkbook();
  14. XSSFSheet spreadsheet = workbook.createSheet("cell types");
  15.  
  16. XSSFRow row = spreadsheet.createRow((short) 2);
  17. row.createCell(0).setCellValue("Type of Cell");
  18. row.createCell(1).setCellValue("cell value");
  19.  
  20. row = spreadsheet.createRow((short) 3);
  21. row.createCell(0).setCellValue("set cell type BLANK");
  22. row.createCell(1);
  23.  
  24. row = spreadsheet.createRow((short) 4);
  25. row.createCell(0).setCellValue("set cell type BOOLEAN");
  26. row.createCell(1).setCellValue(true);
  27.  
  28. row = spreadsheet.createRow((short) 5);
  29. row.createCell(0).setCellValue("set cell type ERROR");
  30. row.createCell(1).setCellValue(XSSFCell.CELL_TYPE_ERROR );
  31.  
  32. row = spreadsheet.createRow((short) 6);
  33. row.createCell(0).setCellValue("set cell type date");
  34. row.createCell(1).setCellValue(new Date());
  35.  
  36. row = spreadsheet.createRow((short) 7);
  37. row.createCell(0).setCellValue("set cell type numeric");
  38. row.createCell(1).setCellValue(20 );
  39.  
  40. row = spreadsheet.createRow((short) 8);
  41. row.createCell(0).setCellValue("set cell type string");
  42. row.createCell(1).setCellValue("A String");
  43.  
  44. FileOutputStream out = new FileOutputStream(new File("typesofcells.xlsx"));
  45. workbook.write(out);
  46. out.close();
  47. System.out.println("typesofcells.xlsx written successfully");
  48. }
  49. }

Save the above code in a file named TypesofCells.java, compile and execute it from the command prompt as follows.

  1. $javac TypesofCells.java
  2. $java TypesofCells

If your system is configured with the POI library, then it will compile and execute to generate an Excel file named typesofcells.xlsx in your current directory and display the following output.

  1. typesofcells.xlsx written successfully

The typesofcells.xlsx file looks as follows.

typesofcells.jpeg

Cell Styles

Here you can learn how to do cell formatting and apply different styles such as merging adjacent cells, adding borders, setting cell alignment and filling with colors.

The following code is used to apply different styles to cells using Java programming.

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3.  
  4. import org.apache.poi.hssf.util.HSSFColor;
  5. import org.apache.poi.ss.usermodel.IndexedColors;
  6. import org.apache.poi.ss.util.CellRangeAddress;
  7. import org.apache.poi.xssf.usermodel.XSSFCell;
  8. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  9. import org.apache.poi.xssf.usermodel.XSSFRow;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12.  
  13. public class CellStyle {
  14. public static void main(String[] args)throws Exception {
  15. XSSFWorkbook workbook = new XSSFWorkbook();
  16. XSSFSheet spreadsheet = workbook.createSheet("cellstyle");
  17. XSSFRow row = spreadsheet.createRow((short) 1);
  18. row.setHeight((short) 800);
  19. XSSFCell cell = (XSSFCell) row.createCell((short) 1);
  20. cell.setCellValue("test of merging");
  21.  
  22. //MEARGING CELLS
  23. //this statement for merging cells
  24.  
  25. spreadsheet.addMergedRegion(
  26. new CellRangeAddress(
  27. 1, //first row (0-based)
  28. 1, //last row (0-based)
  29. 1, //first column (0-based)
  30. 4 //last column (0-based)
  31. )
  32. );
  33.  
  34. //CELL Alignment
  35. row = spreadsheet.createRow(5);
  36. cell = (XSSFCell) row.createCell(0);
  37. row.setHeight((short) 800);
  38.  
  39. // Top Left alignment
  40. XSSFCellStyle style1 = workbook.createCellStyle();
  41. spreadsheet.setColumnWidth(0, 8000);
  42. style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);
  43. style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
  44. cell.setCellValue("Top Left");
  45. cell.setCellStyle(style1);
  46. row = spreadsheet.createRow(6);
  47. cell = (XSSFCell) row.createCell(1);
  48. row.setHeight((short) 800);
  49.  
  50. // Center Align Cell Contents
  51. XSSFCellStyle style2 = workbook.createCellStyle();
  52. style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  53. style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  54. cell.setCellValue("Center Aligned");
  55. cell.setCellStyle(style2);
  56. row = spreadsheet.createRow(7);
  57. cell = (XSSFCell) row.createCell(2);
  58. row.setHeight((short) 800);
  59.  
  60. // Bottom Right alignment
  61. XSSFCellStyle style3 = workbook.createCellStyle();
  62. style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
  63. style3.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
  64. cell.setCellValue("Bottom Right");
  65. cell.setCellStyle(style3);
  66. row = spreadsheet.createRow(8);
  67. cell = (XSSFCell) row.createCell(3);
  68.  
  69. // Justified Alignment
  70. XSSFCellStyle style4 = workbook.createCellStyle();
  71. style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);
  72. style4.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
  73. cell.setCellValue("Contents are Justified in Alignment");
  74. cell.setCellStyle(style4);
  75.  
  76. //CELL BORDER
  77. row = spreadsheet.createRow((short) 10);
  78. row.setHeight((short) 800);
  79. cell = (XSSFCell) row.createCell((short) 1);
  80. cell.setCellValue("BORDER");
  81.  
  82. XSSFCellStyle style5 = workbook.createCellStyle();
  83. style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);
  84. style5.setBottomBorderColor(IndexedColors.BLUE.getIndex());
  85. style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);
  86. style5.setLeftBorderColor(IndexedColors.GREEN.getIndex());
  87. style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);
  88. style5.setRightBorderColor(IndexedColors.RED.getIndex());
  89. style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);
  90. style5.setTopBorderColor(IndexedColors.CORAL.getIndex());
  91. cell.setCellStyle(style5);
  92.  
  93. //Fill Colors
  94. //background color
  95. row = spreadsheet.createRow((short) 10 );
  96. cell = (XSSFCell) row.createCell((short) 1);
  97.  
  98. XSSFCellStyle style6 = workbook.createCellStyle();
  99. style6.setFillBackgroundColor(HSSFColor.LEMON_CHIFFON.index );
  100. style6.setFillPattern(XSSFCellStyle.LESS_DOTS);
  101. style6.setAlignment(XSSFCellStyle.ALIGN_FILL);
  102. spreadsheet.setColumnWidth(1,8000);
  103. cell.setCellValue("FILL BACKGROUNG/FILL PATTERN");
  104. cell.setCellStyle(style6);
  105.  
  106. //Foreground color
  107. row = spreadsheet.createRow((short) 12);
  108. cell = (XSSFCell) row.createCell((short) 1);
  109.  
  110. XSSFCellStyle style7 = workbook.createCellStyle();
  111. style7.setFillForegroundColor(HSSFColor.BLUE.index);
  112. style7.setFillPattern( XSSFCellStyle.LESS_DOTS);
  113. style7.setAlignment(XSSFCellStyle.ALIGN_FILL);
  114. cell.setCellValue("FILL FOREGROUND/FILL PATTERN");
  115. cell.setCellStyle(style7);
  116.  
  117. FileOutputStream out = new FileOutputStream(new File("cellstyle.xlsx"));
  118. workbook.write(out);
  119. out.close();
  120. System.out.println("cellstyle.xlsx written successfully");
  121. }
  122. }

Save the above code in a file named CellStyle.java, compile and execute it from the command prompt as follows.

  1. $javac CellStyle.java
  2. $java CellStyle

It will generate an Excel file named cellstyle.xlsx in your current directory and display the following output.

  1. cellstyle.xlsx written successfully

The cellstyle.xlsx file looks as follows.

cellstyle.jpeg