xml文档生成Excel 将其下载到本地
来源:互联网 发布:乐视网络高清机顶盒 编辑:程序博客网 时间:2024/05/22 14:39
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pl.dcloud.framework.DownLoadUtil;
import com.pl.dcloud.service.ResourceTypeService;
import com.pl.dcloud.xmlParse.JSONToExcel2;
import com.pl.dcloud.xmlParse.XmltoExcelUtil;
/**
*
* @author Created by dx-ycw on 2017/5/18
*
*/
@Controller
@RequestMapping(value="/download",produces="text/plain;charset=UTF-8")
public class DownloadController extends ControllerImpl{
@Resource
private ResourceTypeService resourceTypeService;
@ResponseBody
@RequestMapping(value="/jsonBOM",produces="text/plain;charset=UTF-8")
public String downLoadJSONToExcel2(HttpServletRequest request,
HttpServletResponse response) throws Exception{
String path = "E:\\test\\20170605";
String fileName = "data.xls";
String filePath = path + "\\" + fileName;
JSONToExcel2 jsonExcel = new JSONToExcel2();
String josnStr = jsonExcel.readJSONData("E:\\test\\20170605\\3.json");
List<Map> list = jsonExcel.getExcelMapData(josnStr,resourceTypeService.getAllList());
jsonExcel.writeToExcel(list,filePath);
//下载
DownLoadUtil.downLoadFile(filePath, response, fileName, "xls");
return "下载成功"+filePath;
}
}
package com.pl.dcloud.framework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;
public class DownLoadUtil {
public static boolean downLoadFile(String filePath, HttpServletResponse response, String fileName, String fileType) throws Exception {
File file = new File(filePath); //根据文件路径获得File文件
//设置文件类型(这样设置就不止是下Excel文件了,可以是多个文件类型)
if("pdf".equals(fileType)){
response.setContentType("application/pdf;charset=GBK");
}else if("xls".equals(fileType)){
response.setContentType("application/msexcel;charset=GBK");
}else if("doc".equals(fileType)){
response.setContentType("application/msword;charset=GBK");
}
//文件名
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes(), "ISO8859-1") + "\"");
response.setContentLength((int) file.length());
byte[] buffer = new byte[4096];// 缓冲区
BufferedOutputStream output = null;
BufferedInputStream input = null;
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = -1;
//遍历,开始下载
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
output.flush(); //不可少
response.flushBuffer();//不可少
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流,不可少
if (input != null)
input.close();
if (output != null)
output.close();
}
return false;
}
}
- xml文档生成Excel 将其下载到本地
- 将List下载到本地保存为Excel
- java 中 excel生成并文件下载保存到本地
- ASP.NET 将XML文件下载到本地
- 将本地文档上传至SAP服务器上及从SAP服务器下载到本地
- 将GridView中的数据保存到excel并下载到本地
- dom4j 生成xml 并将其格式化
- 将本地视频下载到本地相册
- SAP:如何将表中的数据下载到本地excel中
- 一个将web中datatable下载到本地excel的函数
- SAP 如何将SMW0 上的EXCEL模板下载到本地
- 使用jquery插件 jquery.table2excel.js 将页面中的表格下载到本地(excel格式)
- java 中 excel生成并文件下载保存到本地(三)
- 将本地文档库提交到Github
- 网络编程---(数据请求+slider)将网络上的大文件下载到本地,并打印其进度
- poi本地生成Excel到桌面
- poi本地生成Excel到桌面
- 使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上
- 分辨率的理解
- bootstrap点击表头实现排序功能
- 【css】li标签竖排轮方图片没有空余图片显示有缝隙的处理
- android 蓝牙通信编程
- 剑指offer题解C++【8】跳台阶
- xml文档生成Excel 将其下载到本地
- /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题
- Java中遍历HashMap
- iOS异常捕获
- 静态初始化块,初始化块,构造函数,静态方法
- 如何建立地球上任何一个区域的地形3d模型,并添加卫星或地貌贴图
- JVM高级特性与实践(十):虚拟机字节码执行引擎(栈帧结构)
- java中静态初始化块,实例初始化块,构造函数区别
- Qt学习小记(二)