JAVA POI自动导出数据和图片到excel
来源:互联网 发布:数据库设计方法和原则 编辑:程序博客网 时间:2024/04/28 17:08
1.需求描述
由于程序导出excel,还有图片,对内存和cpu占用率很高,如果很多人同时点击导出按钮,服务器压力会很大,所以用定时程序让它每天晚上跑一次,然后获取生成路径,点击超链接就可以了,下载速度很快,有人推荐我使用线程,查了一下很繁琐,自己又不熟练,一把伤心泪。。。
2.效果截图
3.创建excel,代码都有注释,不过多解释
package com.demo.task;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
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.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import common.image.util.ImageHepler;
import common.util.DateUtil;
import jxl.write.WriteException;
import oracle.sql.BLOB;
public class TrainerImageJob {
@Autowired
private TrainerService trainerService;
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* 导出图片
* @param model
* @param query
* @param request
* @param workbook
* @param response
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalStateException
* @throws ServletException
* @throws IOException
* @throws WriteException
* @throws SQLException
*/
public void run(){
try {
TrainerQuery query=new TrainerQuery();
query.setTrainerLevelPh(“3”);
query.setFullName(“Trainer_T3”);
exportEmployeeImage(query);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void exportEmployeeImage(TrainerQuery query)
throws NoSuchFieldException, SecurityException,
IllegalStateException, ServletException, IOException,
WriteException, SQLException {
long startTime=System.currentTimeMillis(); //获取开始时间
XSSFWorkbook input_work = new XSSFWorkbook();
XSSFSheet inpub_sheet=input_work.createSheet(“Trainer”);
// 设置样式
XSSFCellStyle textStyle10 = input_work.createCellStyle();
textStyle10.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
// 设置垂直居中
textStyle10.setAlignment(HorizontalAlignment.CENTER);
textStyle10.setVerticalAlignment((short) 1);
// 设置细边框
textStyle10.setWrapText(true);
XSSFFont fontText2 = input_work.createFont();
// 字体号码
fontText2.setFontHeightInPoints((short) 11);
// 字体名称
fontText2.setFontName(“微软雅黑”);
textStyle10.setFont(fontText2);
// 设置样式
XSSFCellStyle textStyle = input_work.createCellStyle();
textStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//左右居中
textStyle10.setAlignment(HorizontalAlignment.CENTER);//上下居中
// 设置细边框
textStyle.setWrapText(true);
XSSFFont fontText = input_work.createFont();
// 字体名称
fontText.setFontName(“微软黑体”);
// 字体号码
fontText.setFontHeightInPoints((short) 18);
fontText.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//加粗
textStyle.setFont(fontText);
query.setNeedQueryAll(false);
query.setTotalItem(query.getTotal());
List
<bean id="trainerImageJob" class="com.demo.task.TrainerImageJob"></bean> <bean id="JobtrainerPhoto" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref local="trainerImageJob" /> </property> <property name="targetMethod"> <!-- 要执行的方法名称 --> <value>run</value> </property> </bean> <bean id="cronTrainerImage" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="JobtrainerPhoto" /> </property> <property name="cronExpression"> <value>0 0 7 * * ?</value> </property> </bean>
4.项目启动,大功告成
- JAVA POI自动导出数据和图片到excel
- java poi导出list数据到excel
- java利用poi导出数据到excel
- java中将数据和图片导出到Excel文件中
- java 导出到excel----POI
- java poi 导出到Excel
- Java POI Excel 数据导出
- java poi实现数据库数据导出到excel
- java利用poi导出数据到excel里
- java中使用poi导出数据到EXCEL模板中。
- 使用POI和Java反射机制导出数据到excel中
- POI导出图片到excel中
- 用apache poi导出数据到Excel
- struts2 poi 把数据导出到excel
- POI导出数据到EXCEL经典实现
- poi技术导出海量数据到excel
- POI实现数据导出到Excel
- 用POI导出数据到excel文件
- 高精度加法(非负)模板
- selenium webDriver 启动Google浏览器遇到的问题
- 安卓上实现的自定义心电波形控件
- c#数据结构——栈
- 基数排序
- JAVA POI自动导出数据和图片到excel
- RSA实现JS前端加密,PHP后端解密
- git相关命令
- 从输入URL到浏览器显示页面发生了什么
- SoundPool的应用
- 关于jquery中的ajax调取数据成功回调函数,失败回调函数
- Longest Univalue Path
- Android drawable中的shape属性详解
- The “Swift Language Version” (SWIFT_VERSION) build setting must be ...