/**
* 导出报修单
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
* @throws WriteException
*/
public ActionForward print(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException, WriteException {
response.reset();//设置页面不缓存
response.setContentType("application/vnd.ms-excel");//设置文件流导出格式
List expendInfoList=dao.findByProperty("states", 1);//此为查询出数据返回为List
ExpendablesfixToExcel ef=new ExpendablesfixToExcel(response.getOutputStream());//此为定义的导出EXCEL类,将输出流传入到构造函数中
ef.ebfToExcel(expendInfoList);//调用导出类里的导出方法
return null;
}
以下为导出方法:
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import com.ghtn.techschool.entitys.Expendablesfix;
public class ExpendablesfixToExcel {
OutputStream os;//输出流
WritableWorkbook wb;//建立Excel文件
WritableSheet ws;//sheet名称
/**
* 构造函数
* @param os
* @throws IOException
*/
public ExpendablesfixToExcel(OutputStream os)throws IOException{
this.os=os;
}
/**
* 导出Excel方法
* @param list
* @throws WriteException
*/
public void ebfToExcel(List list) throws WriteException{
//设置样式(这个方法算是一个容器,可以放进去好多属性;
//第一个: TIMES是字体大小,这里写的是12;第二个: BOLD是判断是否为斜体,选择true时为斜体;
//第三个: ARIAL;第四个: UnderlineStyle.NO_UNDERLINE 下划线;
//第五个: jxl.format.Colour.RED 字体颜色是红色的)
WritableFont font2 = new WritableFont(WritableFont.TIMES, 12,
WritableFont.BOLD);
WritableCellFormat format2 = new WritableCellFormat(font2);
format2.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
try{
wb=Workbook.createWorkbook(os);
ws=wb.createSheet("报修单", 0);
//第一个是代表列数,
//第二是代表行数,
//第三个代表要写入的内容
//第四个是可选项,是输入这个label里面的样式
//然后通过写sheet的方法addCell()把内容写进sheet里面。
Label labelA=new Label(0,0,"报修人",format2);
ws.addCell(labelA);
Label labelB=new Label(1,0,"所在部门",format2);
ws.addCell(labelB);
Label labelC=new Label(2,0,"报修日期",format2);
ws.addCell(labelC);
Label labelD=new Label(3,0,"消耗品名称",format2);
ws.addCell(labelD);
Label labelF=new Label(4,0,"消耗品报修描述",format2);
ws.addCell(labelF);
writeRecruit(list);//获取动态内容的方法
wb.write();//写入
wb.close();//关闭
}catch(Exception e){
e.printStackTrace();
}
}
private void writeRecruit(List stuList){
if(stuList.size()==0){
return ;