Apache POI 插件读写Excel

//图片 jar 包如下,下载地址我就不多说了,百度一下有很多
//这里有 poi 对 excel 的读写操作希望能帮到大家!

Apache POI 插件读写Excel

 
package com.poi.excel;

import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 读取 excel
 * @author LGF
 *
 */
public class ReadExcel {
    
    public static void main(String[] args) {
        ReadExcel read = new ReadExcel();
        try {
            read.read("ORACLE.xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 读取 excel 文件
     * @param name 文件名称
     * @throws IOException
     */
    public void read(String name) throws IOException{
        //获取文件流
        InputStream input = getInputStream(name);
        //构造 XSSFWorkbook 对象传入 文件流
        XSSFWorkbook wb = new XSSFWorkbook(input);
        //获取 excel中第一个表
        XSSFSheet hs = wb.getSheetAt(0);
        //获取第一个表的总行数
        int count = hs.getLastRowNum();
        //获取excel中有几个表
        int sheetCount = wb.getNumberOfSheets();
        //简单打印一下
        System.out.println("sheet1 count:" + count);
        System.out.println("sheetCount:" + sheetCount);
        //循环遍历第一个表中的数据
        for (int i = 0; i <= count; i++) {
            //获取第 i 行对象
            XSSFRow row = hs.getRow(i);
            //获取第 i 行对象中的总列数
            int column = row.getLastCellNum();
            //循环遍历取值
            for (int j = 0; j <column; j++) {
                XSSFCell cell = row.getCell(j);
                String value = cell.getStringCellValue();
                if(value.trim().isEmpty())continue;
                System.out.print(value + "|");
            }
            System.out.println("\n------------------------------------------");
        }
        //最后释放资源
        wb.close();
        input.close();
    }
    
    /**
     * 获取 class path 中的文件流
     * @param name 名称
     * @return InputStream
     */
    public InputStream getInputStream(String name){
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
    }
}

package com.poi.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * @author LGF 2015-01-03
 * 创建excel文件
 */
public class WriterExcel {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		//需要一个文件输入流
		OutputStream out = new FileOutputStream(new File("src/test.xlsx"));
		//创建 XSSFWorkbook 对象
		XSSFWorkbook wb = new XSSFWorkbook();
		//创建 excel中的表
		XSSFSheet sheet = wb.createSheet("test");
		//循环添加行,列
		for (int i = 0; i < 3; i++) {
			XSSFRow row = sheet.createRow(i);
			for (int j = 0; j < 5; j++) {
				XSSFCell cell = row.createCell(j);
				String value ="date:2012-03-0"+j;
				cell.setCellValue(value);
				sheet.autoSizeColumn(j);
			}
		}
		//释放资源
		wb.write(out);
		out.close();
		System.out.println("success");
	}

}
 

 

你可能感兴趣的:(apache,poi,插件读写Excel)