POI封装Excel工具类(获取单元格值,对单元格赋值,写入图片)

这个博客介绍了如何使用Apache POI库创建一个Java工具类,用于读取和写入Excel文件中的单元格值。包括确认Excel文件类型、获取指定单元格、设置单元格值、读取单元格内容等功能,并在版本二中新增了写入图片到Excel的能力。提供了一些关键方法的示例代码。
摘要由CSDN通过智能技术生成

 版本二:加入了图片写出到表格,优化了版本一

package com.wenhao.three;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * POI工具类:
 * 获取指定位置单元格的值
 * 设置指定位置单元格的值
 */
public class MyPOIUtils {
    
    public static Workbook workbook ;
    
    public static Sheet sheet;
    
    public static FileInputStream fileInputStream ;
    
    public static FileOutputStream out = null;
    
    /**
     * 确认读取文件的版本类型
     * @param filePath
     */
    public static void insureExcelType(String filePath) {
        try {
            fileInputStream = new FileInputStream(filePath);
            if (filePath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fileInputStream);
               // sheet = workbook.getSheetAt(0);
            } else if (filePath.endsWith(".xls")) {
                POIFSFileSystem fileSystem = new POIFSFileSystem(fileInputStream);
                workbook = new HSSFWorkbook(fileSystem);
               // sheet = workbook.getSheetAt(0);
            } else {
                throw new RuntimeException("错误提示: 您设置的Excel文件名不合法!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void insureExcelType(String filePath,String fileName) {
        try {
            fileInputStream = new FileInputStream(filePath + fileName);
            if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fileInputStream);
            } else if (fileName.endsWith(".xls")) {
                POIFSFileSystem fileSystem = new POIFSFileSystem(fileInputStream);
                workbook = new HSSFWorkbook(fileSystem);
            } else {
                throw new RuntimeException("错误提示: 您设置的Excel文件名不合法!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 获取第一个Sheet中的单元格
     * @param rowIndex
     * @param colIndex
     * @return
     */
    public static Cell getCellInSheet(int rowIndex, int colIndex) {
        sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(rowIndex - 1);
        Cell cell = row.getCell(colIndex - 1);
        return cell ;
    }
    
    /**
     * 获取指定Sheet中的单元格
     * @param st
     * @param rowIndex
     * @param colIndex
     * @return
     */
    public static Cell getCellInSheet(int st,int rowIndex, int colIndex) {
        sheet = workbook.getSheetAt(st);
        Row rowst = sheet.getRow(rowIndex - 1);
        if(rowst == null) {
            rowst.setRowNum(Cell.CELL_TYPE_STRING);
            Cell cellst = rowst.getCell(colIndex - 1);
            return cellst ;
        }
        Cell cellst = rowst.getCell(colIndex - 1);
        return cellst ;
    }
    
    /**
     * 根据行和列获取单元格内容
     * @param filePath文件路径
     * @param rowIndex行号
     * @param colIndex列号
     * @return
     * @throws Exception
     */
    public static String getCellValueAt(String filePath,int rowIndex, int colIndex) throws Exception {
        
        insureExcelType(filePath);
        
        Cell cell = getCellInSheet(rowIndex,colIndex);
        String cellValue = getCellValue(cell);
        return cellValue ;
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值