最近做项目的时候遇到了这样一个问题,微信内置的浏览器把下载这个功能屏蔽了。唉,,,折腾了一天,从网上各种找资料,但是给的解决方案都不是我想要的(也不知道谁复制的谁的,基本都一样)。

在快下班的时候,我请教了一位朋友,我问他是怎么解决的,他说:很简单,发邮件就可以了。

为方便大家开发,下面附上代码。此代码包括发送邮件和生成表格。

/**

* @author h_debug

* @date 2018年7月21日 上午9:13:25

* @Copyright: 2018 . All rights reserved.

* @version V1.0

*/

import java.io.ByteArrayInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.security.GeneralSecurityException;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.util.ByteArrayDataSource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;

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.RequestParam;

import com.sun.mail.util.MailSSLSocketFactory;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

@RequestMapping("/sendmail")

@Controller

public class SendMailUtil {

/*

* 发送邮件到指定邮箱

*/

@SuppressWarnings("deprecation")

@RequestMapping(value = "/moremail", method = RequestMethod.POST)

public static void send(HttpServletRequest request,@RequestParam(value = "emails", required = true) String emails,@RequestParam(value = "contents", required = true) String contents,@RequestParam(value = "mores", required = true) String mores,HttpServletResponse resp) throws Exception{

// // 未填写邮箱地址的供货商列表

// List vendorsList = new ArrayList();

String path = request.getRealPath("WEB-INF/excel/aaa.xls");

ByteArrayOutputStream byteOS = new ByteArrayOutputStream();

@SuppressWarnings("resource")

FileInputStream fis = new FileInputStream(path);

byte[] by = new byte[512];

int t = fis.read(by,0,by.length);

while(t>0){ byteOS.write(by, 0, 512); //这里别写成t,写够512,呵呵,SB的方法对付SB的java API

t = fis.read(by,0,by.length);

}

byteOS.close();

InputStream byteIS = new ByteArrayInputStream(byteOS.toByteArray());

HSSFWorkbook workBook = new HSSFWorkbook(byteIS);

// 获取第一个sheet页

HSSFSheet sheet=workBook.getSheetAt(0);

/******************************设置表头*****************************/

String sheetTitle = contents+"--进出库详情";

// 单元格合并

// 四个参数分别是:起始行,起始列,结束行,结束列

sheet.addMergedRegion(new CellRangeAddress(0,1,0,4));

// 创建表头并赋值

HSSFRow rowTitle = sheet.createRow(0);

HSSFCell cellTitle = rowTitle.createCell(0);

cellTitle.setCellValue(sheetTitle);

// 创建剧中样式

HSSFCellStyle cellStyle = workBook.createCellStyle();

// 垂直居中

cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 水平居中

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 创建字体

HSSFFont cellFont = workBook.createFont();

cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

cellFont.setFontHeight((short) 300);

cellStyle.setFont(cellFont);

cellTitle.setCellStyle(cellStyle);

/**********************遍历物料,并将物料列表显示到模板中,作为邮件的附件***********************/

JSONArray jsonss=JSONArray.fromObject(mores);

// 行号下标,从0开始

int rowIndex = 2 ;

for(int j=0;j

rowIndex++;

// 创建行

HSSFRow row=sheet.createRow(rowIndex);

// 根据下标创建单元格

// 设置物料序号

row.createCell(0).setCellValue(j+1);

// 设置IMPA编码

if(null != jsonss.getJSONObject(j).getString("sname")){

row.createCell(1).setCellValue(jsonss.getJSONObject(j).getString("sname"));

}else {

row.createCell(1).setCellValue("");

}

// 设置物料中文名称

if(null != jsonss.getJSONObject(j).getString("conts")){

row.createCell(2).setCellValue(jsonss.getJSONObject(j).getString("conts"));

}else {

row.createCell(2).setCellValue("");

}

// 设置物料英文名称

if(null != jsonss.getJSONObject(j).getString("iconts")){

row.createCell(3).setCellValue(jsonss.getJSONObject(j).getString("iconts"));

}else {

row.createCell(3).setCellValue("");

}

}

ByteArrayOutputStream baos = new ByteArrayOutputStream();

workBook.write(baos);

baos.flush();

byte[] bt = baos.toByteArray();

InputStream is = new ByteArrayInputStream(bt, 0, bt.length);

baos.close();

boolean s = sendMail(sheetTitle, emails, "aa" , is);

System.out.println(s+"00000");

JSONObject Json = new JSONObject();

JSONArray JsonArray = new JSONArray();

if(s) {

Json.put("status", "1");

Json.put("data", s);

Json.put("contents", "导出成功");

}else {

Json.put("status", "0");

Json.put("data", s);

Json.put("contents", "导出失败");

}

JsonArray.add(Json);

try {

resp.setCharacterEncoding("UTF-8");

resp.getWriter().print(Json);

} catch (IOException e) {

e.printStackTrace();

}

}

public static boolean sendMail(String subject, String to, String content, InputStream is) throws GeneralSecurityException, IOException {

boolean isFlag = false;

// 收件人电子邮箱

//String to = "收件人电子邮箱";

// 发件人电子邮箱

String from = "发件人电子邮箱";

// 指定发送邮件的主机为 smtp.qq.com

String host = "smtp.qq.com"; //QQ 邮件服务器

// 获取系统属性

Properties properties = System.getProperties();

// 设置邮件服务器

properties.setProperty("mail.smtp.host", host);

properties.put("mail.smtp.auth", "true");

MailSSLSocketFactory sf = new MailSSLSocketFactory();

sf.setTrustAllHosts(true);

properties.put("mail.smtp.ssl.enable", "true");

properties.put("mail.smtp.ssl.socketFactory", sf);

// 获取默认session对象

Session session = Session.getDefaultInstance(properties,new Authenticator(){

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication("发件人电子邮箱", "发件人电子邮箱密码"); //发件人邮件用户名、密码

}

});

try{

// 创建默认的 MimeMessage 对象

MimeMessage message = new MimeMessage(session);

// Set From: 头部头字段

message.setFrom(new InternetAddress(from));

// Set To: 头部头字段

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Set Subject: 头部头字段

message.setSubject(subject);

// 设置消息体

// 创建消息部分

BodyPart messageBodyPart = new MimeBodyPart();

// 消息

messageBodyPart.setText(subject);

// 创建多重消息

Multipart multipart = new MimeMultipart();

// 设置文本消息部分

multipart.addBodyPart(messageBodyPart);

// 附件部分

messageBodyPart = new MimeBodyPart();

String filename = subject+".xls";

DataSource source = new ByteArrayDataSource(is, "application/msexcel");

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(filename);

multipart.addBodyPart(messageBodyPart);

// 发送完整消息

message.setContent(multipart);

// 发送消息

Transport.send(message);

System.out.println("Sent message successfully....from runoob.com");

isFlag = true;

}catch (MessagingException mex) {

mex.printStackTrace();

isFlag = false;

}

return isFlag;

//return false;

}

}

