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.項目啓動,大功告成