POI简单实例

7 篇文章 0 订阅

POI简单实例

1. 创建空的Excel

package com.hengxin.platform.demo;

import java.io.FileOutputStream;

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

public class Test {

    public static void main(String[] args) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            FileOutputStream out = new FileOutputStream("D:/课程表.xls");
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. 单元格赋值

package com.hengxin.platform.demo;
import java.io.FileOutputStream;
import java.util.Random;

import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.Region;
public class TestExcel {


     public  static HSSFCellStyle getStyleHeader(HSSFWorkbook workbook) {
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 12);//设置字体大小
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体加粗
            font.setFontName("宋体");//设置字体名字
            HSSFCellStyle style = workbook.createCellStyle();//设置样式
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置颜色
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//前景颜色
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充方式,前色填充
            //边框填充
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            style.setTopBorderColor(HSSFColor.BLACK.index);//上边框颜色
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            style.setRightBorderColor(HSSFColor.BLACK.index);//右边框颜色
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            style.setBottomBorderColor(HSSFColor.BLACK.index); //下边框颜色
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            style.setLeftBorderColor(HSSFColor.BLACK.index); //左边框颜色
            style.setFont(font);//设置的字体
            style.setWrapText(true);//设置自动换行
            style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//设置水平对齐的样式为居中对齐
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置垂直对齐的样式为居中对齐
            return style;
        }

     public static HSSFCellStyle getStyleBody(HSSFWorkbook workbook) {
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 12);
            font.setFontName("宋体");
            HSSFCellStyle style = workbook.createCellStyle();
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBottomBorderColor(HSSFColor.BLACK.index);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setLeftBorderColor(HSSFColor.BLACK.index);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setRightBorderColor(HSSFColor.BLACK.index);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setTopBorderColor(HSSFColor.BLACK.index);
            style.setFont(font);
            style.setWrapText(true);
            style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            return style;
        }

    public static void main(String[] args) {
         try {
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.createSheet("课程表");
                HSSFCellStyle headerStyle = getStyleHeader(workbook);
                HSSFCellStyle bodyStyle = getStyleBody(workbook);
                HSSFRow row = sheet.createRow(0);
                HSSFCell cell00 = row.createCell(0);
                cell00.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell00.setCellValue("星期一");
                cell00.setCellStyle(headerStyle);

                HSSFCell cell01 = row.createCell(1);
                cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell01.setCellValue("星期二");
                cell01.setCellStyle(headerStyle);

                HSSFCell cell02 = row.createCell(2);
                cell02.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell02.setCellValue("星期三");
                cell02.setCellStyle(headerStyle);

                HSSFCell cell03 = row.createCell(3);
                cell03.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell03.setCellValue("星期四");
                cell03.setCellStyle(headerStyle);

                HSSFCell cell04 = row.createCell(4);
                cell04.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell04.setCellValue("星期五");
                cell04.setCellStyle(headerStyle);
                Random random = new Random();
                String[] course = {"语文","数学","英语","物理","化学","政治","历史","音乐","美术","体育"};
                for (int rowNo = 1; rowNo <= 7; rowNo++) {
                    HSSFRow rowHSSF = sheet.createRow(rowNo);
                    for (int cellNo = 0; cellNo <= 4; cellNo++) {
                        int i = random.nextInt(10);
                        HSSFCell cell = rowHSSF.createCell(cellNo);
                        cell.setCellValue(course[i]);
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellStyle(bodyStyle);
                    }
                }
                FileOutputStream out = new FileOutputStream("D:/课程表.xls");
                workbook.write(out);
                out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }

}

这里写图片描述

3. 单元格合并