转自LCSDNY的博客

java+自带excel导出_Java实现微信内置浏览器导出Excel表格功能相关推荐

  1. 微信内置浏览器导出Excel表格功能

    最近做项目的时候遇到了这样一个问题,微信内置的浏览器把下载这个功能屏蔽了.唉,,,折腾了一天,从网上各种找资料,但是给的解决方案都不是我想要的(也不知道谁复制的谁的,基本都一样). 在快下班的时候,我 ...

  2. 判断网页是否为微信内置浏览器打开?

    文章目录 (两种方法)教你:"如何判断网页是不是在微信端内置浏览器打开?" 本文根据项目开发实际情况,着重探讨在微信内置浏览器中调用支付功能,遇到的几个坑! 目的: (方法一)全部 ...

  3. 使用微信内置浏览器预览图片

    在微信H5开发中预览图片,可以使用其他的一些图片预览插件,但是这样却不能把其中的某张图片发送给好友.对于 这种情况可以使用微信内置浏览器图片预览功能,就可以解决这个问题.不说废话直接看代码: 1.首先 ...

  4. 微信内置浏览器怎么才能自动跳转到手机自带浏览器

    上半年公司有一个新的APP项目上线,我们在项目首页做个二维码,然后用户用手机扫一扫就能下载了.但是很多用户反映扫一扫之后下载不了,了解之后才知道这些用户都是使用的微信的扫一扫,而我们开发测试人员一般使 ...

  5. 企业微信内置浏览器中去除自带的放大缩小控件

    企业微信浏览器中嵌套h5页面的时候会出现放大缩小控件感觉特别别扭.在这里记录一下解决这个问题的办法 <meta http-equiv="Content-Type" conte ...

  6. 微信调试--微信内置浏览器为什么对pharser.js支持这么差???

    微信内置浏览器对于html5的支持如何? 是否可以等同于webkit内核的浏览器?CSS3动画的支持程度怎么样? 添加评论 分享 按投票排序按时间排序 31 个回答 28赞同 反对,不会显示你的姓名 ...

  7. 微信内置浏览器的JsAPI(WeixinJSBridge续)[转载]

    原文地址:  http://www.baidufe.com/item/f07a3be0b23b4c9606bb.html 之前有写过几篇关于微信内置浏览器(WebView)中特有的Javascript ...

  8. python 模拟微信浏览器请求_使用Chrome修改user agent模拟微信内置浏览器

    很多时候,我们需要模拟微信内置浏览器,今天教大家用chrome简单模拟.如图设置: F12或者右键审查元素进入开发者模式,点击Emulation,然后点击Network,把Spoof user age ...

  9. 安卓下微信内置浏览器视频出现解析错误

    原文地址 今天给客户做一个微信端的HTML5的动画页面,页面内有一个视频文件,今天上线,这是前提.刚上线不久,客户的服务器便不堪重负,为了解决问题,我们将该页面的媒体文件放在自己的服务器上.问题来了, ...

