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<ProjectApply> list2=
new
ArrayList<ProjectApply>();
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<list2.size();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
>
本文出自 “想要跨越,就要付出更多” 部落格,請務必保留此出處http://andywuchuanlong.blog.51cto.com/4898493/1303529