pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<mysql.version>5.1.10</mysql.version>
<poi.version>3.9</poi.version>
<slf4j.version>1.7.21</slf4j.version>
<mybatis.version>3.2.8</mybatis.version>
</properties>
<dependencies>
<!-- mysql-jdbc -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- slf4j associated jar package -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
TestExcel.java
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
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;
/**
* Description:
* 作者:gu.weidong(Jack)
* date:2018年9月27日
* ProjectName:ExcelExport
*/
public class TestExcel {
public static void main(String[] args) throws ParseException {
//创建一个HSSF,对应一个excel
HSSFWorkbook workbook = new HSSFWorkbook();
//在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = workbook.createSheet("学生表");
//在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
//创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell(0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("生日");
cell.setCellStyle(style);
//写入实体数据
// List list = DemoDaoImpl.getStudent();
List list=new LinkedList<>();
list.add(1);
list.add(2);
list.add(2);
list.add(2);
list.add(2);
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
// Student stu = (Student) list.get(i);
//创建单元格,并设置值
row.createCell(0).setCellValue(i);
row.createCell(1).setCellValue("Jack");
row.createCell(2).setCellValue(20);
cell = row.createCell(3);
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
}
//将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/学生表.xls");
workbook.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
显示结果