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");
}
}
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");
}
}