最新文章

  1. 静态时序分析的约束命令
  2. Grails通过sessionId获取session对象
  3. mysql版本选择最终建议
  4. activePerl的PPM如何使用http代理
  5. 用Scrutor来简化ASP.NET Core的DI注册
  6. Windows消息机制疑问探究
  7. OPPO小布助手算法系统的探索、实践与思考
  8. 安装linux时找不到硬盘,关于安装LINUX时找不到硬盘问题解决
  9. EFResume 一个普通的 Swift 简历模板
  10. 使用python打开多台IMAGINGSOURCE工业相机
  11. Java 读取Doc/Docx 文档
  12. EXCEL VBA开发单元格日历选择
  13. Mono 的执行流程
  14. apple 证书 账号 内购 详解
  15. 我们都在过着「抽奖」的人生
  16. 作业中关于H5中动画的实现——animation
  17. 2017年中国互联网企业100强排行榜
  18. 插槽和axios的封装
  19. Myeclipse配置Tomcat
  20. vs2017取消起始页(设定起始页)/(.ashx文件的添加)

热门文章

  1. 不服跑个分?双模5G性能旗舰iQOO 3发布
  2. html自动识别语言,SyntaxHighlighter自动识别并加载脚本语言
  3. 创作类游戏《塞尔达传说:旷野之息》风格的水着色器
  4. 小识堂 | 嵌入式系统的11大特点,你知多少?
  5. 深度学习有哪些局限?
  6. HTML5 移动Web App阅读器-3(开发流程介绍、需求分解和技术选型)
  7. 网友报告QQ扫描并上传用户的浏览器历史
  8. 图形化编程 超级马里奥_超级马里奥可以向我们传授哪些图形技术?
  9. wireshark抓tcp包并分析
  10. Android12之OpenSL ES中objectIDtoClass分析拆解(十三)