下載模板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