Excel自定义文档生成

来源:互联网 发布:男生不追女生 知乎 编辑:程序博客网 时间:2024/06/06 03:05
package com.company.item.util.ExcelInfo;




    import java.io.FileOutputStream;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.HashMap;
import java.util.List;  
      
import java.util.Map;


    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;
    
/**
 * 
 * @author zt
 *
 */
    public class CreateExcel {
        
/*自定义导出EXCEL模板字段*/
private static final String[] EXCELTITLES = {"省份","企业名称","企业税号","税控盘号","税控码","开票机号"};
     
        public static void createExcel(String[] EXCELTITLES,String sheetName,List<Map<String,String>> mapList){
            /*创建一个EXCEL文档对象*/
            HSSFWorkbook hswb = new HSSFWorkbook();
            /*创建EXCEL中的一个子表,如果数据量很大可以创建多个子表*/
            HSSFSheet hssfsheet = hswb.createSheet(sheetName);
            /*在sheet表对象中创建标题行(默认为下标为0)*/
            HSSFRow titleRow = hssfsheet.createRow(0);
            /*设置Excel的文档表格样式*/
            HSSFCellStyle style = hswb.createCellStyle();
            /*样式居中*/
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            /*为该行 行对象row创建单元格,并设标题名称*/
            HSSFCell cell = null;
            for(int i=0;i<EXCELTITLES.length;i++){
        cell = titleRow.createCell(i);
        cell.setCellValue(EXCELTITLES[i]);
        cell.setCellStyle(style);
            }
            
            /*将数据填到excel表格中,根据mapList的大小得到需要创建的表格内容行数*/
            HSSFRow contentRow = null;
            HSSFCell contentCell = null;
            for(int i=0;i<mapList.size();i++){
        contentRow = hssfsheet.createRow(i+1);
        for(int f=0;f<EXCELTITLES.length;f++){
           contentCell = contentRow.createCell(f);
           contentCell.setCellValue(mapList.get(i).get(EXCELTITLES[f]));
           contentCell.setCellStyle(style);
        }
            }
            
            /*将生成的EXCEL文件保存到指定位置*/
            try{
            FileOutputStream fos = new FileOutputStream("D:\\excelCreate.xls");
            hswb.write(fos);
            fos.flush();
            fos.close();
            }catch(Exception e){
        e.printStackTrace();
            }
        }
        public static void main(String[] args) throws Exception  
        {  
            List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;
            
            for(int i = 0;i<10;i++){
        map = new HashMap<String,String>();
       
        map.put(EXCELTITLES[0], "四川省");
        map.put(EXCELTITLES[1], "传化注册码导出测试企业");
        map.put(EXCELTITLES[2], "512102412001112");
        map.put(EXCELTITLES[3], "512512412");
        map.put(EXCELTITLES[4], "");
        map.put(EXCELTITLES[5], "0");
        mapList.add(map);
            }
            createExcel(EXCELTITLES,"注册码申请",mapList);
            
        }  
    }  

0 0