您输入电子表格中的任何数据始终存储在单元格中。我们使用行和列的标签来标识单元格。本章介绍如何使用Java编程来处理电子表格中单元格中的数据。

创建一个单元

您需要在创建单元格之前创建一行。行不过是单元格的集合。

以下代码段用于创建单元格。

  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);

单元格类型

单元格类型指定一个单元格可以包含字符串,数字值还是公式。字符串单元格不能容纳数值,数字单元格不能容纳字符串。下面给出的是单元格的类型,它们的值和类型语法。

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

以下代码用于在电子表格中创建不同类型的单元格。

  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. }

将上面的代码保存在一个名为TypesofCells.java的文件中,在命令提示符下进行编译并执行,如下所示。

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

如果您的系统配置了POI库,则它将编译并执行以在当前目录中生成一个名为typesofcells.xlsx的Excel文件,并显示以下输出。

  1. typesofcells.xlsx written successfully

typesofcells.xlsx文件如下所示。

typesofcells.jpeg

单元格样式

在这里,您可以学习如何进行单元格格式化以及如何应用不同的样式,例如合并相邻的单元格,添加边框,设置单元格对齐方式以及用颜色填充。

以下代码用于使用Java编程将不同的样式应用于单元格。

  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. }

将上面的代码保存在一个名为CellStyle.java的文件中,按如下所示在命令提示符下进行编译和执行。

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

它将在当前目录中生成一个名为cellstyle.xlsx的Excel文件,并显示以下输出。

  1. cellstyle.xlsx written successfully

cellstyle.xlsx文件如下所示。

cellstyle.jpeg