日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java poi生成excel文件_java poi 导出Excel文件

發布時間:2025/3/8 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java poi生成excel文件_java poi 导出Excel文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1,導包 ?poi-3.9-XXX.JAR

2, 創建一個實體對象

public class Student implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

private int id;

private String name;

private int age;

private Date borth;

public Student(int id, String name, int age, Date borth){

this.id = id;

this.name = name;

this.age = age;

this.borth = borth;

}

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 getBorth() {

return borth;

}

public void setBorth(Date borth) {

this.borth = borth;

}

public static long getSerialversionuid() {

return serialVersionUID;

}

3,創建實體數據,也可以獲取數據庫的信息

public class StuData {

public static List> getStuInfo() throws ParseException {

List> listStuInfo = new ArrayList>();

List listStu = new ArrayList();

DateFormat format = new SimpleDateFormat("yyyy-mm-dd");

Student s1 = new Student(1, "zhangsan", 16, format.parse("1987-05-06"));

Student s2 = new Student(2, "li", 17, format.parse("1988-05-06"));

Student s3 = new Student(3, "wangwu", 18, format.parse("1989-05-06"));

Student s4 = new Student(4, "zhaoliu", 19, format.parse("1990-05-06"));

listStu.add(s1);

listStu.add(s2);

listStu.add(s3);

listStu.add(s4);

for (Student stu : listStu) {

Map map = new HashMap();

map.put("id", stu.getId());

map.put("name", stu.getName());

map.put("age", stu.getAge());

map.put("borth", stu.getBorth());

listStuInfo.add(map);

}

return listStuInfo;

}

}

4, 創建表頭,以及單元格的樣式等

private static Map createHeaderInfo(Workbook wb, Sheet sheet, int headerNumber) {

Row row = sheet.createRow(headerNumber);

Map header = createHeader();

for(String str : header.keySet()) {

int rowNumber = (int) header.get(str);

Cell cell = row.createCell(rowNumber);

CellStyle cellstyle = wb.createCellStyle();

cellstyle.setAlignment(CellStyle.ALIGN_CENTER);

Font font = wb.createFont();

font.setBoldweight(Font.BOLDWEIGHT_BOLD);

cellstyle.setFont(font);

cell.setCellStyle(cellstyle);

cell.setCellValue(str);

}

headerNumber++;

return header;

}

5,表頭信息:

private static Map createHeader(){

Map header = new HashMap();

header.put("id", 0);

header.put("name", 1);

header.put("age", 2);

header.put("borth", 3);

return header;

}

6,創建Excel對象,創建sheet頁簽,創建行,創建每行的單元格

public static void main(String[] args) throws ParseException, IOException {

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("StuInfo");

//創建表頭

int rowNumber = 0 ;

Map header = createHeaderInfo(wb, sheet, rowNumber);

List> listStuInfo = StuData.getStuInfo();

for(Map stuMap : listStuInfo) {

rowNumber++;

Row row = sheet.createRow(rowNumber);

Iterator> iterator = header.entrySet().iterator();

while(iterator.hasNext()){

Entry entry = iterator.next();

String headerCell = entry.getKey();

int cellNumber = (int) entry.getValue();

Cell cell = row.createCell(cellNumber);

CellStyle cellstyle = wb.createCellStyle();

cellstyle.setAlignment(CellStyle.ALIGN_CENTER);

cell.setCellStyle(cellstyle);

Object value = stuMap.get(headerCell);

if(value instanceof String) {

cell.setCellValue((String)value);

}else if(value instanceof Date){

cell.setCellValue(((Date) value).toLocaleString());

}else if(value instanceof Integer){

cell.setCellValue((Double.valueOf(value.toString())));

}

}

}

FileOutputStream fos = new FileOutputStream("E:/studentInfo.xlsx");

wb.write(fos);

fos.flush();

fos.close();

System.out.println("OK");

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java poi生成excel文件_java poi 导出Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。