Java生成Excel文件

生成ExcelAnd创建数据

package com.ninemax.common.utils;

import java.text.SimpleDateFormat;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.ninemax.common.model.ExcelModel;

/**
 * 生成Excel表格
 * 
 * @author Darker
 *
 */
public class ExcelUtil {
	
	/**
	 * @throws Exception 
	 * 
	 * @功能:Excel存放数据
	 */
	public static List<ExcelModel> getExcelModel() throws Exception{
		
		List<ExcelModel> list= new ArrayList<ExcelModel>();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		
		ExcelModel user1= new ExcelModel(1,"one",16,df.parse("2015-12-1"));
		ExcelModel user2= new ExcelModel(1,"two",16,df.parse("2015-12-2"));
		ExcelModel user3= new ExcelModel(1,"three",16,df.parse("2015-12-3"));
		
		list.add(user1);
		list.add(user2);
		list.add(user3);
		
		return list;
	}
	/**
	 * 创建Excel
	 * 
	 * @return
	 * @throws Exception
	 */
	public static HSSFWorkbook CreateExcel() throws Exception {
		//1.创建一个workbook,对应一个Excel文件
		HSSFWorkbook wb= new HSSFWorkbook();
		//2.在workbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet=wb.createSheet("学生表一");
		//3.在sheet中添加表头第0行
		HSSFRow row= sheet.createRow((int)0);
		//4.创建单元格,并且设置表头,设置表头居中
		HSSFCellStyle style=wb.createCellStyle();
		//4.1格式居中
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		//4.2单元格信息设置
		HSSFCell cell=row.createCell((short)0);
		cell.setCellValue("学号");
		cell.setCellStyle(style);
		cell = row.createCell((short)1);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell((short)2);
		cell.setCellValue("年龄");
		cell.setCellStyle(style);
		cell = row.createCell((short)3);
		cell.setCellValue("生日");
		cell.setCellStyle(style);
		
		//5.写入实体数据
		List<ExcelModel>list= ExcelUtil.getExcelModel();
		
		for(int i=0;i<list.size();i++){
			
			row=sheet.createRow((int)i+1);
			
			ExcelModel stu=(ExcelModel)list.get(i);
			
			//4.3单元格信息值填写
			row.createCell((short)0).setCellValue((double)stu.getId());
			row.createCell((short)1).setCellValue(stu.getName());
			row.createCell((short)2).setCellValue((double)stu.getAge());
			cell=row.createCell((short)3);
			cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
		}
		return wb;
	}
	
	
}

Model类(如果不是测试用,可以插入数据库的数据)

package com.ninemax.common.model;

import java.util.Date;

/**
 * 
 * @author Darker
 * 
 */
public class ExcelModel {
	
	private int id;
	private String name;
	private int age;
	private Date birth;

	public ExcelModel() {
	}

	public ExcelModel(int id, String name, int age, Date birth) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.birth = birth;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}
}

启动方法

package com.ninemax.common.starter;

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ninemax.common.utils.ExcelUtil;

public class ExcelStarter {
	/**
	 * 启动方法
	 * 
	 * @param args
	 * 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		//调用创建Excel的方法
		HSSFWorkbook wb = ExcelUtil.CreateExcel();
		//将文件保存到指定位置
		FileOutputStream fous= new FileOutputStream("E:/students.xls");
		//将设置的内容写入到Excel文件中
		wb.write(fous);
		//关闭输出流
		fous.close();
	}


}


转载于:https://my.oschina.net/Tsher2015/blog/550456

要在 Java生成 Excel 文件,有多种方式可以实现。其中一种比较常见的方式是使用 Apache POI 库。下面是一个简单的示例代码,可以使用 Apache POI 在 Java生成一个 Excel 文件,并将数据写入其中。 ```java 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.xssf.usermodel.XSSFWorkbook; public class ExcelGenerator { public static void main(String[] args) throws IOException { // 创建一个新的 Excel 工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个新的工作表 String sheetName = "Sheet1"; workbook.createSheet(sheetName); // 在工作表中创建一行 Row row = workbook.getSheet(sheetName).createRow(0); // 在行中创建单元格,并写入数据 Cell cell1 = row.createCell(0); cell1.setCellValue("Hello"); Cell cell2 = row.createCell(1); cell2.setCellValue("World"); // 保存 Excel 文件 String filename = "example.xlsx"; FileOutputStream outputStream = new FileOutputStream(filename); workbook.write(outputStream); workbook.close(); } } ``` 在这个示例中,我们首先创建了一个新的 Excel 工作簿。然后,我们创建了一个新的工作表,并在其中创建了一行。最后,我们在行中创建了两个单元格,并将数据写入其中。最后,我们将 Excel 文件保存到磁盘上。 需要注意的是,这个示例中使用的是 XSSFWorkbook 类,它可以创建 Excel 2007 格式的文件。如果需要创建 Excel 2003 格式的文件,可以使用 HSSFWorkbook 类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值