这是提交方式..
document.location.href="servlet/ExportServlet";
servlet中
{
String fileName = "exportBOM.xls";
try {
fileName = new String(fileName.getBytes(), "iso8859-1");//解决中文 文件名问题
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("content-disposition", "attachment; filename="+ fileName);// 设定输出文件头
response.setContentType("application/msexcel;charset=UTF-8");// 定义输出类型
getInputStream(response,bomId);
}
public void getInputStream(HttpServletResponse response,String bomId) {
BCompGoods bg=new BCompGoods();
try {
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("Sheet1", 0);
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 8,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label label1 = new Label(0, 0, "物料编号", wcfFC);
ws.addCell(label1);
Label label2 = new Label(1, 0, "物料名称", wcfFC);
ws.addCell(label2);
Label label3 = new Label(2, 4, "规格型号", wcfFC);
ws.addCell(label3);
Label label4 = new Label(3, 0, "封装", wcfFC);
ws.addCell(label4);
Label label5 = new Label(4, 0, "单位", wcfFC);
ws.addCell(label5);
Label label6 = new Label(5, 0, "子BOM编号", wcfFC);
ws.addCell(label6);
Label label7 = new Label(6, 0, "子BOM名称", wcfFC);
ws.addCell(label7);
Label label8 = new Label(7, 0, "子项数量", wcfFC);
ws.addCell(label8);
Label label9 = new Label(8, 0, "仓库名称", wcfFC);
ws.addCell(label9);
Resultobj rt=bg.searchBOM(bomId);
for(int i=1;i<rt.getRows();i++){
Label name = new Label(0, i, rt.getCell("CODE", i));
ws.addCell(name);
Label num = new Label(1, i, rt.getCell("SHORTNAME", i));
ws.addCell(num);
Label num2 = new Label(2, i, rt.getCell("STYLE", i));
ws.addCell(num2);
Label num3 = new Label(3, i, rt.getCell("APPROVALNO", i));
ws.addCell(num3);
Label num4 = new Label(4, i, rt.getCell("Name", i));
ws.addCell(num4);
Label num5 = new Label(5, i, rt.getCell("SUBBOMCODE", i));
ws.addCell(num5);
Label num6 = new Label(6, i, rt.getCell("SUBBOMNAME", i));
ws.addCell(num6);
Label num7 = new Label(7, i, rt.getCell("STANDBATCH", i));
ws.addCell(num7);
Label num8 = new Label(8, i, rt.getCell("storeName", i));
ws.addCell(num8);
}
//关闭操作
wwb.write();
wwb.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GeneralException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
就完成 导出excel了.