package cn.richinfo.cmail.log.common;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
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.HSSFRichTextString;
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.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
@SuppressWarnings({ "unchecked", "deprecation" })
public class ExpExcelHelper {
public static void exportExcel(String[] Title,Map<String, ArrayList<ArrayList<String>>> listContent,String hidName,HttpServletRequest request,
HttpServletResponse response,String startTime,String endTime) throws Exception{
response.reset();
response.setContentType("APPLICATION/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=/"+getFileName()+".xls");
//设置第一个工作表的名称为name
HSSFWorkbook workbook=new HSSFWorkbook();// 产生工作薄对象-------文档对象
HSSFSheet sheet=workbook.createSheet();// 产生工作表对象---------表单
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)14); //字体高度
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
// 设置单元格类型样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//行底色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT); //水平布局:居左
cellStyle2.setWrapText(true);
HSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setAlignment(HSSFCellStyle.ALIGN_RIGHT); //水平布局:居右
cellStyle3.setFillBackgroundColor(HSSFColor.ORCHID.index);//行底色
cellStyle3.setWrapText(true);
Region region1 =new Region(0,(short)0,0,(short)6);
sheet.addMergedRegion(region1);
Region region2 =new Region(1,(short)0,1,(short)6);
sheet.addMergedRegion(region2);
sheet.setColumnWidth(0, 3766);
sheet.setColumnWidth(1, 4266);
sheet.setColumnWidth(2, 4266);
sheet.setColumnWidth(3, 4266);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
//产生一行
HSSFRow title =sheet.createRow((short)0);
title.setHeightInPoints(24);
HSSFCell cell = title.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(hidName));
HSSFRow dateStr =sheet.createRow((short)1);
HSSFCell cell1 = dateStr.createCell(0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(cellStyle3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// cell1.setCellValue(new HSSFRichTextString("日期:"+sdf.format(new Date())));
cell1.setCellValue(new HSSFRichTextString("查询日期:"+startTime+"-"+endTime));
HSSFRow header=sheet.createRow((short)2);
/** ***************以下是EXCEL第一行列标题********************* */
for (int i = 0; i < Title.length; i++) {
header.createCell((short)i).setCellValue(Title[i]);
}
/** ***************以下是EXCEL正文数据********************* */
int i=3;
Set<String> key = listContent.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
HSSFRow rowdate=sheet.createRow(i);
rowdate.createCell((short)0).setCellValue(s);
ArrayList<ArrayList<String>> rows1=listContent.get(s);
for(int r=0;r<rows1.size();r++)
{
HSSFRow row=sheet.createRow(i+1);
ArrayList<String> roww=rows1.get(r);
int j=0;
for(int jj=0;jj<roww.size();jj++)
{
if(roww.get(jj)!=null)
{
row.createCell((short)j).setCellValue(roww.get(jj).toString());
}
else
{
row.createCell((short)j).setCellValue("");
}
j++;
}
i=i+1;
}
i=i+1;
}
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
}
public static void exportExcelByStat(String[] Title, ArrayList<ArrayList<String>> listContent,String hidName,HttpServletRequest request,
HttpServletResponse response,String startTime,String endTime) throws Exception{
response.reset();
response.setContentType("APPLICATION/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=/"+getFileName()+".xls");
//设置第一个工作表的名称为name
HSSFWorkbook workbook=new HSSFWorkbook();// 产生工作薄对象-------文档对象
HSSFSheet sheet=workbook.createSheet();// 产生工作表对象---------表单
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)14); //字体高度
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
// 设置单元格类型样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//行底色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT); //水平布局:居左
cellStyle2.setWrapText(true);
HSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setAlignment(HSSFCellStyle.ALIGN_RIGHT); //水平布局:居右
cellStyle3.setFillBackgroundColor(HSSFColor.ORCHID.index);//行底色
cellStyle3.setWrapText(true);
Region region1 =new Region(0,(short)0,0,(short)6);
sheet.addMergedRegion(region1);
Region region2 =new Region(1,(short)0,1,(short)6);
sheet.addMergedRegion(region2);
sheet.setColumnWidth(0, 3766);
sheet.setColumnWidth(1, 4266);
sheet.setColumnWidth(2, 4266);
sheet.setColumnWidth(3, 4266);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
//产生一行
HSSFRow title =sheet.createRow((short)0);
title.setHeightInPoints(24);
HSSFCell cell = title.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(hidName));
HSSFRow dateStr =sheet.createRow((short)1);
HSSFCell cell1 = dateStr.createCell(0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(cellStyle3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//cell1.setCellValue(new HSSFRichTextString("日期:"+sdf.format(new Date())));
cell1.setCellValue(new HSSFRichTextString("查询日期:"+startTime+" - "+endTime));
HSSFRow header=sheet.createRow((short)2);
/** ***************以下是EXCEL第一行列标题********************* */
for (int i = 0; i < Title.length; i++) {
header.createCell((short)i).setCellValue(Title[i]);
}
/** ***************以下是EXCEL正文数据********************* */
int i=3;
for(int m=0;m<listContent.size();m++)
{
HSSFRow row=sheet.createRow(i);
ArrayList<String> rows=listContent.get(m);
int j=0;
for(int r=0;r<rows.size();r++)
{
if(rows.get(r)!=null)
{
row.createCell((short)j).setCellValue(rows.get(r).toString());
}
else
{
row.createCell((short)j).setCellValue("");
}
j++;
}
i=i+1;
}
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
}
public static String getFileName(){
SimpleDateFormat datetime = new SimpleDateFormat("yyyyMMddhhmmssSSS");
Date time = new Date();
String name = datetime.format(time);
return name;
}
}
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
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.HSSFRichTextString;
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.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
@SuppressWarnings({ "unchecked", "deprecation" })
public class ExpExcelHelper {
public static void exportExcel(String[] Title,Map<String, ArrayList<ArrayList<String>>> listContent,String hidName,HttpServletRequest request,
HttpServletResponse response,String startTime,String endTime) throws Exception{
response.reset();
response.setContentType("APPLICATION/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=/"+getFileName()+".xls");
//设置第一个工作表的名称为name
HSSFWorkbook workbook=new HSSFWorkbook();// 产生工作薄对象-------文档对象
HSSFSheet sheet=workbook.createSheet();// 产生工作表对象---------表单
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)14); //字体高度
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
// 设置单元格类型样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//行底色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT); //水平布局:居左
cellStyle2.setWrapText(true);
HSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setAlignment(HSSFCellStyle.ALIGN_RIGHT); //水平布局:居右
cellStyle3.setFillBackgroundColor(HSSFColor.ORCHID.index);//行底色
cellStyle3.setWrapText(true);
Region region1 =new Region(0,(short)0,0,(short)6);
sheet.addMergedRegion(region1);
Region region2 =new Region(1,(short)0,1,(short)6);
sheet.addMergedRegion(region2);
sheet.setColumnWidth(0, 3766);
sheet.setColumnWidth(1, 4266);
sheet.setColumnWidth(2, 4266);
sheet.setColumnWidth(3, 4266);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
//产生一行
HSSFRow title =sheet.createRow((short)0);
title.setHeightInPoints(24);
HSSFCell cell = title.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(hidName));
HSSFRow dateStr =sheet.createRow((short)1);
HSSFCell cell1 = dateStr.createCell(0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(cellStyle3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// cell1.setCellValue(new HSSFRichTextString("日期:"+sdf.format(new Date())));
cell1.setCellValue(new HSSFRichTextString("查询日期:"+startTime+"-"+endTime));
HSSFRow header=sheet.createRow((short)2);
/** ***************以下是EXCEL第一行列标题********************* */
for (int i = 0; i < Title.length; i++) {
header.createCell((short)i).setCellValue(Title[i]);
}
/** ***************以下是EXCEL正文数据********************* */
int i=3;
Set<String> key = listContent.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
HSSFRow rowdate=sheet.createRow(i);
rowdate.createCell((short)0).setCellValue(s);
ArrayList<ArrayList<String>> rows1=listContent.get(s);
for(int r=0;r<rows1.size();r++)
{
HSSFRow row=sheet.createRow(i+1);
ArrayList<String> roww=rows1.get(r);
int j=0;
for(int jj=0;jj<roww.size();jj++)
{
if(roww.get(jj)!=null)
{
row.createCell((short)j).setCellValue(roww.get(jj).toString());
}
else
{
row.createCell((short)j).setCellValue("");
}
j++;
}
i=i+1;
}
i=i+1;
}
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
}
public static void exportExcelByStat(String[] Title, ArrayList<ArrayList<String>> listContent,String hidName,HttpServletRequest request,
HttpServletResponse response,String startTime,String endTime) throws Exception{
response.reset();
response.setContentType("APPLICATION/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=/"+getFileName()+".xls");
//设置第一个工作表的名称为name
HSSFWorkbook workbook=new HSSFWorkbook();// 产生工作薄对象-------文档对象
HSSFSheet sheet=workbook.createSheet();// 产生工作表对象---------表单
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)14); //字体高度
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
// 设置单元格类型样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
cellStyle.setWrapText(true);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//行底色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT); //水平布局:居左
cellStyle2.setWrapText(true);
HSSFCellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.setAlignment(HSSFCellStyle.ALIGN_RIGHT); //水平布局:居右
cellStyle3.setFillBackgroundColor(HSSFColor.ORCHID.index);//行底色
cellStyle3.setWrapText(true);
Region region1 =new Region(0,(short)0,0,(short)6);
sheet.addMergedRegion(region1);
Region region2 =new Region(1,(short)0,1,(short)6);
sheet.addMergedRegion(region2);
sheet.setColumnWidth(0, 3766);
sheet.setColumnWidth(1, 4266);
sheet.setColumnWidth(2, 4266);
sheet.setColumnWidth(3, 4266);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
//产生一行
HSSFRow title =sheet.createRow((short)0);
title.setHeightInPoints(24);
HSSFCell cell = title.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(hidName));
HSSFRow dateStr =sheet.createRow((short)1);
HSSFCell cell1 = dateStr.createCell(0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(cellStyle3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//cell1.setCellValue(new HSSFRichTextString("日期:"+sdf.format(new Date())));
cell1.setCellValue(new HSSFRichTextString("查询日期:"+startTime+" - "+endTime));
HSSFRow header=sheet.createRow((short)2);
/** ***************以下是EXCEL第一行列标题********************* */
for (int i = 0; i < Title.length; i++) {
header.createCell((short)i).setCellValue(Title[i]);
}
/** ***************以下是EXCEL正文数据********************* */
int i=3;
for(int m=0;m<listContent.size();m++)
{
HSSFRow row=sheet.createRow(i);
ArrayList<String> rows=listContent.get(m);
int j=0;
for(int r=0;r<rows.size();r++)
{
if(rows.get(r)!=null)
{
row.createCell((short)j).setCellValue(rows.get(r).toString());
}
else
{
row.createCell((short)j).setCellValue("");
}
j++;
}
i=i+1;
}
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
}
public static String getFileName(){
SimpleDateFormat datetime = new SimpleDateFormat("yyyyMMddhhmmssSSS");
Date time = new Date();
String name = datetime.format(time);
return name;
}
}
调用
ExpExcelHelper.exportExcel(title,listContent,"公共邮箱操作日志", req, resp,startTime,endtime);
return null;