public static void main(String[] args) throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); // 创建第一个sheet页 createFirstSheet(workbook); // 创建第二个sheet页 createSecondSheet(workbook); workbook.write(new FileOutputStream("E:/test/a.xls")); } private static void createFirstSheet(HSSFWorkbook workbook) { Sheet sheet = workbook.createSheet("waitdeliveryorders_01"); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 36)); for (int i = 0; i <= 36; i++) { sheet.setColumnWidth(i, 32 * 130); } // 设置EXCEL第一行(标题行) setTitle(sheet, workbook); // 设置EXCEL第二行(字段行) setColumn(sheet, workbook); } private static void createSecondSheet(HSSFWorkbook workbook) { Sheet sheet = workbook.createSheet("快递公司"); Row row = sheet.createRow(0); row.setHeight((short) 600); Cell cell = row.createCell(0); cell.setCellValue("承运单位"); cell.setCellStyle(getSecondSheetStyle(workbook)); sheet.setColumnWidth(0, 32 * 180); String[] expressCompanyNames = getExpressCompanyNames(); for (int i = 0; i < expressCompanyNames.length; i++) { Row expressRowi = sheet.createRow(i + 1); expressRowi.setHeight((short) 600); Cell expressCell0 = expressRowi.createCell(0); expressCell0.setCellValue(expressCompanyNames[i]); } } public static CellStyle getTitletyle(HSSFWorkbook workbook) { // 设置字体; HSSFFont font = workbook.createFont(); // 设置字体大小; font.setFontHeightInPoints((short) 14); // 设置字体名字; font.setFontName("SansSerif"); // 设置字体颜色 font.setColor(HSSFColor.RED.index); // 设置字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置样式; HSSFCellStyle style = workbook.createCellStyle(); // 在样式用应用设置的字体; style.setFont(font); // 设置自动换行; style.setWrapText(false); // 设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public static CellStyle getColumnStyle(HSSFWorkbook workbook, boolean isRed) { // 设置字体; HSSFFont font = workbook.createFont(); // 设置字体大小; font.setFontHeightInPoints((short) 10); // 设置字体名字; font.setFontName("SansSerif"); if (isRed) { // 设置字体颜色 font.setColor(HSSFColor.RED.index); } // 设置字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置样式; CellStyle style = workbook.createCellStyle(); // 在样式用应用设置的字体; style.setFont(font); // 设置自动换行; style.setWrapText(false); // 设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public static CellStyle getSecondSheetStyle(HSSFWorkbook workbook) { // 设置字体; HSSFFont font = workbook.createFont(); // 设置字体大小; font.setFontHeightInPoints((short) 14); // 设置字体名字; font.setFontName("SansSerif"); // 设置字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置样式; HSSFCellStyle style = workbook.createCellStyle(); // 在样式用应用设置的字体; style.setFont(font); // 设置自动换行; style.setWrapText(false); // 设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.VERTICAL_CENTER); // 设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public static String[] getExpressCompanyNames() { String[] expressCompanyNames = { "苏宁自配送", "AAE-中国", "安信达快递", "BHT", "中国邮政国内包裹/挂号信", "希伊艾斯(CCES)", "DHL", "D速快递", "大田物流", "德邦物流", "EMS", "E邮宝", "飞康达物流", "FedEx(国外)", "广东邮政物流", "港中能达物流", "汇通快运", "天地华宇", "佳吉快运", "佳怡物流", "京广速递", "急先达", "快捷速递", "龙邦物流", "联昊通快递查询", "联邦快递", "全晨快递", "全峰全球专递", "全日通快递", "顺丰速运", "三态速递", "TNT", "天天快递", "天地华宇", "UPS", "UC优速物流", "星晨急便", "鑫飞鸿速递", "新邦物流", "圆通速递", "韵达快运", "亚风快递", "源伟丰快递", "一邦快递", "远成物流", "元智快递", "运通快递", " 越丰快递", "中通快递", "中铁快运", "中邮物流", "宅急送", "百福东方", "共速达", "全一快递", " 申通快递", "速尔快递", "中铁物流 " }; return expressCompanyNames; } private static void setTitle(Sheet sheet, HSSFWorkbook workbook) { String title = "在此模板中,输入快递单号、快递公司名称(须和“快递公司”表中的名称保持高度一致),无需物流输入送货人和手机号,然后点击“导入发货订单”按钮,可进行批量发货操作。"; Row titleRow = sheet.createRow(0); titleRow.setHeight((short) 600); Cell titleCell = titleRow.createCell(0); titleCell.setCellValue(title); titleCell.setCellStyle(getTitletyle(workbook)); } private static void setColumn(Sheet sheet, HSSFWorkbook workbook) { String[] columnNames = { "订单编号", "快递公司名称", "快递单号 ", "送货人 ", "手机号 ", "订单创建时间 ", "订单来源", "订单类型 ", "买家账号 ", "苏宁商品编码 ", "商家商品编码 ", "商品名称 ", "商品型号", "商品属性1", "商品属性2", "购买数量 ", "单价", "运费 ", "总金额 ", "实际支付金额", "支付方式", "收货人姓名", "收货地址 ", "联系电话 ", "联系手机", "发票抬头 ", "订单备注 ", "商家编码", "店铺ID ", "店铺名称 ", "异常信息", "发货时间 ", "OMS行项目号 ", "B2C行项目号", "行项目状态", "运输区域字段编码 ", "顾客备注" }; Row columnrow = sheet.createRow(1); columnrow.setHeight((short) 480); for (int i = 0; i < columnNames.length; i++) { Cell cell = columnrow.createCell(i); cell.setCellValue(columnNames[i]); if (i >= 1 && i <= 4) { cell.setCellStyle(getColumnStyle(workbook, true)); } else { cell.setCellStyle(getColumnStyle(workbook, false)); } } }