📜  如何使用Java在电子表格中创建不同类型的单元格?

📅  最后修改于: 2022-05-13 01:55:43.713000             🧑  作者: Mango

如何使用Java在电子表格中创建不同类型的单元格?

Apace POI 是 Apache Foundation 提供的 API,用于为单元格设置各种类型的值。 Apache POI 的重载函数setCellValue( )用于设置单元格的值类型。 Maven 是一个强大的项目管理工具,它基于 POM(项目对象模型)。它用于项目构建依赖和文档。它像 ANT 一样简化了构建过程。但是它比ANT先进太多了。

算法:在电子表格中创建不同类型单元格的步骤

  • 步骤 1:创建工作簿的实例
  • 第 2 步:在上述工作簿中创建一个电子表格。
  • 步骤 3:使用 XSSFRow 创建行
  • 第 4 步:通过定义 FileOutputStream 类型的对象将内容写入工作簿
  • 第五步:关闭文件

程序:

步骤1:在eclipse中创建一个Maven项目,并添加Apache POI(用于设置单元格的值类型)依赖项或一个安装了Apache POI库的Java项目。

第2步:与创建Maven项目一样,需要先将以下Maven依赖项添加到pom.xml文件中,才能使用Apache POI库。对于这些库,在线搜索Maven Central Library,然后搜索Apache POI并获取依赖项,或者也可以将如下所示的依赖项复制到主Java程序中。

XML

    
        
        
            org.apache.poi
            poi
            3.12
        
    
        
        
            org.apache.poi
            poi-ooxml
            3.9
          
    


Java
// Java Program to create 
// different types of cells in a spreadsheet
  
// Importing Maven dependenncies
// Importing Apache POI dependency files
// Importing java input/output file and
// Date class from java.util package
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
// Class having different types of cells
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Step 1: Create an instance of the workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        CreationHelper creationHelper
            = workbook.getCreationHelper();
  
        // Step 2: Create a spreadsheet in the above
        // workbook
        // and name it as CellTypesSheet
        XSSFSheet spreadsheet
            = workbook.createSheet("CellTypesSheet");
  
        // Step 3: Create rows using XSSFRow
        // We need to create row before creating cells,
        // row is a collection of cells
        XSSFRow noOfRows = spreadsheet.createRow((short)2);
  
        // Creating cells
        // Custom inputs in cells
  
        // Cell 1
        noOfRows.createCell(0).setCellValue("Cell Types");
  
        // Cell 2
        noOfRows.createCell(1).setCellValue("Cell Value");
  
        // Above two cells(Cell Types and Cell Values) are
        // created at row 3 in excel and similarly for rest
        // of the cells
        noOfRows = spreadsheet.createRow((short)3);
  
        // Cell 3
        // Setting a Blank type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-BLANK");
        noOfRows.createCell(1);
        noOfRows = spreadsheet.createRow((short)4);
  
        // Cell 4
        // Setting a Boolean type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-BOOLEAN");
        noOfRows.createCell(1).setCellValue(true);
        noOfRows = spreadsheet.createRow((short)5);
  
        // Cell 5
        // Setting a Error type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-ERROR");
        noOfRows.createCell(1).setCellValue(
            XSSFCell.CELL_TYPE_ERROR);
        noOfRows = spreadsheet.createRow((short)6);
  
        // Cell 6
        // Setting a Date and Time type cell
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(
            creationHelper.createDataFormat().getFormat(
                "d/m/y h:mm"));
        noOfRows.createCell(0).setCellValue(
            "Cell Type-DATE-TIME");
        noOfRows.createCell(1).setCellValue(new Date());
        noOfRows.getCell(1).setCellStyle(cellStyle);
        noOfRows = spreadsheet.createRow((short)7);
  
        // Cell 7
        // Setting a Numeric type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-Numeric");
        noOfRows.createCell(1).setCellValue(35);
        noOfRows = spreadsheet.createRow((short)8);
  
        // Cell 8
        // Setting a String type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-String");
        noOfRows.createCell(1).setCellValue(
            "GeeksForGeeks");
  
        // Step 4: Writing the content to the workbook
        // by defining object of type  FileOutputStream
        FileOutputStream out = new FileOutputStream(
            new File("GfgTypesOfCells.xlsx"));
        workbook.write(out);
  
        // Step 5: Close the file
        out.close();
  
        // Display message to be printed for successful
        // execution of program
        System.out.println("GFG.xlsx Created Successfully");
    }
}


