POI

下载地址:demo

package poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 
 * @author bxh
 * 
 * HSSFWorkbook  工作薄
 * HSSFSheet     sheet 
 * HSSFRow       单元行
 * HSSFCell      单元格
 * 
 * 
 * HSSFDataFormat  日期格式
 * BorderStyle     边框风格
 * IndexedColors   颜色枚举
 */
public class MyExcel {
    private final String fileName="c://debug.xls";
    private HSSFWorkbook wb;
    private HSSFSheet    sheet;
    private HSSFCellStyle style;
    public MyExcel() {
        File file = new File(fileName);
        if(file.exists()){
            FileInputStream io;
            try {
                io    = new FileInputStream(file);
                wb    = new HSSFWorkbook(io);
                sheet = wb.getSheetAt(0);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            wb    =   new HSSFWorkbook();
            sheet = wb.createSheet("sheet1");

            initTitle();

        }
    }
    /**
     * excle标题
     */
    private void initTitle() {
        HSSFRow  titleRow      = sheet.createRow(0);
        HSSFCell cellName = titleRow.createCell(0);
        HSSFCell cellSex  = titleRow.createCell(1);
        HSSFCell cellAge  = titleRow.createCell(2);

        cellName.setCellValue("姓名");
        cellSex.setCellValue("性别");
        cellAge.setCellValue("年龄");

        initStyle();

        cellName.setCellStyle(style);
        cellSex.setCellStyle(style);
        cellAge.setCellStyle(style);
    }
    /**
     * 写出excel文件
     */
    public void writeToExcel(){
        PrintStream bookOut;
        try {
            bookOut = new PrintStream(fileName);
            wb.write(bookOut);
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 写入单元格数据
     * @param s       单元夹
     * @param datas   数据  前面数组是  行  后面是数据列  
     *
     * {{cell,cell,cell},{cell,cell,cell}} 该记录有两个row 一个row 有3 个cell 
     */
    public void createRow(HSSFSheet s ,String datas [][] ){
        if(s==null){
            s=sheet;
        }
        //单元格总行数
        int rowNo= s.getLastRowNum()+1;
        for (int i = 0; i < datas.length; i++) {
            HSSFRow row = s.createRow(i+rowNo);
            for (int j = 0; j < datas[i].length; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(datas[i][j]);
            }
        }
    }

    /**
     * 合并单元格
     * @param sheet    合并的单元夹
     * @param firstRow 首个单元格开始位置
     * @param lastRow  最后单元格开始位置
     * @param firstCol 首个单元格结束始位置
     * @param lastCol  最后单元格结束位置
     */
    public void MergeExcel(HSSFSheet sheet,int firstRow,int lastRow, int firstCol,int lastCol){
        sheet.addMergedRegion( new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
    }
    public void initStyle(){
        style = wb.createCellStyle();

        // 设置这些样式
        style.setAlignment(HorizontalAlignment.CENTER);//水平居中 
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

         //背景色
        style.setFillPattern(FillPatternType.BIG_SPOTS); 
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); 

        //设置边框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);  


        // 生成一个字体
        HSSFFont font = wb.createFont();
        font.setColor(IndexedColors.RED.getIndex());
        font.setFontHeightInPoints((short) 10);
        font.setFontName("微软雅黑");
        font.setBold(true);

        // 把字体 应用到当前样式
        style.setFont(font);

        //自动换行  
        style.setWrapText(true);

    }
}
package poi;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value=Parameterized.class)
public class TestMyExcel {

    private String name ;
    private String sex;
    private String age ;

    public TestMyExcel(String name , String sex, String age) {
        this.age=age;
        this.name=name;
        this.sex=sex;
    }
    @Parameters
    public static Collection testDate(){
        return  Arrays.asList(new Object[][]{
                {"赵6","男","33"},
                {"张三","女","21"},
                {"李四","女","22"},
                {"王五","男","33"}

        });
    }

    /**
     * junit 测试入口  
     */
    @Test
    public void TestWookBook(){
        MyExcel myExcel = new MyExcel();
        myExcel.createRow(null, new String[][]{{name,sex,age}});
        myExcel.writeToExcel();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值