本章将引导您完成使用Java编程在单元格上应用不同公式的过程。Excel应用程序的基本目的是通过在其上应用公式来维护数值数据。

    在公式中,我们在Excel工作表中传递动态值或值的位置。执行此公式后,您将获得所需的结果。下表列出了Excel中经常使用的一些基本公式。

    Operation Syntax
    Adding multiple numbers = SUM(Loc1:Locn) or = SUM(n1,n2,)
    Count = COUNT(Loc1:Locn) or = COUNT(n1,n2,)
    Power of two numbers = POWER(Loc1,Loc2) or = POWER(number, power)
    Max of multiple numbers = MAX(Loc1:Locn) or = MAX(n1,n2,)
    Product = PRODUCT(Loc1:Locn) or = PRODUCT(n1,n2,)
    Factorial = FACT(Locn) or = FACT(number)
    Absolute number = ABS(Locn) or = ABS(number)
    Today date =TODAY()
    Converts lowercase = LOWER(Locn) or = LOWER(text)
    Square root = SQRT(locn) or = SQRT(number)

    以下代码用于将公式添加到单元格并执行。

    1. import java.io.File;
    2. import java.io.FileOutputStream;
    3.  
    4. import org.apache.poi.xssf.usermodel.XSSFCell;
    5. import org.apache.poi.xssf.usermodel.XSSFRow;
    6. import org.apache.poi.xssf.usermodel.XSSFSheet;
    7. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    8.  
    9. public class Formula {
    10. public static void main(String[] args)throws Exception {
    11. XSSFWorkbook workbook = new XSSFWorkbook();
    12. XSSFSheet spreadsheet = workbook.createSheet("formula");
    13. XSSFRow row = spreadsheet.createRow(1);
    14. XSSFCell cell = row.createCell(1);
    15.  
    16. cell.setCellValue("A = ");
    17. cell = row.createCell(2);
    18. cell.setCellValue(2);
    19. row = spreadsheet.createRow(2);
    20. cell = row.createCell(1);
    21. cell.setCellValue("B = ");
    22. cell = row.createCell(2);
    23. cell.setCellValue(4);
    24. row = spreadsheet.createRow(3);
    25. cell = row.createCell(1);
    26. cell.setCellValue("Total = ");
    27. cell = row.createCell(2);
    28.  
    29. // Create SUM formula
    30. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    31. cell.setCellFormula("SUM(C2:C3)");
    32. cell = row.createCell(3);
    33. cell.setCellValue("SUM(C2:C3)");
    34. row = spreadsheet.createRow(4);
    35. cell = row.createCell(1);
    36. cell.setCellValue("POWER =");
    37. cell=row.createCell(2);
    38.  
    39. // Create POWER formula
    40. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    41. cell.setCellFormula("POWER(C2,C3)");
    42. cell = row.createCell(3);
    43. cell.setCellValue("POWER(C2,C3)");
    44. row = spreadsheet.createRow(5);
    45. cell = row.createCell(1);
    46. cell.setCellValue("MAX = ");
    47. cell = row.createCell(2);
    48.  
    49. // Create MAX formula
    50. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    51. cell.setCellFormula("MAX(C2,C3)");
    52. cell = row.createCell(3);
    53. cell.setCellValue("MAX(C2,C3)");
    54. row = spreadsheet.createRow(6);
    55. cell = row.createCell(1);
    56. cell.setCellValue("FACT = ");
    57. cell = row.createCell(2);
    58.  
    59. // Create FACT formula
    60. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    61. cell.setCellFormula("FACT(C3)");
    62. cell = row.createCell(3);
    63. cell.setCellValue("FACT(C3)");
    64. row = spreadsheet.createRow(7);
    65. cell = row.createCell(1);
    66. cell.setCellValue("SQRT = ");
    67. cell = row.createCell(2);
    68.  
    69. // Create SQRT formula
    70. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
    71. cell.setCellFormula("SQRT(C5)");
    72. cell = row.createCell(3);
    73. cell.setCellValue("SQRT(C5)");
    74. workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
    75. FileOutputStream out = new FileOutputStream(new File("formula.xlsx"));
    76. workbook.write(out);
    77. out.close();
    78. System.out.println("fromula.xlsx written successfully");
    79. }
    80. }

    将以上代码另存为Formula.java,然后在命令提示符下进行编译并执行,如下所示。

    1. $javac Formula.java
    2. $java Formula

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

    1. fromula.xlsx written successfully

    该formula.xlsx文件如下所示。

    formula.jpeg