public static void main(String[] args) {
         try {
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.createSheet("课程表");
                HSSFCellStyle headerStyle = getStyleHeader(workbook);

                HSSFRow row00 = sheet.createRow(0);
                HSSFCell row0_cell00 = row00.createCell(0);
                row0_cell00.setCellType(HSSFCell.CELL_TYPE_STRING);
                row0_cell00.setCellValue("课程表");
                row0_cell00.setCellStyle(headerStyle);

                HSSFCell row10_cell01 = row00.createCell(1);
                row10_cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
                row10_cell01.setCellStyle(headerStyle);

                Region region1 = new Region(0, (short) 0, 0, (short) 1);
                sheet.addMergedRegion(region1);

                FileOutputStream out=new FileOutputStream("D:/课程表.xls");
                workbook.write(out);
                out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }

这里写图片描述

4. 创建文档摘要信息

public static void main(String[] args) {
        try{
        FileInputStream is = new FileInputStream("D:/课程表.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        workbook.createInformationProperties();//创建文档信息
        DocumentSummaryInformation dsi= workbook.getDocumentSummaryInformation();//摘要信息
        dsi.setCategory("类别:Excel文件");//类别
        dsi.setManager("管理者:Liki");//管理者
        dsi.setCompany("公司:无");//公司
        SummaryInformation si = workbook.getSummaryInformation();//摘要信息
        si.setSubject("主题:课程表");//主题
        si.setTitle("标题:初中课程表");//标题
        si.setAuthor("作者:zyn");//作者
        si.setComments("备注:课程表展示");//备注
        FileOutputStream out=new FileOutputStream("D:/课程表.xls");
        workbook.write(out);
        out.close();
        is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

这里写图片描述

5. 设置批注

     public static void main(String[] args) {
            try{
            FileInputStream is = new FileInputStream("D:/课程表.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(is);
            HSSFSheet sheet=workbook.getSheet("课程表");
            HSSFPatriarch patr = sheet.createDrawingPatriarch();
            HSSFClientAnchor anchor = patr.createAnchor(0,0,0,0, 9,0, 11,6);//创建批注位置
            HSSFComment comment = patr.createCellComment(anchor);//创建批注
            comment.setString(new HSSFRichTextString("这是一个批注段落!"));//设置批注内容
            comment.setAuthor("HM");//设置批注作者
            comment.setVisible(true);//设置批注默认显示
            HSSFCell cell = sheet.getRow(0).getCell(0);
            cell.setCellComment(comment);
            FileOutputStream out=new FileOutputStream("D:/课程表.xls");
            workbook.write(out);
            out.close();
            is.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

这里写图片描述

6. 单元格格式操作

public static void main(String[] args) {
        try{
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet=workbook.createSheet("格式转换");
        HSSFRow row0=sheet.createRow(0);
        /**
         * 时间格式转换
         * 我们用第一排第一个、第二个、第三个单元格都设置当前时间
         * 然后第一个单元格不进行任何操作,第二个单元格用内嵌格式,第三个单元格用自定义
         */
        Date date=new Date();
        HSSFCell row1_cell1=row0.createCell(0);
        HSSFCell row1_cell2=row0.createCell(1);
        HSSFCell row1_cell3=row0.createCell(2);
        row1_cell1.setCellValue(date);
        row1_cell2.setCellValue(date);
        row1_cell3.setCellValue(date);
        HSSFCellStyle style1=workbook.createCellStyle();
        style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        HSSFCellStyle style2=workbook.createCellStyle();
        style2.setDataFormat(workbook.createDataFormat().getFormat("yyyy-mm-dd hh:m:ss"));
        row1_cell2.setCellStyle(style1);
        row1_cell3.setCellStyle(style2);
        /**
         * 第二排我们进行小数处理
         * 第一个不进行任何处理,第二个我们用内嵌格式保留两位,第三个我们用自定义
         */
        HSSFRow row1=sheet.createRow(1);
        double db=3.1415926;
        HSSFCell row2_cell1=row1.createCell(0);
        HSSFCell row2_cell2=row1.createCell(1);
        HSSFCell row2_cell3=row1.createCell(2);
        row2_cell1.setCellValue(db);
        row2_cell2.setCellValue(db);
        row2_cell3.setCellValue(db);
        HSSFCellStyle style3=workbook.createCellStyle();
        style3.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
        HSSFCellStyle style4=workbook.createCellStyle();
        style4.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
        row2_cell2.setCellStyle(style3);
        row2_cell3.setCellStyle(style4);
        /**
         * 下面是进行货币的三种形式
         */
        HSSFRow row2=sheet.createRow(2);
        double money=12345.6789;
        HSSFCell row3_cell1=row2.createCell(0);
        HSSFCell row3_cell2=row2.createCell(1);
        HSSFCell row3_cell3=row2.createCell(2);
        row3_cell1.setCellValue(money);
        row3_cell2.setCellValue(money);
        row3_cell3.setCellValue(money);
        HSSFCellStyle style5=workbook.createCellStyle();
        style5.setDataFormat(HSSFDataFormat.getBuiltinFormat("¥#,##0.00"));
        HSSFCellStyle style6=workbook.createCellStyle();
        style6.setDataFormat(workbook.createDataFormat().getFormat("¥#,##0.00"));
        row3_cell2.setCellStyle(style3);
        row3_cell3.setCellStyle(style4);
        FileOutputStream out=new FileOutputStream("D:/格式转换.xls");
        workbook.write(out);
        out.close();
        }catch(Exception e){
        e.printStackTrace();
        }
    }

这里写图片描述

7. 基本计算

public static void main(String[] args) {
        try{
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet=workbook.createSheet("基本计算");
            HSSFRow row=sheet.createRow(0);
            HSSFCell cell0=row.createCell(0);
            cell0.setCellFormula("5*5+2");//可直接赋予一个简单的计算公式
            cell0=row.createCell(1);
            cell0.setCellValue(20);
            cell0=row.createCell(2);
            cell0.setCellFormula("A1+B1");
            cell0=row.createCell(3);
            cell0.setCellFormula("A1-B1");
            cell0=row.createCell(4);
            cell0.setCellFormula("A1*B1");
            cell0=row.createCell(5);
            cell0.setCellFormula("A1/B1");
            FileOutputStream out=new FileOutputStream("D:/基本计算.xls");
            workbook.write(out);
            out.close();
            }catch(Exception e){
            e.printStackTrace();
            }
    }

这里写图片描述

8. SUM函数

public static void main(String[] args) {
        try{
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet=workbook.createSheet("基本计算");
            HSSFRow row=sheet.createRow(0);
            row.createCell(0).setCellValue(1);
            row.createCell(1).setCellValue(2);
            row.createCell(2).setCellValue(3);
            row.createCell(3).setCellValue(4);
            row.createCell(4).setCellValue(5);
            row.createCell(5).setCellValue(6);
            row.createCell(6).setCellValue(7);
            //第七/八列进行计算,两种都等价A1+B1+C1+D1+E1+F1+G1
            row.createCell(7).setCellFormula("sum(A1,B1,C1,D1,E1,F1,G1)");
            row.createCell(8).setCellFormula("sum(A1:G1)");    
            FileOutputStream out=new FileOutputStream("D:/基本计算.xls");
            workbook.write(out);
            out.close();
            }catch(Exception e){
            e.printStackTrace();
            }
    }

这里写图片描述

9. ABS绝对值、INT取整函数、ROUND四舍五入

 public static void main(String[] args) {
        try{
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet=workbook.createSheet("基本计算");
        HSSFRow row0=sheet.createRow(0);
        row0.createCell(0).setCellValue(-1234);
        row0.createCell(1).setCellValue(5678);

        HSSFRow row1=sheet.createRow(1);
        row1.createCell(0).setCellValue(23.456);
        row1.createCell(1).setCellValue(-54.562);

        HSSFRow row2=sheet.createRow(2);
        row2.createCell(0).setCellValue(8.49314);
        row2.createCell(1).setCellValue(12.927);
        /**
         * 取绝对值
         */
        row0.createCell(2).setCellFormula("ABS(A1)");
        row0.createCell(3).setCellFormula("ABS(B1)");
        /**
         * 取整
         */
        row1.createCell(2).setCellFormula("INT(A2)");
        row1.createCell(3).setCellFormula("INT(B2)");
        /**
         * 四舍五入
         */
        row2.createCell(2).setCellFormula("ROUND(A3,1)");
        row2.createCell(3).setCellFormula("ROUND(B3,1)");

        FileOutputStream out=new FileOutputStream("D:/基本计算.xls");
        workbook.write(out);
        out.close();
        }catch(Exception e){
        e.printStackTrace();
        }
    }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值