上面代码获得输出流os 生成Excel文件 把输出流传给book。这样页面点完生成Excel文件按钮之后就会直接提示用户另存为/下载Excel文件,
第四步:
for (int i =0;i
Label labelorderNumberTitle = new Label(0, 1, "移库单号",format);
sheet.addCell(labelorderNumberTitle);
Label labelPalletNameTitle = new Label(1, 1, "管理号",format);
sheet.addCell(labelPalletNameTitle);
Label labelMaterialNumberTitle = new Label(2, 1, "物料号",format);
sheet.addCell(labelMaterialNumberTitle);
Label labelMaterialDescTitle = new Label(3, 1, "物料描述",format);
sheet.addCell(labelMaterialDescTitle);
Label labelQtyTitle = new Label(4, 1, "移库数量",format);
sheet.addCell(labelQtyTitle);
Label labelBatchNumberTitle = new Label(5, 1, "批次号",format);
sheet.addCell(labelBatchNumberTitle);
Label labelAreaNameTitle = new Label(6, 1, "原库区",format);
sheet.addCell(labelAreaNameTitle);
Label labelPosNameTitle = new Label(7, 1, "原库位",format);
sheet.addCell(labelPosNameTitle);
Label labelStockAreaNameTitle = new Label(8, 1, "目标库区",format);
sheet.addCell(labelStockAreaNameTitle);
Label labelStockPosNameTitle = new Label(9, 1, "目标库位",format);
sheet.addCell(labelStockPosNameTitle);
Label labelNumberTitle = new Label(10, 1, "参考号",format);
sheet.addCell(labelNumberTitle);
Map map = list.get(i);
Label labelOrderNumber = new Label(0, i+2, map.get("orderNumber"));
Label labelPalletName = new Label(1, i+2, map.get("palletName"));
Label materialNumber = new Label(2, i+2, map.get("materialNumber"));
Label materialDesc = new Label(3, i+2, map.get("materialDesc"));
Label qty = new Label(4, i+2, map.get("qty"));
Label batchNumber = new Label(5, i+2, map.get("batchNumber"));
Label areaName = new Label(6, i+2, map.get("areaName"));
Label posName = new Label(7, i+2, map.get("posName"));
Label stockAreaName = new Label(8, i+2, map.get("stockAreaName"));
Label stockPosName = new Label(9, i+2, map.get("stockPosName"));
Label number = new Label(10, i+2, map.get("number"));
sheet.addCell(labelOrderNumber);
sheet.addCell(labelPalletName);
sheet.addCell(materialNumber);
sheet.addCell(materialDesc);
sheet.addCell(qty);
sheet.addCell(batchNumber);
sheet.addCell(areaName);
sheet.addCell(posName);
sheet.addCell(stockAreaName);
sheet.addCell(stockPosName);
sheet.addCell(number);
}
book.write();
book.close();
os.flush();
os.close();
上面代码循环遍历List集合里面的数据。并将数据放到单元格中。在把定义好的单元格添加到工作表中。写入数据并关闭文件。清空输出流并关闭输出流。这样一个Excel文件就生成了。