java+自带excel导出_Java实现微信内置浏览器导出Excel表格功能
最近做项目的时候遇到了这样一个问题,微信内置的浏览器把下载这个功能屏蔽了。唉,,,折腾了一天,从网上各种找资料,但是给的解决方案都不是我想要的(也不知道谁复制的谁的,基本都一样)。
在快下班的时候,我请教了一位朋友,我问他是怎么解决的,他说:很简单,发邮件就可以了。
为方便大家开发,下面附上代码。此代码包括发送邮件和生成表格。
/**
* @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表格功能相关推荐
- 微信内置浏览器导出Excel表格功能
最近做项目的时候遇到了这样一个问题,微信内置的浏览器把下载这个功能屏蔽了.唉,,,折腾了一天,从网上各种找资料,但是给的解决方案都不是我想要的(也不知道谁复制的谁的,基本都一样). 在快下班的时候,我 ...
- 判断网页是否为微信内置浏览器打开?
文章目录 (两种方法)教你:"如何判断网页是不是在微信端内置浏览器打开?" 本文根据项目开发实际情况,着重探讨在微信内置浏览器中调用支付功能,遇到的几个坑! 目的: (方法一)全部 ...
- 使用微信内置浏览器预览图片
在微信H5开发中预览图片,可以使用其他的一些图片预览插件,但是这样却不能把其中的某张图片发送给好友.对于 这种情况可以使用微信内置浏览器图片预览功能,就可以解决这个问题.不说废话直接看代码: 1.首先 ...
- 微信内置浏览器怎么才能自动跳转到手机自带浏览器
上半年公司有一个新的APP项目上线,我们在项目首页做个二维码,然后用户用手机扫一扫就能下载了.但是很多用户反映扫一扫之后下载不了,了解之后才知道这些用户都是使用的微信的扫一扫,而我们开发测试人员一般使 ...
- 企业微信内置浏览器中去除自带的放大缩小控件
企业微信浏览器中嵌套h5页面的时候会出现放大缩小控件感觉特别别扭.在这里记录一下解决这个问题的办法 <meta http-equiv="Content-Type" conte ...
- 微信调试--微信内置浏览器为什么对pharser.js支持这么差???
微信内置浏览器对于html5的支持如何? 是否可以等同于webkit内核的浏览器?CSS3动画的支持程度怎么样? 添加评论 分享 按投票排序按时间排序 31 个回答 28赞同 反对,不会显示你的姓名 ...
- 微信内置浏览器的JsAPI(WeixinJSBridge续)[转载]
原文地址: http://www.baidufe.com/item/f07a3be0b23b4c9606bb.html 之前有写过几篇关于微信内置浏览器(WebView)中特有的Javascript ...
- python 模拟微信浏览器请求_使用Chrome修改user agent模拟微信内置浏览器
很多时候,我们需要模拟微信内置浏览器,今天教大家用chrome简单模拟.如图设置: F12或者右键审查元素进入开发者模式,点击Emulation,然后点击Network,把Spoof user age ...
- 安卓下微信内置浏览器视频出现解析错误
原文地址 今天给客户做一个微信端的HTML5的动画页面,页面内有一个视频文件,今天上线,这是前提.刚上线不久,客户的服务器便不堪重负,为了解决问题,我们将该页面的媒体文件放在自己的服务器上.问题来了, ...
最新文章
- Codeforces 358 D. Dima and Hares
- win10 远程出现身份验证错误 要求的函数不受支持
- HDFS的API调用,创建Maven工程,创建一个非Maven工程,HDFS客户端操作数据代码示例,文件方式操作和流式操作
- 局部变量,静态局部变量,全局变量,静态全局变量在内存中的存放区别(转)...
- [WPF]获取控件间的相对位置
- STM32F0使用LL库实现SHT70通讯
- 使用PHP创建SOCKET服务
- python 3d绘图立方体_python绘制3D立方体
- Spring全家桶——SpringCloud之Feign(Finchley版)
- TensorFlow可以“预装”数据集了,新功能Datasets出炉
- vue回调函数this指向问题
- 电子测量技术——基于Python的测量数据误差处理程序
- kindle亚马逊个人文档不显示_Kindle的PC版如何看个人文档
- 鸟哥的Linux私房菜基础学习篇 第2章的重点探索
- vue中用canvas 画一个六边形 类似蜂窝形的功能导航主菜单
- 使用Houdini快速将图片转换成文字模型
- CYCADA: cycle-consistent adversaial domain adaption阅读笔记
- word插入图表目录的时候同时插入了原图的解决办法
- Matlab中meshgrid的用法
- python十六进制转为二进制数_python进制转换(二进制、十进制和十六进制)及注意事项...
热门文章
- 爬虫框架Scrapy(10)下载文件与图片
- 以太网没有有效的ip配置/无Internet访问权限
- 金属钯配合物磷光材料相关性能及应用发展|配合物C8FPt、C8Ptpiq和FPt2(FIrPic-C8-SX2T)2齐岳试剂
- 我没有学历,但不代表我失去了能力!——随笔
- Vue2.0进阶组件 短信倒计时组件
- 入手评测 小米笔记本Pro X怎么样
- 创赛入门常见30问解答,或许有你想知道的
- Java小程序|网页后台 物流查询 第三方平台:极速数据物流查询
- Echarts常见问题整理
- 快递单号查询API接口对接Java源码示例_快递鸟