packagecom.sxdx.excelpoi.action;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjxl.Cell;importjxl.CellType;importjxl.NumberCell;importjxl.Sheet;importjxl.Workbook;importjxl.read.biff.BiffException;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.HorizontalAlignment;importorg.apache.poi.ss.usermodel.VerticalAlignment;importorg.apache.poi.ss.util.CellRangeAddress;/*** HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
**/
public classPoiAction {/*** 生成excel
*@paramargs*/
public static voidmain(String[] args) {
HSSFWorkbook wb= new HSSFWorkbook();//创建HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("sheet0");//创建HSSFSheet对象//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,10));
sheet.setDefaultRowHeightInPoints(20);//设置缺省列高
sheet.setDefaultColumnWidth(8);//设置缺省列宽//设置指定列的列宽,256 * 50这种写法是因为width参数单位是单个字符的256分之一
sheet.setColumnWidth(0, 256 * 30);//设置单元格的横向和纵向对齐方式
HSSFCellStyle cellStyle =wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//-----------------------------------------------------------------------
HSSFRow row0 = sheet.createRow(0);//创建HSSFRow对象
row0.setHeight((short) 600);//设置行高
HSSFCell cell0 = row0.createCell(0);
cell0.setCellValue("考勤结果表");
cell0.setCellStyle(cellStyle);
HSSFRow row1= sheet.createRow(1);//创建HSSFRow对象//创建HSSFCell对象 HSSFCell cell = row.createCell(0)//设置单元格的值
for(int i=0;i<31;i++){
HSSFCell cell1=row1.createCell(i);
cell1.setCellValue(i+1);
cell1.setCellStyle(cellStyle);
}
HSSFRow row2= sheet.createRow(2);for(int i=0;i<31;i++){
HSSFCell cell2=row2.createCell(i);
cell2.setCellValue("正常");
cell2.setCellStyle(cellStyle);
}
HSSFRow row3= sheet.createRow(3);for(int i=0;i<31;i++){
HSSFCell cell3=row3.createCell(i);
cell3.setCellValue("迟到");
cell3.setCellStyle(cellStyle);
}
HSSFRow row4= sheet.createRow(4);for(int i=0;i<31;i++){
HSSFCell cell4=row4.createCell(i);
cell4.setCellValue("请假");
cell4.setCellStyle(cellStyle);
}try{//输出Excel文件
FileOutputStream output = new FileOutputStream("d:\\workbook.xls");
wb.write(output);
output.flush();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 读取excel*/
public static voidreadExcel(){//导入已存在的Excel文件,获得只读的工作薄对象
FileInputStream fis = null;try{
fis= new FileInputStream("d:\\workbook.xls");
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
Workbook wk= null;try{
wk=Workbook.getWorkbook(fis);
}catch(BiffException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}//获取第一张Sheet表
Sheet sheet = (Sheet) wk.getSheet(0);//获取总行数
int rowNum =sheet.getRows();//从数据行开始迭代每一行
for(int i=0;i
System.out.println(sheet.getCell(0, i).getContents());
}try{
fis.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
wk.close();
}
}