简单excel操作

来源:互联网 发布:java action servlet 编辑:程序博客网 时间:2024/05/16 17:26

目录:
创建一个workbook
创建一个sheet
创建cells
创建日期cells
设定单元格格式

说明:
以下可能需要使用到如下的类
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;
import org.apache.poi.hssf.util.HSSFColor;

创建workbook

HSSFWorkbook wb = new HSSFWorkbook();
//使用默认的构造方法创建workbook
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//指定文件名
wb.write(fileOut);
//输出到文件
fileOut.close();

创建一个sheet

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//workbook创建sheet
HSSFSheet sheet2 = wb.createSheet("second sheet");
//workbook创建另外的sheet
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

创建cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//注意以下的代码很多方法的参数是short 而不是int 所以需要做一次类型转换
HSSFRow row = sheet.createRow((short)0);
//sheet 创建一行
HSSFCell cell = row.createCell((short)0);
//行创建一个单元格
cell.setCellValue(1);
//设定单元格的值
//值的类型参数有多中double ,String ,boolean,
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

创建日期cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");

HSSFRow row = sheet.createRow((short)0);

HSSFCell cell = row.createCell((short)0);
//设定值为日期
cell.setCellValue(new Date());

HSSFCellStyle cellStyle = wb.createCellStyle();
//指定日期显示格式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
//设定单元格日期显示格式
cell.setCellStyle(cellStyle);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

设定单元格格式
单元格格式的设定有很多形式包括单元格的对齐方式,内容的字体设置,
单元格的背景色等,因为形式比较多,只举一些例子.以下的例子在
POI1.5中可能会有所改变具体查看API.
..........
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
//创建一个样式
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
//设定此样式的的背景颜色填充
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);

//样式的填充类型。
//有多种式样如:
//HSSFCellStyle.BIG_SPOTS
//HSSFCellStyle.FINE_DOTS
//HSSFCellStyle.SPARSE_DOTS等
style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
//居中对齐
style.setFillBackgroundColor(HSSFColor.GREEN.index);
//设定单元个背景颜色
style.setFillForegroundColor(HSSFColor.RED.index);
//设置单元格显示颜色
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);

 

 

使用poi的hssf生成一个excel文件以后
有一个主类Workbook(相当于一个excel文件)的方法
Workbook.write(OutputStream)可以写到response.getOutputStream()里面
如果事先设置response的contentType为excel和下载的附件名称就可下载excel

    HSSFWorkbook book = _proxy.expertExcel(_formBean,_login);    if(book!=null)    {        response.setContentType ( "application/ms-excel" ) ;        response.setHeader ( "Content-Disposition" ,                             "attachment;filename="+new String("导出Excel.xls".getBytes(),"iso-8859-1")) ;        book.write(response.getOutputStream());    }

其中expertExcel无非是从数据库或者其他地方获取数据创建excel即可.