java对POI的Excel操作

这是自己通过Java对POI的Excel所写的一个小Demo,有兴趣的可以看一看
package test;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
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.XSSFWorkbook;
import org.junit.Test;

/**
 * 
     * Title: TestJunit.java    
     * Description: java对POI的Excel操作
     * @author liushaocheng      
     * @created 2016年6月5日 下午9:01:20
 */
public class TestJunit {

    /**
     *
         * @description 创建Excel文件
         * @author  liushaocheng      
         * @throws Exception
         * @created 2016年5月30日 上午11:53:51
     */
    public void CreateWorkBook() throws Exception {

        /**
         * 这里说明一下,POI对Excel文件的操作提供了两个对象
         * 一个是下面所写的XSSFWorkbook对象,另一个是HSSFWorkbook对象
         * 它们的区别主要在于前者与MS-Office版本2007或更高版本兼容。后者与微软Office97-2003版本兼容。
         * 另外,如果创建的文件有错,不要担心,只需要在创建工作薄的时候创建工作表就不会有错了
         */
        Workbook wb = new XSSFWorkbook();
        //XSSFWorkBook wb = new XSSFWrokbook();

        FileOutputStream fileOut = new FileOutputStream("D:\\workbook.xlsx");

        wb.write(fileOut);

        fileOut.close();
    }

    /**
     * 
         * @description 创建工作表
         * @author  liushaocheng       
         * @created 2016年5月30日 下午4:56:15     
         * @throws Exception
     */
    public void createSheet() throws Exception {

        XSSFWorkbook wb = new XSSFWorkbook();

        Sheet sheetOne = wb.createSheet("sheet1");

        Sheet sheetTwo = wb.createSheet("sheet2");

        FileOutputStream fileOut = new FileOutputStream("D:\\workbook.xlsx");

        wb.write(fileOut);

        fileOut.close();
    }

    /**
     * 
         * @description 创建单元格并写入数据
         * @author  liushaocheng       
         * @throws Exception 
         * @created 2016年5月30日 下午5:38:39
     */
    public void CreateCells() throws Exception {

        XSSFWorkbook wb = new XSSFWorkbook();

        CreationHelper creationHelper = wb.getCreationHelper();

        Sheet sheet = wb.createSheet("new sheet");

        //创建一行,并将单元格放入。行为0。
        Row row = sheet.createRow(0);

        //创建一个单元格
        Cell cell = row.createCell(0);

        cell.setCellValue(0);

        row.createCell(1).setCellValue(1);

        row.createCell(2).setCellValue(creationHelper.createRichTextString("create cell"));

        row.createCell(3).setCellValue(true);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\workbook.xlsx");

        wb.write(fileOutputStream);

        fileOutputStream.close();
    }

    /**
     * 
         * @description 创建日期格式的单元格
         * @author  liushaocheng       
         * @created 2016年5月30日 下午8:03:54     
         * @throws Exception
     */
    public void createDateCells() throws Exception {

        XSSFWorkbook wb = new XSSFWorkbook();

        CreationHelper creationHelper = wb.getCreationHelper();

        Sheet sheet = wb.createSheet("sheet one");

        Row row = sheet.createRow(0);

        Cell cell = row.createCell(0);

        cell.setCellValue(new Date());

        CellStyle cellstyle = wb.createCellStyle();

        //设置单元格格式为日期格式
        cellstyle.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));

        cell = row.createCell(1);

        cell.setCellValue(new Date());

        cell.setCellStyle(cellstyle);

        cell = row.createCell(2);

        //也可以使用Calendar来获取日期
        cell.setCellValue(Calendar.getInstance());

        cell.setCellStyle(cellstyle);

        FileOutputStream file = new FileOutputStream("D:\\workbook.xlsx");

        wb.write(file);

        file.close();
    }

    /**
     * 
         * @description 创建不同格式的单元格
         * @author  liushaocheng       
         * @created 2016年5月30日 下午8:14:10     
         * @throws Exception
     */
    @Test
    public void createDifferent() throws Exception {

        XSSFWorkbook wb = new XSSFWorkbook();

        Sheet sheet = wb.createSheet("sheet two");

        Row row = sheet.createRow(0);

        row.createCell(0).setCellValue(1.1);

        row.createCell(1).setCellValue(new Date());

        row.createCell(2).setCellValue(Calendar.getInstance());

        row.createCell(3).setCellValue("string");

        row.createCell(4).setCellValue(true);

        row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);

        FileOutputStream file = new FileOutputStream("D:\\workbook.xlsx");

        wb.write(file);

        file.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值