利用pio的纯java 操作excel的api

package com.kevin.excel;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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;

public class ProcessExcel {
   
    private HSSFWorkbook workbook;
    private HSSFSheet sheet;
    private HSSFRow row;
    private HSSFCell cell;
    private HSSFCellStyle cellStyle;
   
    public ProcessExcel(){
       
    }
   
    /**
     * 创建Excel的工作书册(Workbook)
     * @return workbook
     */
    public HSSFWorkbook createExcelWorkbook(){
       
        workbook = new HSSFWorkbook();
         
        return workbook;
    }
   
    /**
     * 创建Excel的工作sheet
     * @return
     */
    public HSSFSheet createSheet( String sheetName){
       
        sheet =  workbook.createSheet(sheetName);
        return sheet;
    }
   
    /**
     * 创建Excel的sheet的一行
     * @param i
     * @return
     */
    public HSSFRow createRow(int i){
       
        row = sheet.createRow(i);
        return row;
    }
   
    /**
     * 创建一个Excel的单元格
     * @return
     */
    public HSSFCell createCell(short i){
       
        cell = row.createCell(i);
        return cell;
    }
   
    /**
     * 创建单元格的风格
     * @return
     */
    public HSSFCellStyle createCellStyle(){
       
        cellStyle = workbook.createCellStyle();
        cellStyle.setFillBackgroundColor((short)1);
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-mm-dd hh:mm"));
        return cellStyle;
    }
   
    /**
     * @throws IOException
     *
     *
     */
    public void createExcel(String fileName ) throws IOException{
       
        HSSFWorkbook wb = new HSSFWorkbook();
        FileOutputStream fileOut = new FileOutputStream(fileName+".xls");
        HSSFSheet sht = wb.createSheet("students");
       
        //设定列的宽度
        sht.setColumnWidth((short)0,(short)10000);
        sht.setColumnWidth((short)1,(short)10000);
        sht.setColumnWidth((short)2,(short)10000);
        sht.setColumnWidth((short)3,(short)10000);
        int rowNum = 0;
        HSSFRow rw = sht.createRow(rowNum);
        //设定行的高度
        rw.setHeight((short)800);
       
        short cellNum = 0;
        HSSFCellStyle cellStyle = wb.createCellStyle();
        //cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
        //cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
        cellStyle.setWrapText(true);
       
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short)24);
        font.setFontName("Courier New");
        font.setItalic(true);
        font.setStrikeout(false);
        font.setColor(HSSFFont.COLOR_RED);
       
        cellStyle.setFont(font);
       
        HSSFCell cell1 = rw.createCell(cellNum++);
        cell1.setCellStyle(cellStyle);
        cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell1.setCellValue("Full Name");
       
       
        HSSFCell cell2 = rw.createCell(cellNum++);
        cell2.setCellStyle(cellStyle);
        cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell2.setCellValue("年龄");
       
        HSSFCell cell3 = rw.createCell(cellNum++);
        cell3.setCellStyle(cellStyle);
        cell3.setCellValue("Telephone");
        cell3.setAsActiveCell();
       
        HSSFCell cell4 = rw.createCell(cellNum++);
        cell4.setCellStyle(cellStyle);
        cell4.setCellValue("Home");
       
       
       
       
        wb.write(fileOut);
        fileOut.close();
    }
   
   
   
   
   
   
   
   
    /**
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
       
        ProcessExcel p = new ProcessExcel();
        p.createExcel("test");

    }

}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值