Java 在Excel文件的特定位置创建单元格

Java 在Excel文件的特定位置创建单元格

Apache POI 是一个开源的java库,用于创建和操作基于Microsoft Office的各种文件格式。使用POI,人们应该能够对以下文件格式进行创建、修改和显示/读取操作/它可以用来在Given Excel文件的特定位置创建一个单元格。Apache POI是由Apache基金会提供的一个API。

在给定的Excel文件中的特定位置创建单元格的步骤如下

  1. 在eclipse中创建一个maven项目( Maven 是一种构建自动化工具,主要用于Java项目)或一个安装了 POI库 的Java项目。
  2. 在pom.xml文件中添加以下maven依赖项
  3. 在javaresource文件夹中编写java代码

例子

// Java Program to Demonstrate Creation Of Cell
// At Specific Position in Excel File
 
// Importing required classes
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
 
// Class
// CreateCellAtSpecificPosition
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws FileNotFoundException, IOException
    {
 
        // Creating a workbook instances
        Workbook wb = new HSSFWorkbook();
 
        // Creating output file
        OutputStream os
            = new FileOutputStream("Geeks.xlsx");
 
        // Creating a sheet using predefined class
        // provided by Apache POI
        Sheet sheet = wb.createSheet("Company Preparation");
 
        // Creating a row at specific position
        // using predefined class provided by Apache POI
 
        // Specific row number
        Row row = sheet.createRow(1);
 
        // Specific cell number
        Cell cell = row.createCell(1);
 
        // putting value at specific position
        cell.setCellValue("Geeks");
 
        // Finding index value of row and column of given
        // cell
        int rowIndex = cell.getRowIndex();
        int columnIndex = cell.getColumnIndex();
 
        // Writing the content to Workbook
        wb.write(os);
 
        // Printing the row and column index of cell created
        System.out.println("Given cell is created at "
                           + "(" + rowIndex + ","
                           + columnIndex + ")");
    }
}
Java

输出: 在控制台

Given cell is created at (1,1)
Java

输出: 名为’Geeks.xlsx’的内部文件

用Java在Excel文件的特定位置创建单元格

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册