Apache POI练习生成EXCEL(2)

package com.ysstect.test.poi;



import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

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.CellReference;
import org.apache.poi.hssf.util.Region;

import com.ysstect.test.po.Student;

public class StudentExcel {
	public static String outputFile = "d:\\student.xls";
	public static void main(String[] args) {
		try{
			FileOutputStream out = new FileOutputStream(outputFile);
			Student student1 = new Student("张三", "男", 18, "三年二班", 99);
			Student student2 = new Student("lisi", "男", 18, "三年二班", 99);
			List<Student> students = new ArrayList<Student>();
			students.add(student1);
			students.add(student2);
			createExcel createExcel = new createExcel();
			HSSFWorkbook workbook  = createExcel.createExcel(students);
			workbook.write(out);
			out.flush();
			out.close();
			System.out.println("创建完成");
			
		}catch(Exception e){
			
		}
		
	}
}

class createExcel{
	public HSSFWorkbook createExcel(List<Student> list){
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet  = workbook.createSheet("学生信息表");
		
		//创建样式
		
		//字体样式
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		
		
		//表格样式
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		//指定单元格居中对齐
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//指定单元格垂直居中对齐
		cellStyle.setWrapText(true);//指定单元格自动换行
		
		cellStyle.setFont(font);
		
		//创建表头
		HSSFRow headerRow = sheet.createRow(0);
		headerRow.setHeightInPoints(25f);//设置行高度
		HSSFCell nameHeader = headerRow.createCell((short) 0);
		nameHeader.setCellValue("学生");
		nameHeader.setCellStyle(cellStyle);
		
		HSSFCell sexHeader = headerRow.createCell((short) 1);
		sexHeader.setCellValue("性别");
		sexHeader.setCellStyle(cellStyle);
		
		HSSFCell ageHeader = headerRow.createCell((short) 2);
		ageHeader.setCellValue("年龄");
		ageHeader.setCellStyle(cellStyle);
		
		HSSFCell classHeader  = headerRow.createCell((short) 3);
		classHeader.setCellValue("班级");
		classHeader.setCellStyle(cellStyle);
		
		HSSFCell scoreHeader   = headerRow.createCell((short) 4);
		scoreHeader.setCellValue("成绩");
		scoreHeader.setCellStyle(cellStyle);
		
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row = sheet.createRow(i+1);
			row.setHeightInPoints(20f);
			Student student = list.get(i);
			HSSFCell nameCell= row.createCell((short) 0);
			nameCell.setCellValue(student.getName());
			nameCell.setCellStyle(cellStyle);
			
			HSSFCell sexCell= row.createCell((short) 1);
			sexCell.setCellValue(student.getGender());
			sexCell.setCellStyle(cellStyle);
			
			HSSFCell ageCell= row.createCell((short) 2);
			ageCell.setCellValue(student.getAge());
			ageCell.setCellStyle(cellStyle);
			
			
			HSSFCell classCell= row.createCell((short) 3);
			classCell.setCellValue(student.getSclass());
			classCell.setCellStyle(cellStyle);
			
			HSSFCell scoreCell= row.createCell((short) 4);
			scoreCell.setCellValue(student.getScore());
			scoreCell.setCellStyle(cellStyle);
			
			
		}
		//合并单元格获得最后一行
		sheet.getLastRowNum();
		//分别代表起始行、起始列、结束行、结束列
		sheet.addMergedRegion(new Region(1, (short)3, sheet.getLastRowNum(), (short)3));
		
		return workbook;
	}
	
}


效果如下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache POI是一个用于操作Excel文档的Java库。它可以读取、写入和修改Excel文件,包括xls和xlsx格式的文件。 对于Excel版本的支持,Apache POI可以同时处理Excel 2003和Excel 2007及以上版本的文件。Excel 2003版本的文件扩展名为.xls,而Excel 2007及以上版本的文件扩展名为.xlsx。 使用Apache POI进行Excel操作时,你需要下载Apache POI的相关jar包,并将其添加到你的项目中。你还需要了解如何使用Apache POI的API来读取、写入和修改Excel文件。 如果你使用的是Maven项目,你可以通过在pom.xml文件中添加所需的依赖来配置Apache POI。这样可以简化项目的管理和构建过程。 总结来说,Apache POI是一个强大的Java库,可以帮助你在Java应用程序中操作Excel文件,包括读取、写入和修改Excel文件。你只需要下载相应的jar包,并按照官方文档学习如何使用它即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [apache-poi导出数据到excel(SXSSF)](https://blog.csdn.net/qq_36256590/article/details/125558022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Apache POI Excel操作](https://download.csdn.net/download/j960828/10774019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值