写表格

package com.xiang;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
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.xiang.bean.PageData;

public class ExcelWriter {
    /**
     * 导出Excel
     * 
     * @param sheetName
     *            sheet名称
     * @param title
     *            标题
     * @param values
     *            内容
     * @param wb
     *            HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        // 声明列对象
        HSSFCell cell = null;

        // 创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        // 创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                // 将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }

    public static void main(String[] args) throws IOException {
        Reader br = new InputStreamReader(System.in);
        List<PageData> list = new ArrayList<PageData>();
        list.add(new PageData("张三", "男", "北大", "电商"));
        list.add(new PageData("李四", "男", "北大", "电商"));
        list.add(new PageData("旺三", "男", "北大", "电商"));
        // excel标题
        String[] title = { "名称", "性别", "学校", "班级" };

        // excel文件名
        String fileName = "学生信息表" + System.currentTimeMillis() + ".xls";
        // sheet名
        String sheetName = "学生信息表";
        String content[][] = new String[list.size()][4];
        for (int i = 0; i < list.size(); i++) {
            PageData obj = list.get(i);
            content[i][0] = obj.getStuName();
            content[i][1] = obj.getStuSex();
            content[i][2] = obj.getStuSchoolName();
            content[i][3] = obj.getStuClassName();
        }
        System.out.println(Arrays.toString(content));
        // 创建HSSFWorkbook
        HSSFWorkbook wb = ExcelWriter.getHSSFWorkbook(sheetName, title, content, null);
        OutputStream os = new FileOutputStream(fileName);
        wb.write(os);
        os.flush();
        os.close();
    }
}

 

posted @ 2021-05-10 09:28  傲云萧雨  阅读(93)  评论(0编辑  收藏  举报