public
class
ExportExcel3Action {
@Resource
TotalQueryService totalQueryService;
private
String fileName;
public
InputStream getInputStream()
throws
Exception{
HSSFWorkbook wb =
new
HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(
"sheet1"
);
HSSFRow row = sheet.createRow(
0
);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置对齐方式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
String city=ServletActionContext.getRequest().getParameter(
"city"
);
//获取选取到的值
int
expSize=city.split(
","
).length;
//计算获取到多少个字段
//设置标题行
HSSFCell cell = row.createCell(
0
);
cell.setCellStyle(style);
cell.setCellValue(
"序号"
);
sheet.setColumnWidth(
0
,
2000
);
for
(
int
j =
1
; j <= expSize; j++) {
String[] biaozhi = city.split(
","
);
String lieming = biaozhi[j -
1
].split(
"~"
)[
1
].toString();
// String liezhi = biaozhi[j - 1].split("~")[0].toString();
cell = row.createCell(j);
sheet.setColumnWidth(j,
6000
);
cell.setCellStyle(style);
cell.setCellValue(lieming);
}
UserInfo userInfo =(UserInfo)ServletActionContext.getRequest().getSession().getAttribute(Constants.SessionUser);
String startYear = ServletActionContext.getRequest().getParameter(
"startyearbkxh"
);
String endYear = ServletActionContext.getRequest().getParameter(
"endyearbkxh"
);
String projectTypeName = ServletActionContext.getRequest().getParameter(
"projectTypeNamebkxh"
);
String departmentName = ServletActionContext.getRequest().getParameter(
"unitNamebkxh"
);
String departmentId = ServletActionContext.getRequest().getParameter(
"unitIdbkxh"
);
String state = ServletActionContext.getRequest().getParameter(
"statebkxh"
);
String dir =(String)ServletActionContext.getRequest().getSession().getAttribute(
"dir1"
);
String sort =(String)ServletActionContext.getRequest().getSession().getAttribute(
"sort1"
);
//获取数据
List list2=
new
ArrayList();
if
(startYear==
null
&&endYear==
null
&&projectTypeName==
null
&&departmentId==
null
&&state==
null
){
list2 = totalQueryService.findProjectApplyList(userInfo.getDepBarIds().split(
","
),userInfo,sort, dir);
}
else
{
list2 = totalQueryService.fingProAppBySta(userInfo.getDepBarIds().split(
","
),userInfo,startYear, endYear, projectTypeName, departmentId, state, sort, dir);
}
for
(
int
i =
0
;i
ProjectApply pro = list2.get(i);
row = sheet.createRow(i +
1
);
cell = row.createCell(
0
);
cell.setCellValue(i +
1
);
Map paa = POJO2Map.testReflect(pro);
for
(
int
j =
1
; j <= expSize; j++) {
String[] biaozhi = city.split(
","
);
String liezhi = biaozhi[j -
1
].split(
"~"
)[
0
].toString();
cell = row.createCell(j);
Object object = paa.get((biaozhi[j -
1
]).split(
"~"
)[
0
]);
String cellValueString =
null
;
if
(object !=
null
) {
cellValueString = object.toString();
}
if
(cellValueString !=
null
&& !cellValueString.equals(
""
))
cell.setCellValue(cellValueString);
}
}
Date date =
new
Date();
//定义以随机时间命名的文件名
SimpleDateFormat sd =
new
SimpleDateFormat(
"yyyyMMddHHmmss"
);
fileName = sd.format(date);
File file =
new
File(
"d:/aaa.xls"
);
//创建一个File 拿来当缓存用.也就是先将内存中的excel写入File中.然后再将File转换成输出流
try
{
FileOutputStream os =
new
FileOutputStream(file);
wb.write(os);
os.close();
}
catch
(Exception e) {
e.printStackTrace();
}
FileInputStream inputStream =
null
;;
try
{
inputStream =
new
FileInputStream(file);
System.out.println(inputStream);
}
catch
(Exception e) {
e.printStackTrace();
}
return
inputStream;
}
public
String execute()
throws
Exception{
return
"success"
;
}
public
String getFileName() {
return
fileName;
}
public
void
setFileName(String fileName) {
this
.fileName = fileName;
}
public
TotalQueryService getTotalQueryService() {
return
totalQueryService;
}
public
void
setTotalQueryService(TotalQueryService totalQueryService) {
this
.totalQueryService = totalQueryService;
}
}
配置信息
<
actio n
name
=
"ExportExcel3"
class
=
"cn.sina.andywuchuanlong.exportExcel3Action"
>
<
result
type
=
"stream"
>
<
param
name ="contentType">application/vnd.ms-excel
param
>
<
param
name
=
"inputName"
>inputStream
param
>
<
param
name
=
"contentDisposition"
>attachement;filename="${fileName}.xls"
param
>
<
param
name
=
"bufferSize"
>1024
param
>
result
>
action
>