根据Excel表格模板,填充数据后再导出。

1、导入依赖。

 <!--导出xlms表格-->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>

2、导入表格模板到class目录下。

  

 

3、编写Controller类

 @ResponseBody
 @RequestMapping(value = "/reportInspectionExpert", method = { RequestMethod.POST, RequestMethod.GET })
  public void reportInspectionExpert(HttpServletResponse response, String reportId) throws IOException {
      this.reportService.reportInspectionExpert(response,reportId);
  }

4、编写service层

 public void reportInspectionExpert(HttpServletResponse response, String reportIdString) throws IOException {
        Long reportId=Long.parseLong(reportIdString);

        ReportModel reportModel=this.mapper.selectById(reportId);
        if (null == reportModel){
            APIResult.error("报告id不存在,打印出错");
        }
        //检测环境
        ReportEnvironmentListVo reportEnvironmentListVo=environmentService.selectVoByReportId(reportId);
        if (null == reportEnvironmentListVo){
            APIResult.error("根据报告id查询,检测环境对象不存在,打印出错");
        }

        //车辆信息
        Long carId=reportModel.getCarId();
        CarListVo carListVo=this.carService.selectModelByCarId(carId);
        if (null == carListVo){
            APIResult.error("没有查询到车辆信息");
        }

        Map<String,Object> map=getMapParams(reportModel,reportEnvironmentListVo,carListVo);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        String sheetName = "导出表格";

        map.put("sysAreasName", carListVo.getSysAreasName()); // 所在地区
        map.put("enterpriseAddress", carListVo.getEnterpriseAddress()); // 地址
        map.put("contactName", carListVo.getContactName()); // 联系人
        map.put("contactMobile", carListVo.getContactMobile()); // 电话

        //污染装置
        ReportDataDeviceListVo 
                    dataDeviceListVo=this.dataDeviceService.selectVoByReportId(reportId);
        if (null == dataDeviceListVo){
            APIResult.error("没有查询污染控制装置核查");
        }
        map.put("mode1Way",dataDeviceListVo.getMode1WayString());
        map.put("mode1WayDetail",dataDeviceListVo.getMode1WayDetail());
        map.put("product1Way",dataDeviceListVo.getProduct1WayString());
        map.put("product1WayDetail",dataDeviceListVo.getProduct1WayDetail());

        map.put("mode2Way",dataDeviceListVo.getMode2WayString());
        map.put("mode2WayDetail",dataDeviceListVo.getMode2WayDetail());
        map.put("product2Way",dataDeviceListVo.getProduct2WayString());
        map.put("product2WayDetail",dataDeviceListVo.getProduct2WayDetail());

        map.put("mode3Way",dataDeviceListVo.getMode3WayString());
        map.put("mode3WayDetail",dataDeviceListVo.getMode3WayDetail());
        map.put("product3Way",dataDeviceListVo.getProduct3WayString());
        map.put("product3WayDetail",dataDeviceListVo.getProduct3WayDetail());

        map.put("mode4Way",dataDeviceListVo.getMode4WayString());
        map.put("mode4WayDetail",dataDeviceListVo.getMode4WayDetail());
        map.put("product4Way",dataDeviceListVo.getProduct4WayString());
        map.put("product4WayDetail",dataDeviceListVo.getProduct4WayDetail());

        map.put("mode5Way",dataDeviceListVo.getMode5WayString());
        map.put("mode5WayDetail",dataDeviceListVo.getMode5WayDetail());
        map.put("product5Way",dataDeviceListVo.getProduct5WayString());
        map.put("product5WayDetail",dataDeviceListVo.getProduct5WayDetail());

        map.put("mode6Way",dataDeviceListVo.getMode6WayString());
        map.put("mode6WayDetail",dataDeviceListVo.getMode6WayDetail());
        map.put("product6Way",dataDeviceListVo.getProduct6WayString());
        map.put("product6WayDetail",dataDeviceListVo.getProduct6WayDetail());

        map.put("mode7Way",dataDeviceListVo.getMode7WayString());
        map.put("mode7WayDetail",dataDeviceListVo.getMode7WayDetail());
        map.put("product7Way",dataDeviceListVo.getProduct7WayString());
        map.put("product7WayDetail",dataDeviceListVo.getProduct7WayDetail());


        map.put("mode8Way",dataDeviceListVo.getMode8WayString());
        map.put("mode8WayDetail",dataDeviceListVo.getMode8WayDetail());
        map.put("product8Way",dataDeviceListVo.getProduct8WayString());
        map.put("product8WayDetail",dataDeviceListVo.getProduct8WayDetail());


        map.put("mode9Way",dataDeviceListVo.getMode9WayString());
        map.put("mode9WayDetail",dataDeviceListVo.getMode9WayDetail());
        map.put("product9Way",dataDeviceListVo.getProduct9WayString());
        map.put("product9WayDetail",dataDeviceListVo.getProduct9WayDetail());

        map.put("mode10Way",dataDeviceListVo.getMode10WayString());
        map.put("mode10WayDetail",dataDeviceListVo.getMode10WayDetail());
        map.put("product10Way",dataDeviceListVo.getProduct10WayString());
        map.put("product10WayDetail",dataDeviceListVo.getProduct10WayDetail());

        map.put("deviceTestResult",dataDeviceListVo.getTestResultString());


        CarQueryDto dto=new CarQueryDto();
        dto.setReportId(reportId);
        List<CarInspectDetailListVo> ret = this.carService.getReportInfo(dto);
        for (CarInspectDetailListVo vo : ret){
            map.put("communication",vo.getCommunication());//通讯
            map.put("faultCode",vo.getFaultCode());//报警及故障码
            map.put("readyStatus",vo.getReadyStatus());//就绪未完成项目
            map.put("ureaMeasured",vo.getUreaMeasured());//车用尿素
            map.put("oilMeasured",vo.getOilMeasured());//车用柴油
            map.put("average",vo.getAverage());//光吸收系数
            map.put("maxValues",vo.getMaxValues());//林格曼黑度
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            String reportInspectDate = sdf.format(vo.getReportInspectDate());
            map.put("reportInspectDate",reportInspectDate);//检测日期
            map.put("technicianName",vo.getTechnicianName());//主检人
        }
        list.add(map);
          /**
            *list 存放需要填充的数据
            *sheetName  导出的表格名称
            */
        JxlUtils.excelUtilsFactoryInspectionResultAppear(response, list, sheetName);
    }

 4.1  JxlUtils帮助类

