Fork me on GitHub

java导出和读取excel数据

使用的是poi的jar包

下载地址http://poi.apache.org/download.html

主要是把jar包导入,直接新建一个列子测试一下就明白了。使用起来还是比较方便的,代码里面的原理也很好懂。

下面的代码中一共给出三个方法,第一个主要是用在写成xlsx后缀的文件使用的,后面两个方法是直接照搬网上的,通用方法无论什么版本均可以使用。

可以根据自己的需要对里面的东西进行修改

package excelTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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;
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的demo
 * @author XX
 *
 */
public class Test {
    
    public static void main(String[] args) throws IOException {    
        String path = "E:/";    
        String fileName = "test";    
        String fileType = "xlsx";    
        writeXLSX(path, fileName, fileType);    
        read(path, fileName, fileType);    
    }    
    
    /**
     * 写入xlsx文件的方法
     * @param path
     * @param fileName
     * @param fileType
     */
    private static void writeXLSX(String path, String fileName,String fileType)
    {
        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet sheet1 = workBook.createSheet("sheet1");
        
        //创建第一行表头
        XSSFRow row = sheet1.createRow(0);
        for (int j = 0; j < 8; j++) {    
            XSSFCell cell = row.createCell(j);    
            cell.setCellValue("功能"+j);    
        }
        
        for (int i = 1; i < 10; i++) {    
            XSSFRow row2 = sheet1.createRow(i);    
            //循环写入列数据     
            for (int j = 0; j < 8; j++) {    
                XSSFCell cell = row2.createCell(j);    
                cell.setCellValue("数据"+ 123456789 + j);    
            }    
        }    
        OutputStream stream = null;
        try {
            stream = new FileOutputStream(path+fileName+"."+fileType);
            workBook.write(stream);    
            workBook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 可以使用两种不同格式的创建excel的方法
     * @param path
     * @param fileName
     * @param fileType
     * @throws IOException
     */
    private static void writer(String path, String fileName,String fileType) throws IOException {    
        //创建工作文档对象     
        Workbook wb = null;    
        if (fileType.equals("xls")) {    
            wb = new HSSFWorkbook();    
        }    
        else if(fileType.equals("xlsx"))    
        {    
            wb = new XSSFWorkbook();    
        }    
        else    
        {    
            System.out.println("您的文档格式不正确!");    
        }    
        //创建sheet对象     
        Sheet sheet1 = (Sheet) wb.createSheet("sheet1");    
        //循环写入行数据     
        for (int i = 0; i < 65539; i++) {    
            Row row = (Row) sheet1.createRow(i);    
            //循环写入列数据     
            for (int j = 0; j < 8; j++) {    
                Cell cell = row.createCell(j);    
                cell.setCellValue("测试"+j);    
            }    
        }    
        //创建文件流     
        OutputStream stream = new FileOutputStream(path+fileName+"."+fileType);    
        //写入数据     
        wb.write(stream);    
        //关闭文件流     
        stream.close();    
    }    
    
    /**
     * 使用两种不同格式的读取excel的方法
     * @param path
     * @param fileName
     * @param fileType
     * @throws IOException
     */
    public static void read(String path,String fileName,String fileType) throws IOException    
    {    
        InputStream stream = new FileInputStream(path+fileName+"."+fileType);    
        Workbook wb = null;    
        if (fileType.equals("xls")) {    
            wb = new HSSFWorkbook(stream);    
        }    
        else if (fileType.equals("xlsx")) {    
            wb = new XSSFWorkbook(stream);    
        }    
        else {    
            System.out.println("您输入的excel格式不正确");    
        }    
        Sheet sheet1 = wb.getSheetAt(0);    
        for (Row row : sheet1) {    
            for (Cell cell : row) {    
                System.out.print(cell.getStringCellValue()+"  ");    
            }    
            System.out.println();    
        }    
    } 
}


后面两个方法转载自:http://blog.csdn.net/ptzrbin/article/details/8751229
posted @ 2016-07-12 14:11  LinkinStar  阅读(323)  评论(0编辑  收藏  举报