poi生成excel

来源:互联网 发布:中国工业实力知乎 编辑:程序博客网 时间:2024/06/15 16:37

poi使用总结

首先介绍poi是做什么的,poi提供APIJava程序对Microsoft Office格式档案读和写的功能。

基本功能

结构:

HSSF- 提供读写Microsoft Excel格式档案的功能。

XSSF- 提供读写Microsoft ExcelOOXML格式档案的功能。

HWPF- 提供读写Microsoft Word格式档案的功能。

HSLF- 提供读写Microsoft PowerPoint格式档案的功能。

HDGF- 提供读写Microsoft Visio格式档案的功能。

下面重点介绍一下HSSFHSSF主要是针对Excel操作生成api接口

下面一段代码生成一个excel文件

packagecom.form.utils;


importorg.apache.poi.hssf.usermodel.*;
importorg.apache.poi.ss.util.CellRangeAddress;

importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjava.util.List;


public classPOIUtils {
    private staticStringFileOutPutPath="D://test.xls";
    public static voidexportUserExcel()
    {
        List list=newArrayList();
        try{
            //1.创建工作簿
            HSSFWorkbook workbook =new HSSFWorkbook();
            //1.1创建合并单元格对象
            CellRangeAddress callRangeAddress =new CellRangeAddress(0,0,0,4);//起始行,结束行,起始列,结束
            //1.2头标题样式
            HSSFCellStyle headStyle =createCellStyle(workbook,(short)16);
            //1.3列标题样式
            HSSFCellStyle colStyle =createCellStyle(workbook,(short)13);
            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("用户列表");
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(25);
            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);

            //加载单元格样式
            cell.setCellStyle(headStyle);
            cell.setCellValue("用户列表");

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            String[] titles = {"用户名","账号","所属部门","性别","电子邮箱"};
            for(inti=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }


            //4.操作单元格;将用户列表写入excel
            if(list !=null)
            {
                for(intj=0;j<list.size();j++)
                {
                    //创建数据行,前面有两行,头标题行和列标题行
               /**
                    HSSFRow row3 = sheet.createRow(j+2);
                    HSSFCell cell1 = row3.createCell(0);
                    cell1.setCellValue(list.get(j).getName());
                    HSSFCell cell2 = row3.createCell(1);
                    cell2.setCellValue(list.get(j).getAccount());
                    HSSFCell cell3 = row3.createCell(2);
                    cell3.setCellValue(list.get(j).getDept());
                    HSSFCell cell4 = row3.createCell(3);
                    cell4.setCellValue(list.get(j).isGender()?"男":"女");
                    HSSFCell cell5 = row3.createCell(4);
                    cell5.setCellValue(list.get(j).getEmail());
                */
                
}
            }
            FileOutputStream outputStream=newFileOutputStream(FileOutPutPath);
            //5.输出
            workbook.write(outputStream);
            outputStream.close();
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }


    /**
     *
     *
@paramworkbook
     *
@paramfontsize
     *
@return单元格样式
     */
    
private staticHSSFCellStyle createCellStyle(HSSFWorkbook workbook, shortfontsize) {
        //TODO Auto-generated method stub
        
HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }
}

 

 

原创粉丝点击