1、图片下载
@RequestMapping("/downPicture")
public void downPicture(HttpServletRequest request,HttpServletResponse response){
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
String imagePath = "f://test.png";//无斜杠结尾
File file = new File(imagePath);
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) != -1) {
out.write(b, 0, b.length);
}
in.close();
String fileName = "test.png";
ServletOutputStream sos = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(out.size());
response.setHeader("Content-disposition", "attachment;filename="+fileName);
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
sos.write(out.toByteArray());
out.flush();
out.close();
sos.flush();
}catch(Exception e){
logger.error("downPicture:下载图片异常:"+e.getMessage(), e);
}
}
2、导出Excel为压缩包
@SuppressWarnings("unchecked")
@RequestMapping("/exportTacList")
public void exportTacList(HttpServletRequest request, HttpServletResponse response)throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
Map<String, Object> params = new HashMap<String, Object>();
String fileName = "导出列表"; // 文件名
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/zip");
String outfilename;
// IE浏览器 终极解决文件名乱码
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
outfilename = URLEncoder.encode(fileName, "UTF-8");
}
// firefox浏览器
else {
outfilename = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
response.setHeader("Content-disposition", "attachment;filename=" + outfilename + ".zip");
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
OutputStreamWriter osw = null;
BufferedWriter bw = null;
Connection conn = DbCommon.getConnection();//连接数据库
Statement sts = null;
ResultSet rs = null;
try{
osw = new OutputStreamWriter(zipOut, "gbk");
bw = new BufferedWriter(osw);
String csvName = "导出列表-"+ sdf2.format(new Date()) + ".csv";
zipOut.putNextEntry(new ZipEntry(csvName));
// 导出头部
StringBuffer report_head_csv = new StringBuffer().append(getExcelHead());
bw.write(report_head_csv.toString());
bw.newLine();
int page = 1;
int tempSize=2000;//一次查询条数
List<TestVo> list = new ArrayList<TestVo>();
TestVo testVo= new TestVo();
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
do{
params.put(Page.CURR_PAGE, page);
params.put(Page.PAGE_SIZE, tempSize);
String sql = createSql(params);//创建sql
sts = conn.createStatement();
rs = sts.executeQuery(sql);
list = new ArrayList<TmlTerminalTac>();
while(rs.next()){
testVo= new TestVo();
testVo.setTacCode(rs.getString("TAC_CODE"));//设置属性
list.add(testVo);
}
for (int j = 0; j < list.size(); j++) {
TestVottt = list.get(j);
StringBuffer tttStr = new StringBuffer();
tttStr.append(ttt.getTacCode()==null?"":ttt.getTacCode()).append(",");
}
bw.write(tttStr.deleteCharAt(tttStr.length() - 1).toString());
bw.newLine();
}
page++;
}while(CollectionUtils.isNotEmpty(list) && list.size() == tempSize);
bw.flush();
}catch(Exception e){
e.printStackTrace();
}finally {
DbCommon.closeConnection(conn);
try {
if (zipOut != null) {
zipOut.close();
}
if (osw != null) {
osw.close();
}
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Object getExcelHead() {
StringBuffer headStr = new StringBuffer();
headStr.append("第一列,");
String str = headStr.toString();
str = str.substring(0, str.length()-1);
return str;
}
3、Excel的表格样式
private XSSFWorkbook excel(List<TmlTerminalTac> list,String operType) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 第一步,创建一个webbook,对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("TAC码列表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
XSSFRow firstRow = sheet.createRow(0); // 创建标题行
// 第四步,创建单元格,并设置值表头 设置表头居中
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
sheet.setColumnWidth(0, 30 * 256); // 设置第一列的宽
sheet.setColumnWidth(1, 30 * 256); // 设置第二列的宽
XSSFRow secondRow = sheet.createRow(0); // 创建第一行
XSSFCell cell = secondRow.createCell(0);// 第一列
cell.setCellValue("TAC码");
cell.setCellStyle(style);
cell = secondRow.createCell(1);// 第二列
cell.setCellValue("品牌");
cell.setCellStyle(style);
int i = 1;
for (TmlTerminalTac tmlTerminalTac : list) {
secondRow = sheet.createRow(i);// 从第二行开始,创建下一行
cell = secondRow.createCell(0);
cell.setCellValue("第一列");
cell.setCellStyle(style);
cell = secondRow.createCell(1);
cell.setCellValue("第二列");
cell.setCellStyle(style);
i++;
}
return wb;
}