(四)JAVA使用POI操作excel

链接:https://www.cnblogs.com/wishwzp/p/5495076.html

1,字体处理

Demo12.java

package com.wajpzywj.exceldemo.main;

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

import java.io.FileOutputStream;

public class Demo12 {
    public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(1); // 创建一个行

        // 创建一个字体处理类
        Font font=wb.createFont();
        font.setFontHeightInPoints((short)24);//设置字字体高度
        font.setFontName("Courier New");//设置字体
        font.setItalic(true);//设置斜体
        font.setStrikeout(true);//设置删除线

        CellStyle style=wb.createCellStyle();//创建样式
        style.setFont(font);//将字体设置到样式

        Cell cell=row.createCell((short)1);
        cell.setCellValue("This is test of fonts");
        cell.setCellStyle(style);

        FileOutputStream fileOut=new FileOutputStream("d:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

在这里插入图片描述
在这里插入图片描述

2,读取和重写工作簿

Demo13.java

package com.wajpzywj.exceldemo.main;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo13 {
    public static void main(String[] args) throws Exception{
        InputStream inp=new FileInputStream("d:\\工作簿.xls");
        POIFSFileSystem fs=new POIFSFileSystem(inp);
        Workbook wb=new HSSFWorkbook(fs);
        Sheet sheet=wb.getSheetAt(0);  // 获取第一个Sheet页
        Row row=sheet.getRow(1); // 获取第二行
        Cell cell=row.getCell(0); // 获取单元格
        if(cell==null){
            cell=row.createCell(3);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("测试单元格");

        FileOutputStream fileOut=new FileOutputStream("d:\\工作簿1.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

我们上面的例子上读取了工作薄.xls,将读取的数据又创建了工作薄1.xls。
在这里插入图片描述
在这里插入图片描述

3,单元格中使用换行

Demo14.java

package com.wajpzywj.exceldemo.main;

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

import java.io.FileOutputStream;

public class Demo14 {
    public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(2); // 创建一个行
        Cell cell=row.createCell(2);
        cell.setCellValue("我要换行 \n 成功了吗?");

        CellStyle cs=wb.createCellStyle();
        // 设置可以换行
        cs.setWrapText(true);
        cell.setCellStyle(cs);

        // 调整下行的高度
        row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
        // 调整单元格宽度
        sheet.autoSizeColumn(2);

        FileOutputStream fileOut=new FileOutputStream("d:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

在这里插入图片描述
在这里插入图片描述

4,创建用户自定义数据格式

Demo15.java

package com.wajpzywj.exceldemo.main;

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

import java.io.FileOutputStream;

public class Demo15 {
    public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        CellStyle style;
        DataFormat format=wb.createDataFormat();
        Row row;
        Cell cell;
        short rowNum=0;
        short colNum=0;

        row=sheet.createRow(rowNum++);
        cell=row.createCell(colNum);
        cell.setCellValue(111111.25);

        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("0.0")); // 设置数据格式
        cell.setCellStyle(style);

        row=sheet.createRow(rowNum++);
        cell=row.createCell(colNum);
        cell.setCellValue(1111111.25);
        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("#,##0.000"));
        cell.setCellStyle(style);

        FileOutputStream fileOut=new FileOutputStream("d:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值