package com.aegon_cnooc.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import java.io.IOException;
/**
* 导出Excel的公共方法
* @Author: liuxinghui
* @Date: Dec 29, 2011
* @Version: 2.0
* @Despcrition:
*/
public class ExcelUtil {
/**
*
* @param request request对象
* @param sheetname 工作表实例的名称
* @return
* @throws Exception
*/
public static void exportExcelList(HttpServletRequest request,HttpServletResponse response,String sheetname,String tablename,String excelName,String[] titles,Map[] dataMap)throws Exception{
// 定义HSSFWorkbook对象,代表excel工作表
HSSFWorkbook workbook = null;
// 创建工作簿实例
workbook = new HSSFWorkbook();
// 创建工作表实例
HSSFSheet sheet = workbook.createSheet(sheetname);
//去掉网格线
sheet.setDisplayGridlines(false);
//设置列的默认宽度
sheet.setDefaultColumnWidth((short)20.00);
//合并第一行的单元格
sheet.addMergedRegion(new Region(0,(short)0,0,(short)(titles.length-1)));
//创建表头style
HSSFCellStyle cellStyleTitle=workbook.createCellStyle();
cellStyleTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //填充单元格
cellStyleTitle.setFillForegroundColor(HSSFColor.AQUA.index);
cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// //居中显示
HSSFRow tableNameRow = sheet.createRow((short)0);//创建第一行
//创建表的名称
HSSFCell tableCell=tableNameRow.createCell((short)0);
tableCell.setCellValue(tablename);
tableCell.setCellStyle(cellStyleTitle);
tableCell.setEncoding(HSSFCell.ENCODING_UTF_16);
//创建标题
if (dataMap != null) {
// 创建标题行
HSSFRow titleRow = sheet.createRow(1);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = titleRow.createCell((short) i);// 创建数据列
cell.setCellStyle(createCellColorStyle(workbook));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(titles[i]);// 给单元格赋值
}
// 填充表格
for (int i = 0; i < dataMap.length; i++) {
HSSFRow dataRow = sheet.createRow(i + 2);
Map map =dataMap[i];
int j = 0;
for(int b=0;b<titles.length;b++){
HSSFCell cell = dataRow.createCell((short)j++);// 创建数据列
cell.setCellStyle(createCellStyle(workbook));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
if(null!=map.get(titles[b])&&""!=map.get(titles[b])){
if("GAP".equals(titles[b])){
cell.setCellValue(Double.parseDouble(map.get(titles[b]).toString()));
}else{
cell.setCellValue(map.get(titles[b]).toString());
}
}else{
cell.setCellValue("");
}
}
}
}
// 设置为下载application/force-download
String ddate=new SimpleDateFormat("HHmmss").format(Calendar.getInstance().getTime());
response.setHeader("content-disposition", "attachment; filename="+excelName+"_"+ddate+".xls");//设定输出文件头
response.setContentType("application/msexcel");//定义输出类型
response.setCharacterEncoding("GBK");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
workbook.write(sos);
sos.flush();
}finally {
try {
sos.close();
} catch (IOException e) {
}
}
}
/**
* 无颜色的Cell并且有边框
* @param wb
* @param row
* @param col
* @param val
*/
private static HSSFCellStyle createCellStyle(HSSFWorkbook wb){
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor((short) 0);
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor((short) 0);
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyle.setRightBorderColor((short) 0);
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyle.setTopBorderColor((short) 0);
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// //居中显示
return cellstyle;
}
/**
* 带有颜色的Cell并且有边框
* @param wb
* @param row
* @param col
* @param val
*/
private static HSSFCellStyle createCellColorStyle(HSSFWorkbook wb){
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //填充单元格
cellstyle.setFillForegroundColor(HSSFColor.AQUA.index); //填绿色
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor((short) 0);
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor((short) 0);
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyle.setRightBorderColor((short) 0);
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyle.setTopBorderColor((short) 0);
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// //居中显示
return cellstyle;
}
}
package com.aegon_cnooc.oa.reportform.action;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.aegon_cnooc.framework.base.action.BaseAction;
import com.aegon_cnooc.framework.db.ConnectionPool;
import com.aegon_cnooc.framework.log.Log;
import com.aegon_cnooc.oa.reportform.service.ReportFormService;
import com.aegon_cnooc.oa.reportform.service.impl.ReportFormServiceImpl;
import com.aegon_cnooc.util.ExcelUtil;
import com.aegon_cnooc.util.LogPathUtil;
import com.aegon_cnooc.util.StringUtil;
/**
* 导SR报表
* @Author: liuxinghui
* @Date: Dec 29, 2011
* @Version: 2.0
* @Despcrition:
*/
public class ReportSrOneAction extends BaseAction{
Log logWrite=new Log();
public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
logWrite.writeLog(LogPathUtil.getPath(), "开始加载数据", "info");
String flag=request.getParameter("flag");//0:查询;1:导出Excel;
ReportFormService reportFormService=new ReportFormServiceImpl();
ConnectionPool connectionPool = ConnectionPool.getInstance();
Connection conn = connectionPool.getConnection();
if("0".equals(flag)){//执行查询
String month=request.getParameter("month");
logWrite.writeLog(LogPathUtil.getPath(), "月份的值为:"+month, "info");
if(StringUtil.isNotEmpty(month)){
Map[] srList=reportFormService.getSrReportListOne(month, conn);
request.setAttribute("srMap", srList);
request.setAttribute("month", month);
conn.close();
}
logWrite.writeLog(LogPathUtil.getPath(), "数据加载完毕", "info");
return mapping.findForward("success");
}else{//执行导出Excel
String month=request.getParameter("month");
String sheetname="REPORT_P5_2_1";
String tablename="REPORT_P5_2_1";
String excelName="REPORT_P5_2_1";
String titles[]={"U_DEPT","U_DEPT_TYPE","U_USERNAME","U_SR_NO","GENSTEPNAME","STATUS","U_SUBMIT_DATE",
"U_DESCRIPTION","USERDSPNAME","BPA_BA_USERNAME","BPA_RECE_DATE","BPA_COMP_DATE",
"ITD_SA_USERNAME","ITD_RECE_DATE","ITD_COMP_DATE","RECTIME","TASKID","BACKUPYM", "SR_TYPE",
"ITD_PROJECT_NO","IS_KEY","SA_RECEIVED_DATE","U_SR_REQCOMP_DATE"
};
Map[] srMap;
if(StringUtil.isNotEmpty(month)){
srMap=reportFormService.getSrReportListOne(month, conn);
}else{
srMap=new HashMap[0];
}
ExcelUtil.exportExcelList(request, response, sheetname, tablename, excelName, titles, srMap);
conn.close();
return null;
}
}
}