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

阿帕奇波伊 可用于在给定Excel文件的特定位置创建单元格。Apache POI是Apache基金会提供的API。

null

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

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

    < dependency >
    < groupId >org.apache.poi</ groupId >
    < artifactId >poi</ artifactId >
    < version >3.12</ version >
    </ dependency >
    < dependency >
    < groupId >org.apache.poi</ groupId >
    < artifactId >poi-ooxml</ artifactId >
    < version >3.12</ version >
    </ dependency >

    
    

  3. 在javaresource文件夹中编写java代码

    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;
    public class CreateCellAtSpecificPosition {
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
    // Create a workbook instances
    Workbook wb = new HSSFWorkbook();
    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" );
    // writing the content to Workbook
    wb.write(os);
    System.out.println( "given cell is created at position (1, 1)" );
    }
    }

    
    

输出

given cell is created at position (1, 1)

产出以极客为单位。xlsx文件 图片[1]-使用Java在Excel文件中的特定位置创建单元格-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享