第 3 步:添加依赖项后创建一个新类。要创建一个新类,请转到src/main/ Java/package 右键单击并创建一个新类并为其命名,这里为了说明目的,文件名为 GFG。 .在这个类中,我们将首先创建工作簿的一个实例,并使用该实例创建一个电子表格,在该电子表格中,我们将定义单元格的值类型。下面是为 GFG 提供的代码片段 如上所述的类(取自此处)。

实现:代码片段

示例:在电子表格中创建不同类型的单元格

Java

// Java Program to create 
// different types of cells in a spreadsheet
  
// Importing Maven dependenncies
// Importing Apache POI dependency files
// Importing java input/output file and
// Date class from java.util package
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
// Class having different types of cells
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Step 1: Create an instance of the workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        CreationHelper creationHelper
            = workbook.getCreationHelper();
  
        // Step 2: Create a spreadsheet in the above
        // workbook
        // and name it as CellTypesSheet
        XSSFSheet spreadsheet
            = workbook.createSheet("CellTypesSheet");
  
        // Step 3: Create rows using XSSFRow
        // We need to create row before creating cells,
        // row is a collection of cells
        XSSFRow noOfRows = spreadsheet.createRow((short)2);
  
        // Creating cells
        // Custom inputs in cells
  
        // Cell 1
        noOfRows.createCell(0).setCellValue("Cell Types");
  
        // Cell 2
        noOfRows.createCell(1).setCellValue("Cell Value");
  
        // Above two cells(Cell Types and Cell Values) are
        // created at row 3 in excel and similarly for rest
        // of the cells
        noOfRows = spreadsheet.createRow((short)3);
  
        // Cell 3
        // Setting a Blank type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-BLANK");
        noOfRows.createCell(1);
        noOfRows = spreadsheet.createRow((short)4);
  
        // Cell 4
        // Setting a Boolean type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-BOOLEAN");
        noOfRows.createCell(1).setCellValue(true);
        noOfRows = spreadsheet.createRow((short)5);
  
        // Cell 5
        // Setting a Error type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-ERROR");
        noOfRows.createCell(1).setCellValue(
            XSSFCell.CELL_TYPE_ERROR);
        noOfRows = spreadsheet.createRow((short)6);
  
        // Cell 6
        // Setting a Date and Time type cell
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(
            creationHelper.createDataFormat().getFormat(
                "d/m/y h:mm"));
        noOfRows.createCell(0).setCellValue(
            "Cell Type-DATE-TIME");
        noOfRows.createCell(1).setCellValue(new Date());
        noOfRows.getCell(1).setCellStyle(cellStyle);
        noOfRows = spreadsheet.createRow((short)7);
  
        // Cell 7
        // Setting a Numeric type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-Numeric");
        noOfRows.createCell(1).setCellValue(35);
        noOfRows = spreadsheet.createRow((short)8);
  
        // Cell 8
        // Setting a String type cell
        noOfRows.createCell(0).setCellValue(
            "Cell Type-String");
        noOfRows.createCell(1).setCellValue(
            "GeeksForGeeks");
  
        // Step 4: Writing the content to the workbook
        // by defining object of type  FileOutputStream
        FileOutputStream out = new FileOutputStream(
            new File("GfgTypesOfCells.xlsx"));
        workbook.write(out);
  
        // Step 5: Close the file
        out.close();
  
        // Display message to be printed for successful
        // execution of program
        System.out.println("GFG.xlsx Created Successfully");
    }
}

输出:将在控制台中打印一条消息,最后在程序中指定“GFG.xlsx Created Successfully”作为负责以下电子表格中更改的程序的成功执行: