使用Java eclipse 创建一个指定内容的表格

 众所周知,excel是一款功能强大,非常出色的电子表格软件,那么,如何使用eclipse来创建excel表

依然是采用外部信息流的方式来创建一个新的表格文件,比如说开头使用workbook,outputsteam()创建目标文件,最后使用write(out)方法,实现内容填充。

为什么要使用workbook来实现有关excel的相关操作,原因是因为excel的读取和输入采用字节流的方式,但是因为excel并不是普通的字节流文件。

值得一提的是,在这篇文章中,关于类对象的实例化,都是通过Workbook对象的create。。。方法实现的。

总体框架如下:

	Workbook wb=new SXSSFWorkbook();
	FileOutputStream fos=new FileOutputStream("E:\\系统默认\\桌面\\test.xlsx");
    wb.write(fos);

先来看一个简单的例子

package excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.streaming.SXSSFWorkbook;

public class demo {
public static void main(String[] args) throws IOException {
	//创建一个workbook
	Workbook wb=new SXSSFWorkbook();
	FileOutputStream fos=new FileOutputStream("E:\\系统默认\\桌面\\test.xlsx");
	Sheet sh=wb.createSheet();
	for(int i=0;i<100;i++)
	{
		Row row =sh.createRow(i);
		Cell cell=row.createCell(0);
		cell.setCellValue(i);
	}
	wb.write(fos);
}
}

可以看出来在createRow/Cell(这里面的内容为下标),指哪里打哪里

了解了这个之后,我们可以进行创作一个常见的表格内容

首先,创建一个表头

        Row row=sh.createRow(0);
		Cell cell= row.createCell(0);
		cell.setCellValue("序号");
		Cell cell1= row.createCell(1);
		cell1.setCellValue("优惠码");
		Cell cell2= row.createCell(2);
		cell2.setCellValue("校验码");
		Cell cell3= row.createCell(3);
		cell3.setCellValue("创建日期");

接下来创建内容

  • 对序号这一列进行一步操作
for(int i=1;i<6;i++)
	{	Row row1 =sh.createRow(i);
	
		Cell cell4=row1.createCell(0);
		cell4.setCellValue(i);
	}
  • 对优惠码,校验码,日期这三列进行同时操作
		Cell cell5=row1.createCell(1);
		cell5.setCellValue(UUID.randomUUID().toString().substring(0,8));
		Cell cell6=row1.createCell(2);
		cell6.setCellValue(UUID.randomUUID().toString().substring(0,4));
		Cell cell7=row1.createCell(3);
		cell7.setCellValue(LocalDateTime.of(2023, 6, 9, 15, 46));

可以发现日期出来之后并不是x年x月x日,而是一个数字

那么如何将日期修改为年月日这种格式的呢?

接下来就是有关样式进行操作(包括字体样式,单元格样式,日期样式)

这里要介绍到一个类,也是通过Workbook的接口实现的,DataFrame

DataFormat dataFormat=wb.createDataFormat();
short style = dataFormat.getFormat("yyyy-mm-dd HH:mm:ss");

上述操作为指定在单元格中输出的日期格式

cellstyle.setDataFormat(style);
cell7.setCellStyle(cellstyle);

完成了日期格式的转换

package excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
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.streaming.SXSSFWorkbook;

public class demo {
@SuppressWarnings("null")
public static void main(String[] args) throws IOException {
	//创建一个workbook
		Workbook wb=new SXSSFWorkbook();
		FileOutputStream fos=new FileOutputStream("E:\\系统默认\\桌面\\test.xlsx");
		Sheet sh=wb.createSheet();
		CellStyle cellstyle=wb.createCellStyle();
		DataFormat dataFormat=wb.createDataFormat();
		cellstyle.setAlignment(HorizontalAlignment.CENTER);
		short style = dataFormat.getFormat("yyyy-mm-dd HH:mm:ss");
	    cellstyle.setDataFormat(style);
		CellStyle cellstyle1=wb.createCellStyle();
		cellstyle1.setAlignment(HorizontalAlignment.CENTER);
		
	    
		Row row=sh.createRow(0);
		Cell cell= row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(cellstyle);
		Cell cell1= row.createCell(1);
		cell1.setCellValue("优惠码");
		cell1.setCellStyle(cellstyle);
		Cell cell2= row.createCell(2);
		cell2.setCellValue("校验码");
		cell2.setCellStyle(cellstyle);
		Cell cell3= row.createCell(3);
		cell3.setCellValue("创建日期");

//HeaderCellStyle headerCellStyle=wb.createCellStyle();

//headerCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM)
	for(int i=1;i<6;i++)
	{	Row row1 =sh.createRow(i);
	
		Cell cell4=row1.createCell(0);
		cell4.setCellValue(i);
		cell4.setCellStyle(cellstyle1);
		Cell cell5=row1.createCell(1);
		cell5.setCellValue(UUID.randomUUID().toString().substring(0,8));
		Cell cell6=row1.createCell(2);
		cell6.setCellValue(UUID.randomUUID().toString().substring(0,4));
		cell4.setCellStyle(cellstyle1);
		Cell cell7=row1.createCell(3);
		cell7.setCellValue("2023-6-9 16:39");
		cell7.setCellStyle(cellstyle);
		
		
	}
	
wb.write(fos);
}
}

 

关于这篇文章就告一小段落啦!bye~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值