论坛首页 Java企业应用论坛

利用apache poi来创建excel报表的例子,可供学习

浏览 1913 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-11-24  

要用到poi创建excel的同志来这里看看,会的给点意见,不会的学习学习

 

 

注:poi的jar包自己到官网下载

 

package com.poi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class WorkBook {
    public static void main(String[] args) {
    	String outPath = "D:\\输出文档";
    	WorkBook workBook = new WorkBook();
    	List<String> emplist = new ArrayList<String>();
    	StringBuffer sb = new StringBuffer();
    	for (int i = 0; i < 10; i++) {
			sb.append(i+"`");
			sb.append("卢水发"+i+"`");
			sb.append("158706885 -"+i+"`");
			sb.append("男");
			sb.append("java软件工程师");
			emplist.add(sb.toString());
			sb = new StringBuffer();
		}
		String result = workBook.createWorkBook(emplist,outPath);
    	System.out.println("程序创建工作本的结果是:"+result);
	}
    
    /**
     * 工作本
     * @param outPath
     * @return
     */
    public String createWorkBook(List emplist,String outPath){
    	try {
    		File file = new File(outPath);
    		if(!file.exists()){
    			file.mkdirs();
    		}
    		//创建工作簿
			Workbook wb = new HSSFWorkbook();
			//创建工作表
			Sheet sheet1 = wb.createSheet("员工表");
			//创建一行
			for (int i = 0; i < emplist.size(); i++) {
				String emp = (String) emplist.get(i);
				String[] emps = emp.split("`");
				Row row = sheet1.createRow(i);
				System.out.println("第 "+i+" 行。"+emp);
				for (int j = 0; j < emps.length; j++) {
					 sheet1.setColumnWidth(j, 20 * 256);
					 sheet1.setDefaultRowHeight((short) (2 * 256));
					 createCell(wb,emps[j], row, (short) j, CellStyle.ALIGN_CENTER, CellStyle.ALIGN_CENTER);
				     System.out.println("第 "+i+" 行。第"+j+"单元格");
				}
			}
		    Sheet sheet2 = wb.createSheet("考勤表");
		    Sheet sheet3 = wb.createSheet("宝宝表");
			FileOutputStream fileOut = new FileOutputStream(file+"\\workbook.xls");
			wb.write(fileOut);
			fileOut.close();
			return "输出"+file+"成功了!";
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "出错了!";
    }
    
    //设置表格的对齐样式
    private static void createCell(Workbook wb,String cellValue, Row row, short column, short halign, short valign) {
        Cell cell = row.createCell(column);
        cell.setCellValue(cellValue);
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(halign);
        cellStyle.setVerticalAlignment(valign);
        //设置边框颜色
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor(IndexedColors.GREEN.getIndex());
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor(IndexedColors.GREEN.getIndex());
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex());
        //设置字体
        Font font = wb.createFont();
        font.setFontHeightInPoints((short)12);
        font.setFontName("Courier New");
        font.setItalic(false);
        font.setBoldweight((short)2);
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
    }
}

 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics