POI的简单使用!!!

导入依赖:


        
            org.apache.poi
            poi
            3.14
        
        
        
            org.apache.poi
            poi-ooxml
            3.14
        

我们以Excel表格为例:

首先进行写操作:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;

/**
 * 

Project: ican_parent - WriteExcelPoi

*

Powered by scl On 2024-01-24 16:16:26

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class WriteExcelPoi { public static void main(String[] args) throws Exception { //创建workbook HSSFWorkbook workbook = new HSSFWorkbook(); //创建sheet HSSFSheet sheet = workbook.createSheet("主角团"); //创建row HSSFRow row = sheet.createRow(0); //创建cell HSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); row.createCell(1).setCellValue("技能"); for (int i = 1; i <= 5; i++) { HSSFRow row1 = sheet.createRow(i); //往填写cell内容,第一个单元格 row1.createCell(0).setCellValue("主角"+i); //往填写cell内容,第二个单元格 row1.createCell(1).setCellValue("万剑归宗"+i); } //将excel表格输出到某个地方 workbook.write(new FileOutputStream("C:\\Users\\admin\\Desktop\\主角团.xls")); } }

读取操作:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;

/**
 * 

Project: ican_parent - ReadExcelPoi

*

Powered by scl On 2024-01-24 16:28:40

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class ReadExcelPoi { public static void main(String[] args) throws Exception { //获取workbook HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("C:\\Users\\admin\\Desktop\\主角团.xls")); //获取sheet HSSFSheet sheet = workbook.getSheet("主角团"); for (int i = 1; i <= sheet.getLastRowNum(); i++) { //获取row HSSFRow row = sheet.getRow(i); //获取cell内容 String name = row.getCell(0).getStringCellValue(); String show = row.getCell(1).getStringCellValue(); System.out.println(name+"-------"+show); } } }

你可能感兴趣的:(apache)