package common.util;

import common.conf.ReportConfig;
import jxl.Workbook;
import jxl.write.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

public class JxlUtils {
	
	public static void excelUtilsFactoryInspectionResultAppear(HttpServletResponse response,List<Map<String, Object>> list, String sheetName) throws IOException {
		try {
			String realpath = ReportConfig.JOB_FILE + "template.xls";
			// 第一步:选择模板文件:
			Workbook wb = Workbook.getWorkbook(new File(realpath));
			// 第二步:通过模板得到一个可写的Workbook:
			String filePath = ReportConfig.FILE_FILTER_URLS + sheetName + ".xls";
			File targetFile = new File(filePath);
			WritableWorkbook wwb = Workbook.createWorkbook(targetFile, wb);
			// 这是获取工作薄,然后进行修改(这句是重点)
			WritableSheet wws = wwb.getSheet(0);
			wws.setName(sheetName);// 设置标签页标题
			WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 12);
			WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
			jxl.write.WritableCellFormat wcf = new         jxl.write.WritableCellFormat(cellFormat1);
            /*
            *填充表格数据
            *new Label(1, i + 2, sysAreasName, wcf)
            *第一个参数:所属行;第二个参数:所属列;(它们都是0开始的)
            *第三个参数:需要填充的数据;第四个参数:字体格式;
            */
			for (int i = 0; i < list.size(); i++) {
				String sysAreasName = (String) list.get(i).get("sysAreasName");
				wws.addCell(new Label(1, i + 2, sysAreasName, wcf));

				String enterpriseName = (String) list.get(i).get("enterpriseName");
				wws.addCell(new Label(2, i + 2, enterpriseName, wcf));

				String enterpriseAddress = (String) list.get(i).get("enterpriseAddress");
				wws.addCell(new Label(3, i + 2, enterpriseAddress, wcf));

				String contactName = (String) list.get(i).get("contactName");
				wws.addCell(new Label(4, i + 2, contactName, wcf));

				String contactMobile = (String) list.get(i).get("contactMobile");
				wws.addCell(new Label(5, i + 2, contactMobile, wcf));

				String carNumber = (String) list.get(i).get("carNumber");
				wws.addCell(new Label(6, i + 2, carNumber, wcf));

				String vehicleModel = (String) list.get(i).get("vehicleModel");
				wws.addCell(new Label(7, i + 2, vehicleModel, wcf));

				String carCode = (String) list.get(i).get("carCode");
				wws.addCell(new Label(8, i + 2, carCode, wcf));

				String quality = (String) list.get(i).get("quality");
				wws.addCell(new Label(9, i + 2, quality, wcf));

				String maximumMass = (String) list.get(i).get("maximumMass");
				wws.addCell(new Label(10, i + 2, maximumMass, wcf));

				String engineModel = (String) list.get(i).get("engineModel");
				wws.addCell(new Label(11, i + 2, engineModel, wcf));

				String engineNumber = (String) list.get(i).get("engineNumber");
				wws.addCell(new Label(12, i + 2, engineNumber, wcf));

				String engineDisplacement = (String) list.get(i).get("engineDisplacement");
				wws.addCell(new Label(13, i + 2, engineDisplacement, wcf));

				String ratedSpeed = (String) list.get(i).get("ratedSpeed");
				wws.addCell(new Label(14, i + 2, ratedSpeed, wcf));

				String enginePower = (String) list.get(i).get("enginePower");
				wws.addCell(new Label(15, i + 2, enginePower, wcf));

				String dpf = (String) list.get(i).get("dpf");
				wws.addCell(new Label(16, i + 2, dpf, wcf));

				String dpfModel = (String) list.get(i).get("dpfModel");
				wws.addCell(new Label(17, i + 2, dpfModel, wcf));

				String scr = (String) list.get(i).get("scr");
				wws.addCell(new Label(18, i + 2, scr, wcf));

				String scrModel = (String) list.get(i).get("scrModel");
				wws.addCell(new Label(19, i + 2, scrModel, wcf));

				String cylinders = (String) list.get(i).get("cylinders");
				wws.addCell(new Label(20, i + 2, cylinders, wcf));

				String motorModel = (String) list.get(i).get("motorModel");
				wws.addCell(new Label(21, i + 2, motorModel, wcf));

				String deviceModel = (String) list.get(i).get("deviceModel");
				wws.addCell(new Label(22, i + 2, deviceModel, wcf));

				String battery = (String) list.get(i).get("battery");
				wws.addCell(new Label(23, i + 2, battery, wcf));

				String vehicleManufacturer = (String) list.get(i).get("vehicleManufacturer");
				wws.addCell(new Label(24, i + 2, vehicleManufacturer, wcf));


				String vehicleFactoryDate = (String) list.get(i).get("vehicleFactoryDate");
				wws.addCell(new Label(25, i + 2, vehicleFactoryDate, wcf));

				String mileage = (String) list.get(i).get("mileage");
				wws.addCell(new Label(26, i + 2, mileage, wcf));

				wws.addCell(new Label(27, i + 2, enterpriseName, wcf));


				String phone = (String) list.get(i).get("phone");
				wws.addCell(new Label(28, i + 2, phone, wcf));

				String color = (String) list.get(i).get("color");
				wws.addCell(new Label(29, i + 2, color, wcf));

				String fuelType = (String) list.get(i).get("fuelType");
				wws.addCell(new Label(30, i + 2, fuelType, wcf));


				String fuelModel = (String) list.get(i).get("fuelModel");
				wws.addCell(new Label(31, i + 2, fuelModel, wcf));

				String driveWay = (String) list.get(i).get("driveWay");
				wws.addCell(new Label(32, i + 2, driveWay, wcf));

				String model = (String) list.get(i).get("model");
				wws.addCell(new Label(33, i + 2, model, wcf));


				String transmissionType = (String) list.get(i).get("transmissionType");
				wws.addCell(new Label(34, i + 2, transmissionType, wcf));

				String natureUse = (String) list.get(i).get("natureUse");
				wws.addCell(new Label(35, i + 2, natureUse, wcf));

				String registerDate = (String) list.get(i).get("registerDate");
				wws.addCell(new Label(36, i + 2, registerDate, wcf));


				String mode1Way = (String) list.get(i).get("mode1Way");
				String mode1WayDetail = (String) list.get(i).get("mode1WayDetail");
				wws.addCell(new Label(38, i + 2, mode1Way+mode1WayDetail, wcf));

				String product1Way = (String) list.get(i).get("product1Way");
				String product1WayDetail = (String) list.get(i).get("product1WayDetail");
				wws.addCell(new Label(38, i + 3, product1Way+product1WayDetail, wcf));



				String mode2Way = (String) list.get(i).get("mode2Way");
				String mode2WayDetail = (String) list.get(i).get("mode2WayDetail");
				wws.addCell(new Label(40, i + 2, mode2Way+mode2WayDetail, wcf));

				String product2Way = (String) list.get(i).get("product2Way");
				String product2WayDetail = (String) list.get(i).get("product2WayDetail");
				wws.addCell(new Label(40, i + 3, product2Way+product2WayDetail, wcf));

				String mode3Way = (String) list.get(i).get("mode3Way");
				String mode3WayDetail = (String) list.get(i).get("mode3WayDetail");
				wws.addCell(new Label(42, i + 2, mode3Way+mode3WayDetail, wcf));

				String product3Way = (String) list.get(i).get("product3Way");
				String product3WayDetail = (String) list.get(i).get("product3WayDetail");
				wws.addCell(new Label(42, i + 3, product3Way+product3WayDetail, wcf));


				String mode4Way = (String) list.get(i).get("mode4Way");
				String mode4WayDetail = (String) list.get(i).get("mode4WayDetail");
				wws.addCell(new Label(44, i + 2, mode4Way+mode4WayDetail, wcf));

				String product4Way = (String) list.get(i).get("product4Way");
				String product4WayDetail = (String) list.get(i).get("product4WayDetail");
				wws.addCell(new Label(44, i + 3, product4Way+product4WayDetail, wcf));

				String mode5Way = (String) list.get(i).get("mode5Way");
				String mode5WayDetail = (String) list.get(i).get("mode5WayDetail");
				wws.addCell(new Label(46, i + 2, mode5Way+mode5WayDetail, wcf));

				String product5Way = (String) list.get(i).get("product5Way");
				String product5WayDetail = (String) list.get(i).get("product5WayDetail");
				wws.addCell(new Label(46, i + 3, product5Way+product5WayDetail, wcf));

				String mode6Way = (String) list.get(i).get("mode6Way");
				String mode6WayDetail = (String) list.get(i).get("mode6WayDetail");
				wws.addCell(new Label(48, i + 2, mode6Way+mode6WayDetail, wcf));

				String product6Way = (String) list.get(i).get("product6Way");
				String product6WayDetail = (String) list.get(i).get("product6WayDetail");
				wws.addCell(new Label(48, i + 3, product6Way+product6WayDetail, wcf));

				String mode7Way = (String) list.get(i).get("mode7Way");
				String mode7WayDetail = (String) list.get(i).get("mode7WayDetail");
				wws.addCell(new Label(50, i + 2, mode7Way+mode7WayDetail, wcf));

				String product7Way = (String) list.get(i).get("product7Way");
				String product7WayDetail = (String) list.get(i).get("product7WayDetail");
				wws.addCell(new Label(50, i + 3, product7Way+product7WayDetail, wcf));

				String mode8Way = (String) list.get(i).get("mode8Way");
				String mode8WayDetail = (String) list.get(i).get("mode8WayDetail");
				wws.addCell(new Label(52, i + 2, mode8Way+mode8WayDetail, wcf));

				String product8Way = (String) list.get(i).get("product8Way");
				String product8WayDetail = (String) list.get(i).get("product8WayDetail");
				wws.addCell(new Label(52, i + 3, product8Way+product8WayDetail, wcf));

				String mode9Way = (String) list.get(i).get("mode9Way");
				String mode9WayDetail = (String) list.get(i).get("mode9WayDetail");
				wws.addCell(new Label(54, i + 2, mode9Way+mode9WayDetail, wcf));

				String product9Way = (String) list.get(i).get("product9Way");
				String product9WayDetail = (String) list.get(i).get("product9WayDetail");
				wws.addCell(new Label(54, i + 3, product9Way+product9WayDetail, wcf));

				String mode10Way = (String) list.get(i).get("mode10Way");
				String mode10WayDetail = (String) list.get(i).get("mode10WayDetail");
				wws.addCell(new Label(56, i + 2, mode10Way+mode10WayDetail, wcf));

				String product10Way = (String) list.get(i).get("product10Way");
				String product10WayDetail = (String) list.get(i).get("product10WayDetail");
				wws.addCell(new Label(56, i + 3, product10Way+product10WayDetail, wcf));

				String deviceTestResult = (String) list.get(i).get("deviceTestResult");
				wws.addCell(new Label(57, i + 2, deviceTestResult, wcf));

				String communication = (String) list.get(i).get("communication");
				wws.addCell(new Label(58, i + 2, communication, wcf));

				String faultCode = (String) list.get(i).get("faultCode");
				wws.addCell(new Label(59, i + 2, faultCode, wcf));

				String readyStatus = (String) list.get(i).get("readyStatus");
				wws.addCell(new Label(60, i + 2, readyStatus, wcf));

				String ureaMeasured = (String) list.get(i).get("ureaMeasured");
				wws.addCell(new Label(61, i + 2, ureaMeasured, wcf));

				String oilMeasured = (String) list.get(i).get("oilMeasured");
				wws.addCell(new Label(62, i + 2, oilMeasured, wcf));

				String average = (String) list.get(i).get("average");
				wws.addCell(new Label(63, i + 2, average, wcf));

				String maxValues = (String) list.get(i).get("maxValues");
				wws.addCell(new Label(64, i + 2, maxValues, wcf));

				String reportInspectDate = (String) list.get(i).get("reportInspectDate");
				wws.addCell(new Label(65, i + 2, reportInspectDate, wcf));

				String technicianName = (String) list.get(i).get("technicianName");
				wws.addCell(new Label(66, i + 2, technicianName, wcf));
			}
			wwb.write();
			wwb.close();
			wb.close();

			// 下载生成好的excel文件
			response.reset();
			response.setContentType("application/octet-stream");
			response.setCharacterEncoding("utf-8");
			response.setContentLength((int) targetFile.length());
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String(sheetName.getBytes("utf-8"), "ISO-8859-1") + ".xls");
			byte[] buff = new byte[1024];
			BufferedInputStream bis = null;
			OutputStream os = null;
			try {
				os = response.getOutputStream();
				bis = new BufferedInputStream(new FileInputStream(targetFile));
				int i = 0;
				while ((i = bis.read(buff)) != -1) {
					os.write(buff, 0, i);
					os.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



}

5、测试结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用Apache POI库来根据已有的Excel模板导出数据。Apache POI是一个用于读写Microsoft Office格式文件的Java库,包括Excel、Word和PowerPoint等文件格式。 下面是一种基本的导出数据的方法: 1. 首先,你需要在项目中引入Apache POI的相关依赖。可以在Maven或Gradle配置文件中添加以下依赖: ``` <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Excel模板文件,可以使用Microsoft Excel或其他电子表格软件创建,并在模板中定义好表头和样式。 3. 在Java代码中,使用Apache POI来读取模板文件,并根据需要填充数据。以下是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class ExcelExporter { public static void main(String[] args) { try { // 读取模板文件 InputStream templateFile = new FileInputStream("template.xlsx"); Workbook workbook = new XSSFWorkbook(templateFile); Sheet sheet = workbook.getSheetAt(0); // 填充数据 Row dataRow = sheet.createRow(1); Cell cell = dataRow.createCell(0); cell.setCellValue("John Doe"); // 保存导出文件 OutputStream outputFile = new FileOutputStream("output.xlsx"); workbook.write(outputFile); // 关闭资源 workbook.close(); templateFile.close(); outputFile.close(); System.out.println("数据导出成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述示例中,我们首先通过`FileInputStream`读取模板文件,然后使用`XSSFWorkbook`创建一个工作簿对象。接下来,我们通过`getSheetAt(0)`获取第一个工作表,并使用`createRow()`和`createCell()`方法创建行和单元格对象。最后,使用`setCellValue()`方法填充数据,并通过`FileOutputStream`将工作簿对象写入到输出文件中。 请注意,上述示例仅为基本示例,实际应用中可能需要根据具体需求进行更复杂的操作,例如循环填充数据、设置样式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值