java(poi)向Excel中写入数据代码

pom.xml


        UTF-8
        UTF-8
        1.7
        1.7
        5.1.10
        3.9
        1.7.21
        3.2.8
        
    
    
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            org.apache.poi
            poi
            ${poi.version}
        
        
            org.apache.poi
            poi-ooxml
            ${poi.version}
        
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
    

 

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();
     }
  }
}

 

显示结果

java(poi)向Excel中写入数据代码_第1张图片

你可能感兴趣的:(poi,JavaWeb)