package cn.com.mcd.controller.export;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel. CellStyle;
import org.apache.poi.ss.usermodel.Font;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.mcd.common.ResultModel;
import cn.com.mcd.controller.PurchaseItListController;
import cn.com.mcd.exception.rest.exhandler.DataBaseAccessException;
import cn.com.mcd.util.Constants;
@Controller
@RequestMapping("/nkBuy")
public class NikeBuyExportController implements Serializable{
private static final long serialVersionUID = 2732216546553695880L;
private static final Logger log = LoggerFactory.getLogger(PurchaseItListController.class);
static String excelPath = "nikeBuy.xlsx";
/**
* 导出nike_buy到excel
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value = "/exportExcleXSSF", method = RequestMethod.GET)
@ResponseBody
public ResultModel exportExcleXSSF(HttpServletRequest request, HttpServletResponse response)throws IOException{
ResultModel resultModel = new ResultModel();
//linux下jdk1.8方法获取时,不会拼接自己写的目录
String path = request.getSession().getServletContext(). getRealPath("/") + "/files/xlsprint/"; //模板文件的相对路径(相对于当前工程)
InputStream is = new FileInputStream(new File(path + "nikeBuy.xlsx")); //读取模板文件
Workbook workbook = new XSSFWorkbook(is); //打开一个模板文件,工作簿2007以上版本
boolean isCreateSuccess = false;
Sheet sheet = workbook.getSheetAt(0); //获取到第一个工作表
if (workbook != null) {
for (int rowNum = 4; rowNum < 100; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int i = 0; i < 179; i++) {
Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
cell.setCellValue("10101" + String.valueOf(rowNum + 1) + String.valueOf(i + 1));
}
}
try {
FileOutputStream outputStream = new FileOutputStream(excelPath);
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
log.error(this.getClass().getName()+".exportExcleXSSF.......文件导出失败",e);
throw new DataBaseAccessException(5001+"文件导出失败"+e);
}
}
//此处仅仅是为了打印日志:查看文件出路径(将文件固定路径改为让客户自己手动选择保存位置的弹出框)
File file = new File(excelPath);
System.out.println("..............导出成功,导出路径为:="+file.getAbsolutePath());
log.info(this.getClass().getName()+"..............导出成功,导出路径为:="+file.getAbsolutePath());
resultModel.setResultCode(Constants.SERVICE_SUCCESS_CODE);//200
resultModel.setResultMsg(Constants.DATA_BASE_EXPORT_SUCCESS_MSG+"导出路径为:"+file.getAbsolutePath());//"文件导出成功";
return resultModel;
}
/**
* by tony 2017-3-15
* @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream
* @param response HttpServletResponse 写入response
* @param returnName 返回的文件名
* @return
*/
public static void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException{
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream(); //取得输出流
byteArrayOutputStream.writeTo(outputstream); //写到输出流
byteArrayOutputStream.close(); //关闭
outputstream.flush(); //刷数据
outputstream.close();
}
private static CellStyle getStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 设置单元格字体
Font headerFont = workbook.createFont(); // 字体
headerFont.setFontHeightInPoints((short) 14);
headerFont.setColor(HSSFColor.RED.index);
headerFont.setFontName("宋体");
style.setFont(headerFont);
style.setWrapText(true);
// 设置单元格边框及颜色
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
style.setWrapText(true);
return style;
}
}