首先要导入poi jar包,在这里声明一下,例子中用的是poi-3.0.1.jar,你也可以下载最新的,
如果没有大的改动的话,大部分插件都是向后兼容的;如果你要想掌握poi,请仔细阅读代码,
为便于阅读与记忆我加了详细注释,希望能给大家一定的帮助,下面我开始代码了:
前台调用:
window.location.href='GroupAction!export.action?groupName='+encodeURI(Group.getValue());
<pre name="code" class="java">/*
* 导出数据
* */
public void export() throws Exception{
try{
/*
* 通过查询获取数据
* */
String groupName = getRequest().getParameter("groupName");
java.net.URLDecoder.decode(groupName,"UTF-8");
groupName = new String(groupName.getBytes("iso-8859-1"),"UTF-8"); //对中文参数的转换
HashMap map = new HashMap();
map.put("groupName", groupName);
List<MmCheckGroupBean2> list = new ArrayList<MmCheckGroupBean2>();
list = mmCheckGroupService.selectBillInfoByGroupName(map);
/*
* POI 操作Excel - 步骤:
* */
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
FileOutputStream fos = new FileOutputStream("test.xls"); // 创建.xls文件
HSSFSheet sheet = workbook.createSheet(); // 创建工作表
sheet.setDefaultColumnWidth ((short)20); // 设置工作表列宽
sheet.setDefaultRowHeight((short)10); // 设置工作表行高
/*
* sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
* */
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象
//列头数组定义
String[] label = {"客户编号","组别","序号"};
//设置列头
HSSFRow row1 = sheet.createRow((short)0); // 在索引0的位置创建行(最顶端的行)
HSSFCell cell1 = null; // 在索引0的位置创建单元格(左上端)
// 定义所需列数
int columnNum = 3;
// 将列头设置到sheet的单元格中
for(int n=0;n<columnNum;n++){
cell1 = row1.createCell((short)(n)); //创建列头对应个数的单元格
cell1.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
cell1.setCellValue(label[n]); //设置列头单元格的值
cell1.setCellStyle(columnTopStyle); //设置列头单元格样式
}
/*
* 将查询出的数据设置到sheet对应的单元格中
* */
for(int i=0;i<list.size();i++){
MmCheckGroupBean2 b = list.get(i);//遍历每个对象
//操作所遍历对象的属性
String custId = b.getCustomerId()==null||b.getCustomerId().equals("")?"":b.getCustomerId().toString();
String group0 = b.getGroup0()==null||b.getCustomerId().equals("")?"":b.getGroup0().toString();
String order0 = b.getOrder0()==null||b.getCustomerId().equals("")?"":b.getOrder0().toString();
//将属性转化成字符串数组的格式以便于写到sheet中
String c[] = {custId,group0,order0};
//创建行(从下面的i+1要注意,第0行是列头,因此创建新行要从下一行开始)
HSSFRow row = sheet.createRow(i+1); //创建所需的行数
for(short j=0;j<columnNum;j++){
HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);//设置单元格的数据类型
cell.setCellValue(c[j]); //设置单元格的值
cell.setCellStyle(style); //设置单元格样式
}
}
workbook.write(fos);// 将workbook对象输出到文件test.xls
fos.flush(); // 缓冲
fos.close(); // 关闭流(养成好的习惯打开了就别忘记关闭)
if(workbook !=null){
// 获取当前时间用作文件名
String filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
getResponse().setContentType("application/ms-excel,charset=gbk");
//getResponse().setHeader("Content-Disposition" ,"filename="+new String(new Date().toString().concat(".xls").getBytes(),"iso-8859-1"));
getResponse().setHeader("Content-Disposition", "filename="+new String(filename.toString().concat(".xls").getBytes(),"iso-8859-1"));
workbook.write(getResponse().getOutputStream());
}
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)11);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/*
* 列数据信息单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
//font.setFontHeightInPoints((short)10);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}