下载模板java
信息写入文件并下载web
package com.njwb.controller;spring
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;apache
import javassist.expr.NewArray;app
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;xss
import org.apache.logging.log4j.core.config.plugins.ResolverUtil.NameEndsWith;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;测试
import com.njwb.entity.eopapp.EopApp;
import com.njwb.service.DeptService;spa
@Controller
public class DownController {
@Autowired
private DeptService deptService;
/*
* 下载模板
*/
@ResponseBody
@RequestMapping("/downModel")
public void downModel(HttpServletRequest req,HttpServletResponse resp){
try {
//获取要下载的模板名称(中文乱码须要再次解码)
String fileNames =URLEncoder.encode("测试模板.xls", "utf-8");
String fileName=java.net.URLDecoder.decode( fileNames, "ISO-8859-1");
//设置要下载的文件的名称
resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
//通知客服文件的MIME类型
resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
String f = DownController.class.getResource("importOrderTemplate.xls").getPath();
// String f = DownController.class.getClassLoader().getResource(“4.txt”).getPath();
File file=new File(f);
FileInputStream fileinput=new FileInputStream(file);
/* BufferedReader input = new BufferedReader(new InputStreamReader(fileinput));
OutputStream out = resp.getOutputStream();
// byte[] b = new byte[2048];
// int len;
String line;
while ( (line=input.readLine()) != null) {
out.write(line.getBytes("utf-8"));
}*/
FileInputStream input = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
//修正 Excel在xxx.xlsx中发现不可读取的内容。是否恢复此工做薄的内容?若是信任此工做簿的来源,请点击"是"
resp.setHeader("Content-Length", String.valueOf(fileinput.getChannel().size()));
input.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 输入信息到文件
*/
@ResponseBody
@RequestMapping("writeFile")
public String writeFile(HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException{
/* //获取要下载的模板名称
String fileName =URLEncoder.encode(("测试.xls"), "utf-8");
//设置要下载的文件的名称
resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
//通知客服文件的MIME类型
resp.setContentType("application/vnd.ms-excel;charset=UTF-8");*/
//一、建立工做簿
Workbook wb = new XSSFWorkbook();
//1.一、设置表格的格式----居中
CellStyle cs =wb.createCellStyle();
cs.setAlignment(CellStyle.ALIGN_CENTER);
//2.一、建立工做表
Sheet sheet = wb.createSheet("订单信息列表1");
sheet.setColumnWidth(0, 100 * 40);
sheet.setColumnWidth(1, 100 * 40);
sheet.setColumnWidth(2, 100 * 40);
sheet.setColumnWidth(3, 100 * 40);
sheet.setColumnWidth(4, 100 * 40);
//3.一、建立行----表头行
Row row = sheet.createRow(0);.net
//四、建立格
Cell cell = row.createCell(0);
cell.setCellValue("供应商名称");
cell.setCellStyle(cs);
cell = row.createCell(1);
cell.setCellValue("订单编号");
cell.setCellStyle(cs);
cell = row.createCell(2);
cell.setCellValue("下单时间");
cell.setCellStyle(cs);
cell = row.createCell(3);
cell.setCellValue("订单状态");
cell.setCellStyle(cs);
cell = row.createCell(4);
cell.setCellValue("订单来源");
cell.setCellStyle(cs);
List<EopApp> dopApps=deptService.queryAll();
for(int i=0;i<dopApps.size();i++){
//3.二、建立行----内容行
row=sheet.createRow(i+1);
//第几行第几格 第一行第一格为“code”
row.createCell(0).setCellValue(dopApps.get(i).getApp_Name());
row.createCell(1).setCellValue(dopApps.get(i).getAppId());
row.createCell(2).setCellValue(dopApps.get(i).getPath());
row.createCell(3).setCellValue(dopApps.get(i).getAuthor());
row.createCell(4).setCellValue(dopApps.get(i).getDescript());
}
//指定存储位置
try {
String f = DownController.class.getResource("Orders.xlsx").getPath();
System.out.println(f);
File file=new File(f);
FileOutputStream fout = new FileOutputStream(file);
resp.setHeader("Content-Length", String.valueOf(fout.getChannel().size()));
wb.write(fout);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
/**
*
*/
@ResponseBody
@RequestMapping("/downFile")
public void downFile(HttpServletRequest req,HttpServletResponse resp){
try {
//获取要下载的模板名称
String fileName =URLEncoder.encode(("orders.xls"), "utf-8");
//设置要下载的文件的名称
resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
//通知客服文件的MIME类型
resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
String f = DownController.class.getResource("Orders.xlsx").getPath();
// String f = DownController.class.getClassLoader().getResource(“4.txt”).getPath();
File file=new File(f);
FileInputStream fileinput=new FileInputStream(file);
/* BufferedReader input = new BufferedReader(new InputStreamReader(fileinput));
OutputStream out = resp.getOutputStream();
// byte[] b = new byte[2048];
// int len;
String line;
while ( (line=input.readLine()) != null) {
out.write(line.getBytes("utf-8"));
}*/
FileInputStream input = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
//修正 Excel在xxx.xlsx中发现不可读取的内容。是否恢复此工做薄的内容?若是信任此工做簿的来源,请点击"是"
resp.setHeader("Content-Length", String.valueOf(fileinput.getChannel().size()));
input.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3d