从JAVA导出数据到Excel

实体类

 

package com.hello.pojo;

import java.util.Date;
import lombok.Data;

/**
* 包名+类名: com.hello.pojo.User
*/

@Data
public class User {
	private String code;
	private String name;
	private Date birth;
	
	
	public User(String code,String name,Date birth){
		this.code = code;
		this.name = name;
		this.birth = birth;
	}
}


 

 

 

代码

 

package com.excel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.hello.pojo.User;

/**
* 包名+类名: com.excel.Test
*/
public class Test {
	
	public static List getUser() throws ParseException{
		List<User> li = new ArrayList<>();
		SimpleDateFormat df =new SimpleDateFormat("yyyy-mm-dd");
		
		User u1 = new User("1","中国制造", df.parse("2000-5-20"));
		User u2 = new User("2", "中国智造", df.parse("2020-03-05"));
		User u3 = new User("3", "中国AI", df.parse("2030-06-15"));
		li.add(u1);
		li.add(u2);
		li.add(u3);
		return li;
	}
	

		//逻辑:1、创建工作簿-->2、创建工作表-->3、创建行-->4、创建表格/cell-->5、写入数据-->6、设置储存路径
	public static void main(String[] args) throws ParseException {
		
		//1、创建工作簿
		Workbook wb = new XSSFWorkbook();
			//1.1、设置表格的格式----居中
			CellStyle cs = wb.createCellStyle();
			cs.setAlignment(HorizontalAlignment.CENTER);
		//2.1、创建工作表
		Sheet sheet = wb.createSheet("学生表格");
				//2.2、合并单元格
				sheet.addMergedRegion(new CellRangeAddress(4, 8, 5, 9));
		//3.1、创建行----表头行
		Row row = sheet.createRow(0);
		//4、创建格
		Cell cell = row.createCell(0);
				cell.setCellValue("学号");
				cell.setCellStyle(cs);
			cell = row.createCell(1);
				cell.setCellValue("姓名");
				cell.setCellStyle(cs);
			cell = row.createCell(2);
				cell.setCellValue("时间");
				cell.setCellStyle(cs);
		
		//5、写入实体数据
		List<User> list = Test.getUser();
		for (int i = 0; i < list.size(); i++) {
			//3.2、创建行----内容行
			row = sheet.createRow(i+1);
			User us = (User)list.get(i);
				//第几行第几格  第一行第一格为“code”
			row.createCell(0).setCellValue(us.getCode());
			row.createCell(1).setCellValue(us.getName());
			row.createCell(2).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(us.getBirth()));
		}
		
		//6、将文件储存到指定位置
		try {
			FileOutputStream fout = new FileOutputStream("g:\\uuu.xlsx");
			wb.write(fout);
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {             try {
              if (fout != null) {
               fout.close();
          }
           if (wb != null) {
            wb.close();
          }
         } catch (IOException e) {
            e.printStackTrace();
       }
       }	}
}

 

运行结果:

 


 

注意:

如果无法导入import org.apache.poi.xssf.usermodel.XSSFWorkbook; ,试着在pom.xml中添加依赖解决

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨咖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值