poi java 打印输出Excel

用idea 创建的maven工程,导入2个依赖包,写了一个测试类。

pom.xml    引入依赖包

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
    </dependency>
</dependencies>

测试类

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

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


public class PoiTest {
    @Test
    public void HSSF() throws IOException {
        //1.创建一个工作簿文件
        Workbook wb = new HSSFWorkbook();
        //2.创建一个工作簿sheet
        //默认名字
        Sheet sheet=wb.createSheet();
        //取个名字
        Sheet sheet = wb.createSheet("newSheetName");
        //3.创建行对象,创建到第6行。
        Row row = sheet.createRow(5);
        //此处可以设置行高。
        row.setHeight((short) 1500);
       //4.在此行创建列 在第4列
        Cell nCell = row.createCell(3);
       //5.给单元格设置内容
        nCell.setCellValue("张三的歌");


//==============以下内容设置单元格格式====================
//创建单元格样式
        CellStyle cellStyle = wb.createCellStyle();
        //创建一个字体对象
        Font font = wb.createFont();
        //给这个字体样式赋值
        font.setFontName("华文行楷");//设置字体的格式
        font.setColor((short)35);//设置颜色
        font.setFontHeightInPoints((short) 40);//设置字体的大小
        //将字体格式设置赋给样式
        cellStyle.setFont(font);
        //将样式赋值给单元格
        nCell.setCellStyle(cellStyle);
        //=================以上内容设置单元格格式=================


        //6.保存
        OutputStream os = new FileOutputStream(new File("f:\\new0.xls"));
        wb.write(os);
        //7.关闭
        os.close();
    }
}






猜你喜欢

转载自blog.csdn.net/l23456789o/article/details/80462101
今日推荐