public void exportExcel() {
saleListInfor = new ArrayList<>();
Map params = new HashMap();
params.put("startDate", startTime.value());
params.put("endDate", endTime.value());
JSONObject singleResult = WebUtils.doHttpRequest(APIs.STATISTICS_SINGLEGOODS_LIST, params);
// Platform.runLater(() -> {
if (singleResult.getBoolean("status")) {
JSONObject data1 = singleResult.getJSONObject("data");
JSONArray singleList = data1.getJSONArray("result");
ArrayList<SaleList> saleLists = new Gson().fromJson(singleList.toString(), new TypeToken<List<SaleList>>() {
}.getType());
saleListInfor = saleLists;
// Loading.staticClose();
} else {
// Loading.staticClose();
AlertUtils.alert("请求失败", singleResult.getString("msg"), Alert.AlertType.ERROR);
return;
}
// Platform.runLater(() -> Loading.staticClose());
// });
//创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = workbook.createSheet("单品销售统计表");
// 设置单元格属性
sheet.setDefaultColumnWidth(30);
sheet.setDefaultRowHeightInPoints(20);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//左右居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
style.setWrapText(true);//换行
style.setBorderBottom(BorderStyle.THIN);//下边框
style.setBorderLeft(BorderStyle.THIN);//左边框
style.setBorderRight(BorderStyle.THIN);//右边框
style.setBorderTop(BorderStyle.THIN);//上边框
//列名
String[] titles = new String[]{"商品编号", "品名", "单品数量", "单品金额", "总数量", "套餐内商品数量", "店面位置"};
// 对应数据
String[] methods = new String[]{"Code", "GoodsName", "SgoodsCount", "Price", "GoodsCount", "ComboCount", "Position"};
// 列名行
HSSFRow titleRow = sheet.createRow(0);
HSSFCell titleCell;
for (int i = 0; i < titles.length; i++) {
titleCell = titleRow.createCell(i);
titleCell.setCellValue(titles[i]);
titleCell.setCellStyle(style);
}
// 商品行
HSSFRow contentRow;
HSSFCell contentCell;
SaleList saleData;
for (int i = 0; i < saleListInfor.size(); i++) {
contentRow = sheet.createRow(i + 1);
saleData = saleListInfor.get(i);
Object saleValue = null;
int j;
for (j = 0; j < methods.length; j++) {
try {
// 通过反射获取实体属性
saleValue = SaleList.class.getMethod("get" + methods[j]).invoke(saleData);
} catch (Exception e) {
e.printStackTrace();
}
contentCell = contentRow.createCell(j);
contentCell.setCellValue(null == saleValue ? "" : saleValue.toString());
contentCell.setCellStyle(style);
}
}
// 商品名称自适应
sheet.autoSizeColumn(1);
Platform.runLater(() -> {
Loading.staticClose();
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialFileName("单品销售统计表" + System.currentTimeMillis() + ".xls");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel2003 files (*.xls)", "*.xls");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(stage);
if (file == null)
return;
if (file.exists()) {//文件已存在,则删除覆盖文件
file.delete();
}
String exportFilePath = file.getAbsolutePath();
try {
ExcelUtils.exportExcel(workbook, exportFilePath);
AlertUtils.alert("保存成功", "单品销售统计表导出成功", Alert.AlertType.INFORMATION);
} catch (Exception e) {
AlertUtils.alert("保存失败", e.getMessage(), Alert.AlertType.ERROR);
}
});
}