package com.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
import com.service.PersonService;
import com.service.TestService;
@ParentPackage("struts-default")
@Namespace("/excel_out")
@Results( { @Result(name = "success", location = "/main.jsp"),
@Result(name = "error", location = "/error.jsp") })
@ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class ExcelOutputAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private InputStream excelFile;
@SuppressWarnings("unused")
private String downloadFileName;
public String flag;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getDownloadFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
String downloadFileName = (sf.format(new Date()).toString())
+ "用印文件统计.xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),
"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
@Action(value = "indexJson", results = { @Result(name = "success" ,location="/login.jsp" ,params = { })})
public String indexJson() throws Exception{
PrintWriter pWriter = ServletActionContext.getResponse().getWriter();
pWriter.print("D");
pWriter.write("c");
String renpin = ServletActionContext.getRequest().getParameter("rein");
System.out.println(renpin +"SSSSSSSSSSSSSSSSs" );
System.out.println(String.valueOf(1));
pWriter.flush();
return null;
}
@Action(value = "index", results = { @Result(name = "success" ,location="/login.jsp" ,params = { })})
public String index(){
String renpin = ServletActionContext.getRequest().getParameter("renpin");
System.out.println(renpin +"SSSSSSSSSSSSSSSSs" );
TestService testService = new TestService();
testService.getStat();
return SUCCESS;
}
@Action(value = "exportExcel", results = { @Result(name = "success", type = "stream", params = {
"contentType", "application/vnd.ms-excel", "inputName",
"excelFile", "contentDisposition",
"attachment;filename=${downloadFileName}.xls", "bufferSize", "1024" }) })
public String export2() throws Exception {
String name = ServletActionContext.getRequest().getParameter("name");
System.out.println(name);
PersonService ps = new PersonService();
List<String> listHeader = new ArrayList<String>();
listHeader.add("序号");
listHeader.add("用户名");
listHeader.add("密码");
listHeader.add("顺序名顺序名顺序名顺序名");
@SuppressWarnings("unchecked")
List<Object> list = ps.findPersonByCol();
this.setExcel(listHeader,list);
return SUCCESS;
}
private HSSFCellStyle setFont(HSSFWorkbook wk , int size,boolean borderHas){
HSSFCellStyle style = wk.createCellStyle();
HSSFFont f = wk.createFont();
//设置字体
f.setFontHeightInPoints((short) size);//字号
style.setFont(f);
//据中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置边框
if(borderHas){
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
}
return style;
}
@SuppressWarnings("deprecation")
private HSSFWorkbook setExcel(List<String> listHeader,List<Object> listData) throws Exception {
HSSFWorkbook wk = new HSSFWorkbook();
HSSFSheet sheet = wk.createSheet("shuju");
int rows=0;
sheet.addMergedRegion(new CellRangeAddress(0,0,0,22));
HSSFRow row = sheet.createRow(rows++);
row.setHeight((short)800);
HSSFCell cell = row.createCell(0,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("2013个人奖惩信息统计");
cell.setCellStyle(setFont(wk,30,false));
row = sheet.createRow(rows++);
for(int i = 0; i< listHeader.size();i++){
cell = row.createCell(i+5,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(listHeader.get(i));
HSSFCellStyle style = wk.createCellStyle();
HSSFFont f = wk.createFont();
//设置字体
f.setFontHeightInPoints((short) 14);//字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_FILL);
cell.setCellStyle(style);
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
int length = listHeader.get(i).getBytes().length;
sheet.setColumnWidth(i+5,(length*256));
}
if(listData != null && listData.size() > 0){
int k = 1;
for(Object obj:listData){
row = sheet.createRow(rows++);
Object[] objArray = (Object[]) obj;
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(k);
cell.setCellStyle(setFont(wk,11,true));
int column=1;
int rowNum = 6;
while(column < objArray.length+1 ){
cell = row.createCell(rowNum, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(objArray[column-1].toString());
cell.setCellStyle(setFont(wk,11,true));
column++;
rowNum++;
}
k++;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
wk.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
}else{
cell = row.createCell(1,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("数据不存在");
cell.setCellStyle(setFont(wk,30,true));
ByteArrayOutputStream output = new ByteArrayOutputStream();
wk.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
}
return wk;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
import com.service.PersonService;
import com.service.TestService;
@ParentPackage("struts-default")
@Namespace("/excel_out")
@Results( { @Result(name = "success", location = "/main.jsp"),
@Result(name = "error", location = "/error.jsp") })
@ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class ExcelOutputAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private InputStream excelFile;
@SuppressWarnings("unused")
private String downloadFileName;
public String flag;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getDownloadFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
String downloadFileName = (sf.format(new Date()).toString())
+ "用印文件统计.xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),
"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
@Action(value = "indexJson", results = { @Result(name = "success" ,location="/login.jsp" ,params = { })})
public String indexJson() throws Exception{
PrintWriter pWriter = ServletActionContext.getResponse().getWriter();
pWriter.print("D");
pWriter.write("c");
String renpin = ServletActionContext.getRequest().getParameter("rein");
System.out.println(renpin +"SSSSSSSSSSSSSSSSs" );
System.out.println(String.valueOf(1));
pWriter.flush();
return null;
}
@Action(value = "index", results = { @Result(name = "success" ,location="/login.jsp" ,params = { })})
public String index(){
String renpin = ServletActionContext.getRequest().getParameter("renpin");
System.out.println(renpin +"SSSSSSSSSSSSSSSSs" );
TestService testService = new TestService();
testService.getStat();
return SUCCESS;
}
@Action(value = "exportExcel", results = { @Result(name = "success", type = "stream", params = {
"contentType", "application/vnd.ms-excel", "inputName",
"excelFile", "contentDisposition",
"attachment;filename=${downloadFileName}.xls", "bufferSize", "1024" }) })
public String export2() throws Exception {
String name = ServletActionContext.getRequest().getParameter("name");
System.out.println(name);
PersonService ps = new PersonService();
List<String> listHeader = new ArrayList<String>();
listHeader.add("序号");
listHeader.add("用户名");
listHeader.add("密码");
listHeader.add("顺序名顺序名顺序名顺序名");
@SuppressWarnings("unchecked")
List<Object> list = ps.findPersonByCol();
this.setExcel(listHeader,list);
return SUCCESS;
}
private HSSFCellStyle setFont(HSSFWorkbook wk , int size,boolean borderHas){
HSSFCellStyle style = wk.createCellStyle();
HSSFFont f = wk.createFont();
//设置字体
f.setFontHeightInPoints((short) size);//字号
style.setFont(f);
//据中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//设置边框
if(borderHas){
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
}
return style;
}
@SuppressWarnings("deprecation")
private HSSFWorkbook setExcel(List<String> listHeader,List<Object> listData) throws Exception {
HSSFWorkbook wk = new HSSFWorkbook();
HSSFSheet sheet = wk.createSheet("shuju");
int rows=0;
sheet.addMergedRegion(new CellRangeAddress(0,0,0,22));
HSSFRow row = sheet.createRow(rows++);
row.setHeight((short)800);
HSSFCell cell = row.createCell(0,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("2013个人奖惩信息统计");
cell.setCellStyle(setFont(wk,30,false));
row = sheet.createRow(rows++);
for(int i = 0; i< listHeader.size();i++){
cell = row.createCell(i+5,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(listHeader.get(i));
HSSFCellStyle style = wk.createCellStyle();
HSSFFont f = wk.createFont();
//设置字体
f.setFontHeightInPoints((short) 14);//字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_FILL);
cell.setCellStyle(style);
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
int length = listHeader.get(i).getBytes().length;
sheet.setColumnWidth(i+5,(length*256));
}
if(listData != null && listData.size() > 0){
int k = 1;
for(Object obj:listData){
row = sheet.createRow(rows++);
Object[] objArray = (Object[]) obj;
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(k);
cell.setCellStyle(setFont(wk,11,true));
int column=1;
int rowNum = 6;
while(column < objArray.length+1 ){
cell = row.createCell(rowNum, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(objArray[column-1].toString());
cell.setCellStyle(setFont(wk,11,true));
column++;
rowNum++;
}
k++;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
wk.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
}else{
cell = row.createCell(1,HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("数据不存在");
cell.setCellStyle(setFont(wk,30,true));
ByteArrayOutputStream output = new ByteArrayOutputStream();
wk.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
}
return wk;
}
}