后台:
public InputStream jxl2ExcelProduct() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// 2.生成工作簿
WritableWorkbook wb = Workbook.createWorkbook(baos);
// 3.生成工作表
WritableSheet sheet = wb.createSheet("工作表1", 0); //sheet取名为“工作表1”
// 5.工作表中加入单元格
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat titleFormat = new WritableCellFormat(wfont);
String title[] = { "类别/名称", "编号", "助记码", "规格型号", "类型", "默认单价", "包装", "库存",
"单位", "批准文号", };
for (int i = 0; i < title.length; i++) {
Label titleLabel = new Label(i, 0, title[i], titleFormat);
try {
sheet.addCell(titleLabel);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
List list = getMsgProduct();
Product pd;
Label content;
int col = 1;
for (int c = 0; c < list.size(); c++) {
pd = new Product();
pd = (Product) list.get(c);
content = new Label(0, col, pd.getName());
sheet.addCell(content);
content= new Label(1, col, pd.getSn());
sheet.addCell(content);
content= new Label(2, col, pd.getMnemonicCode());
sheet.addCell(content);
content= new Label(3, col, pd.getStyle());
sheet.addCell(content);
content= new Label(4, col, pd.getType());
sheet.addCell(content);
content= new Label(5, col, pd.getCostPrice());
sheet.addCell(content);
content= new Label(6, col, pd.getPack());
sheet.addCell(content);
jxl.write.Number number8 = new jxl.write.Number(7, col, pd.getSize());
sheet.addCell(number8);
content= new Label(8, col, pd.getUnit());
sheet.addCell(content);
content= new Label(9, col, pd.getApprovalNumber());
sheet.addCell(content);
col++;
}
wb.write();
wb.close();
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (baos!= null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
前台:
<%@ page language="java" import="java.io.*,com.ly.jxcm.web.JxlExport" pageEncoding="utf-8"%>//引入java类
<%
String sub =request.getMethod();
if(sub.equals("POST")){
JxlExport ts = new JxlExport();//实例化类
InputStream iso = ts.jxl2Excel(); //调用方法获得输入流
String filename = "stuff.xls"; //设置文件名
response.setHeader("Content-disposition", "attachment; filename="+new String(filename.getBytes("utf-8"),"iso8859-1"));
int b;
while ((b = iso.read()) != -1 ) {
response.getOutputStream().write(b);
}
response.getOutputStream().flush();
if(iso!=null)
iso.close();
}
%>
<html>
<head>
<title> </title>
</head>
<script>
//此处定义了
function exportxls(){
$("stuffList").action="fowardBranch.action";
$("stuffList").method="post";//注意method必须是post
$("stuffList").submit();
$("stuffList").method="get";
}
</script>
</head>
<body>
<form>
<table>
<tr style="cursor: pointer;" >
<td class="STYLE1"><div align="center"></div></td>
<td>
<input type="button" name="sub" value="" οnclick="exportxls();" style="background-image:url(images/icon31.gif);border:0px;height:26px;width:101px;margin-right:4px;"/>
</td>
</tr>
</table>
</form>
</body>
</html>