http file server模板_项目当中有一个批量流量查询模板,提供一个下载功能?

项目当中有一个批量流量查询模板,提供一个下载功能?

实现:以下不借助oss实现。用oss直接放个下载连接就完事儿了

效果?

4824f20bcdd1d6b022bd197c4e2f971d.png

WordUtils代码:

package com.reachauto.vsp.portal.util;

import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * 下载工具类
 */

public class WordUtils { 

	private WordUtils() {
		
	}
	
	public static void exportFile(HttpServletResponse response, File file, String name) throws IOException {
		InputStream stream = null;
		try {
			stream = new BufferedInputStream(new FileInputStream(file));
			response.setContentType("application/vnd.ms-excel");

			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8") + ".xlsx");
			copy(stream, response.getOutputStream());
		} finally {
			if (stream != null) {
				stream.close();
			}
		}
	}
	
	public static void exportZipFile(HttpServletResponse response, String filePath) throws IOException {
		InputStream stream = null;
		File file = new File(filePath);
		try {
			stream = new BufferedInputStream(new FileInputStream(file));
			response.setContentType("application/zip");
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
			copy(stream, response.getOutputStream());
		} finally {
			IOUtils.closeQuietly(stream);
        	if(file.exists()){
        		file.delete();
        	}
		}
	}

	private static int copy(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[10240];
		int count = 0;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}
}  

Controller代码:

    /**
     * 模板
     * @param request
     * @param response
     */
    @RequestMapping(value = "/downloadExcel")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        try {
//            server.url=/opt/exportTemplates/
//            server.url.window=D:Program Filespicture
            //本地路径上线调整服务器路径
            String templateFilePath = "C:UsersAdministratorDesktopvspnodeworkspacevspcloud-vsptrunk-portalsrcmainresourcesexportTemplates批量流量查询模板.xlsx";
//            File file = new File(request.getSession().getServletContext()
//                    .getRealPath(templateFilePath));
            File file = new File(templateFilePath);
            WordUtils.exportFile(response, file, "批量流量查询模板");
        } catch (Exception e) {
            log.error("/downloadExcel/export", e);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Java代码示例,可以使用Apache POI库来根据Excel模板在同一个Sheet表中批量生成标签。该示例假定您已经有一个Excel模板文件,并且模板文件中有一个名为“标签”的Sheet表。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelLabelGenerator { public static void main(String[] args) throws IOException { // 读取Excel模板文件 FileInputStream templateFile = new FileInputStream("template.xlsx"); Workbook workbook = new XSSFWorkbook(templateFile); Sheet sheet = workbook.getSheet("标签"); // 定义模板中需要替换的变量 Map<String, String> variables = new HashMap<>(); variables.put("{姓名}", "张三"); variables.put("{年龄}", "25"); variables.put("{地址}", "北京市海淀区"); // 按照变量生成标签 int rowIndex = 2; // 从第3行开始生成标签,第1行为标题行,第2行为样式行 for (int i = 0; i < 10; i++) { // 生成10个标签 Row row = sheet.getRow(rowIndex); for (Cell cell : row) { String value = cell.getStringCellValue(); for (Map.Entry<String, String> entry : variables.entrySet()) { value = value.replace(entry.getKey(), entry.getValue()); } cell.setCellValue(value); } rowIndex++; } // 保存生成的Excel文件 FileOutputStream outputFile = new FileOutputStream("output.xlsx"); workbook.write(outputFile); // 关闭文件流 templateFile.close(); outputFile.close(); workbook.close(); } } ``` 在上面的示例中,我们首先读取Excel模板文件,并获取其中名为“标签”的Sheet表。然后,我们定义了一个包含需要替换的变量的Map对象。接下来,我们使用循环在同一个Sheet表中生成标签。对于每个标签,我们从模板文件中读取第3行及以后的行,并将其中的变量替换为实际的值。最后,我们将生成的Excel文件保存到磁盘上,并关闭所有文件流。 请注意,上述示例仅提供了基本的思路和代码示例,您需要根据实际需求进行修改和调整。例如,您可能需要根据不同的数据源生成不同的标签,或者需要自定义标签的样式和布局。在使用POI库时,您还需要注意处理可能发生的异常和错误,例如文件读写错误、Sheet表不存在、单元格格式错误等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值