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

2021年5月12日21:42:16 发表评论 875 次浏览

Apache POI可用于在指定位置的给定Excel文件中创建单元格。 Apache POI是Apache基金会提供的API。

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

在Eclipse或安装了POI库的Java项目中创建一个Maven项目(Maven是主要用于Java项目的构建自动化工具)

在pom.xml文件中添加以下Maven依赖项

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <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>

在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 Prepration" ); //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)" ); } }
  1. filter_none

    编辑 关

    play_arrow

    链接 亮度_4 代码

    chevron_right

     
         

    filter_none

     
         

  2. filter_none

    编辑 关

    play_arrow

    链接 亮度_4 代码

    chevron_right

     
        

    filter_none

     
        

输出如下

given cell is created at position (1, 1)

Geeks.xlsx文件中的输出

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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: