本章说明如何创建电子表格并使用Java对其进行操作。电子表格是Excel文件中的页面;它包含具有特定名称的行和列。

完成本章后,您将能够创建电子表格并对其执行读取操作。

创建一个电子表格

首先,让我们使用前面各章中讨论的引用类创建电子表格。通过遵循上一章,首先创建一个工作簿,然后我们可以继续创建工作表。

以下代码段用于创建电子表格。

  1. //Create Blank workbook
  2. XSSFWorkbook workbook = new XSSFWorkbook();
  3.  
  4. //Create a blank spreadsheet
  5. XSSFSheet spreadsheet = workbook.createSheet("Sheet Name");

电子表格上的行

电子表格具有网格布局。行和列用特定名称标识。列用字母标识,行用数字标识。

以下代码段用于创建一行。

  1. XSSFRow row = spreadsheet.createRow((short)1);

写入电子表格

让我们考虑一个雇员数据的例子。在此,员工数据以表格形式给出。

Emp Id Emp Name Designation
Tp01 Gopal Technical Manager
TP02 Manisha Proof Reader
Tp03 Masthan Technical Writer
Tp04 Satish Technical Writer
Tp05 Krishna Technical Writer
Emp Id Emp Name Designation
Tp01 Gopal Technical Manager
TP02 Manisha Proof Reader
Tp03 Masthan Technical Writer
Tp04 Satish Technical Writer
Tp05 Krishna Technical Writer

以下代码用于将上述数据写入电子表格。

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3.  
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.TreeMap;
  7.  
  8. import org.apache.poi.ss.usermodel.Cell;
  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 Writesheet {
  14. public static void main(String[] args) throws Exception {
  15.  
  16. //Create blank workbook
  17. XSSFWorkbook workbook = new XSSFWorkbook();
  18.  
  19. //Create a blank sheet
  20. XSSFSheet spreadsheet = workbook.createSheet(" Employee Info ");
  21.  
  22. //Create row object
  23. XSSFRow row;
  24.  
  25. //This data needs to be written (Object[])
  26. Map < String, Object[] > empinfo =
  27. new TreeMap < String, Object[] >();
  28. empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
  29. empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
  30. empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
  31. empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
  32. empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
  33. empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" });
  34.  
  35. //Iterate over data and write to sheet
  36. Set < String > keyid = empinfo.keySet();
  37. int rowid = 0;
  38.  
  39. for (String key : keyid) {
  40. row = spreadsheet.createRow(rowid++);
  41. Object [] objectArr = empinfo.get(key);
  42. int cellid = 0;
  43.  
  44. for (Object obj : objectArr) {
  45. Cell cell = row.createCell(cellid++);
  46. cell.setCellValue((String)obj);
  47. }
  48. }
  49.  
  50. //Write the workbook in file system
  51. FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"));
  52. workbook.write(out);
  53. out.close();
  54. System.out.println("Writesheet.xlsx written successfully");
  55. }
  56. }

将上面的Java代码另存为Writesheet.java,然后从命令提示符下编译并运行它,如下所示:

  1. $javac Writesheet.java
  2. $java Writesheet

它将编译并执行以在当前目录中生成一个名为Writesheet.xlsx的Excel文件,您将在命令提示符下获得以下输出。

  1. Writesheet.xlsx written successfully

Writesheet.xlsx文件如下所示。

writesheet.jpeg

从电子表格中读取

让我们考虑上面名为Writesheet.xslx的 excel文件作为输入。遵守以下代码;它用于从电子表格中读取数据。

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3.  
  4. import java.util.Iterator;
  5.  
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.xssf.usermodel.XSSFRow;
  9. import org.apache.poi.xssf.usermodel.XSSFSheet;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11.  
  12. public class Readsheet {
  13. static XSSFRow row;
  14. public static void main(String[] args) throws Exception {
  15. FileInputStream fis = new FileInputStream(new File("WriteSheet.xlsx"));
  16.  
  17. XSSFWorkbook workbook = new XSSFWorkbook(fis);
  18. XSSFSheet spreadsheet = workbook.getSheetAt(0);
  19. Iterator < Row > rowIterator = spreadsheet.iterator();
  20.  
  21. while (rowIterator.hasNext()) {
  22. row = (XSSFRow) rowIterator.next();
  23. Iterator < Cell > cellIterator = row.cellIterator();
  24.  
  25. while ( cellIterator.hasNext()) {
  26. Cell cell = cellIterator.next();
  27.  
  28. switch (cell.getCellType()) {
  29. case Cell.CELL_TYPE_NUMERIC:
  30. System.out.print(cell.getNumericCellValue() + " tt ");
  31. break;
  32.  
  33. case Cell.CELL_TYPE_STRING:
  34. System.out.print(
  35. cell.getStringCellValue() + " tt ");
  36. break;
  37. }
  38. }
  39. System.out.println();
  40. }
  41. fis.close();
  42. }
  43. }

让我们将上面的代码保存在Readsheet.java文件中,然后从命令提示符下编译并运行它,如下所示:

  1. $javac Readsheet.java
  2. $java Readsheet

如果您的系统环境配置了POI库,它将在命令提示符下编译并执行以生成以下输出。

  1. EMP ID EMP NAME DESIGNATION
  2. tp01 Gopal Technical Manager
  3. tp02 Manisha Proof Reader
  4. tp03 Masthan Technical Writer
  5. tp04 Satish Technical Writer
  6. tp05 Krishna Technical Writer