数据库表 purchaseApplyItem:
导出代码:
public void dump2Excel() {
String filePathStr = "C:\\a\\b";
String fileNameStr = "purchaseApplyItems.xls";
File filePath = new File(filePathStr);
File file = new File(filePathStr + "\\" + fileNameStr);
WritableWorkbook wwb = null;
try {
if (!filePath.isDirectory()) {
filePath.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
} else {
System.out.println("该文件已存在,请先删除后再生成!");
return;
}
wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("采购申请单", 0);
ws.setRowView(0, 600, false); // 设置行高
ws.setColumnView(0, 20);// 设置列宽
// 从数据库获取所有的数据(根据自己的情况写对应的方法)
List<PurchaseApplyItem> purchaseApplyItems = getEntity(new PurchaseApplyItem());
// 设置字体
// true表示是否斜体
WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED);
// 设置格式
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setBackground(jxl.format.Colour.YELLOW);
wcf.setBorder(Border.BOTTOM, BorderLineStyle.THICK, Colour.RED);
wcf.setAlignment(Alignment.CENTRE);
wcf.setWrap(true);
Label labelId = new Label(0, 0, "编号", wcf);
Label labelItemName = new Label(1, 0, "物品名", wcf);
Label labelNumber = new Label(2, 0, "数量", wcf);
Label labelPurchaseId = new Label(3, 0, "采购单号", wcf);
wcf.setVerticalAlignment(VerticalAlignment.TOP);
// 添加表头
ws.addCell(labelId);
ws.addCell(labelItemName);
ws.addCell(labelNumber);
ws.addCell(labelPurchaseId);
// 添加表数据
for (int i = 0; i < purchaseApplyItems.size(); i++) {
Number labelId_i = new Number(0, i + 1, purchaseApplyItems.get(i).getId());
Label labelItemName_i = new Label(1, i + 1, purchaseApplyItems.get(i).getItemName());
Number labelNumber_i = new Number(2, i + 1, purchaseApplyItems.get(i).getNumber());
Label labelPurchaseId_i = new Label(3, i + 1, purchaseApplyItems.get(i).getPurchaseId());
ws.addCell(labelId_i);
ws.addCell(labelItemName_i);
ws.addCell(labelNumber_i);
ws.addCell(labelPurchaseId_i);
}
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
参看: poi将数据库表内容导出到excel
http://blog.youkuaiyun.com/chen270310978/article/details/52885410