`
- 浏览:
824658 次
- 性别:
- 来自:
北京
-
-
importjava.io.IOException;
-
importjava.io.OutputStream;
-
importjava.util.List;
-
importjavax.servlet.http.HttpServletResponse;
-
importorg.apache.poi.hssf.usermodel.HSSFCell;
-
importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
-
importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
-
importorg.apache.poi.hssf.usermodel.HSSFFont;
-
importorg.apache.poi.hssf.usermodel.HSSFRow;
-
importorg.apache.poi.hssf.usermodel.HSSFSheet;
-
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
-
importorg.apache.struts2.ServletActionContext;
-
importcom.neusoft.dcd.css.common.BaseAction;
-
importcom.neusoft.dcd.css.model.management.user.UserBaseInfo;
-
importcom.neusoft.dcd.css.model.tsc.Tsc;
-
importcom.neusoft.dcd.css.service.tsc.TscService;
-
importcom.opensymphony.xwork2.Preparable;
-
-
-
publicclassExportExcel{
-
privatestaticfinallongserialVersionUID=1L;
-
privateTscServicecssTscService;
-
publicvoidprepare()throwsException{
-
}
-
-
-
-
-
-
-
-
-
-
-
-
publicStringinit()throwsException{
-
UserBaseInfouserinfo=(UserBaseInfo)ServletActionContext
-
.getRequest().getSession().getAttribute("UserBaseInfo");
-
if(userinfo!=null){
-
HttpServletResponseresponse=ServletActionContext.getResponse();
-
ListdataList=cssTscService.queryForProTsclb(userinfo.getUserId());
-
HSSFWorkbookworkbook=exportExcel(dataList);
-
if(workbook!=null){
-
this.printExcel(workbook,response,"ExcelTsc.xls");
-
}
-
}
-
returnSUCCESS;
-
}
-
-
-
-
-
privatevoidprintExcel(HSSFWorkbookworkbook,HttpServletResponseresponse,Stringstring)throwsIOException{
-
OutputStreamout=response.getOutputStream();
-
response.setHeader("Content-disposition","attachment;filename="+"TSC.xls");
-
response.setContentType("application/msexcel;charset=UTF-8");
-
workbook.write(out);
-
out.flush();
-
out.close();
-
-
}
-
-
publicHSSFWorkbookexportExcel(ListdataList)throwsException
-
{
-
HSSFWorkbookworkbook=null;
-
try
-
{
-
-
-
-
workbook=newHSSFWorkbook();
-
-
HSSFSheetsheet=workbook.createSheet("TscExcel");
-
-
this.setSheetColumnWidth(sheet);
-
-
HSSFCellStylestyle=this.createTitleStyle(workbook);
-
-
-
if(dataList!=null&&dataList.size()>0)
-
{
-
-
HSSFRowrow=sheet.createRow((short)0);
-
-
this.createCell(row,0,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.asktime"));
-
this.createCell(row,1,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.productname"));
-
this.createCell(row,2,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.passsort"));
-
this.createCell(row,3,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.askusername"));
-
this.createCell(row,4,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.dtype"));
-
this.createCell(row,5,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.askone"));
-
this.createCell(row,6,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.department"));
-
this.createCell(row,7,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.requesttime"));
-
this.createCell(row,8,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.answertime"));
-
this.createCell(row,9,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.requesttow"));
-
-
for(inti=0;i<dataList.size();i++)
-
{
-
-
Tscmodel=(Tsc)dataList.get(i);
-
HSSFRowrow1=sheet.createRow((short)(i+1));
-
if(model.getAsktime()!=null)
-
this.createCell(row1,0,style,HSSFCell.CELL_TYPE_STRING,
-
model.getAsktime());
-
if(model.getProductname()!=null)
-
this.createCell(row1,1,style,HSSFCell.CELL_TYPE_STRING,
-
model.getProductname());
-
if(model.getPasssort()!=null)
-
this.createCell(row1,2,style,HSSFCell.CELL_TYPE_STRING,
-
model.getPasssort());
-
if(model.getAskusername()!=null)
-
this.createCell(row1,3,style,HSSFCell.CELL_TYPE_STRING,
-
model.getAskusername());
-
if(model.getDtype()!=null)
-
this.createCell(row1,4,style,HSSFCell.CELL_TYPE_STRING,
-
model.getDtype());
-
if(model.getAskone()!=null)
-
this.createCell(row1,5,style,HSSFCell.CELL_TYPE_STRING,
-
model.getAskone());
-
if(model.getDepartment()!=null)
-
this.createCell(row1,6,style,HSSFCell.CELL_TYPE_STRING,
-
model.getDepartment());
-
if(model.getRequesttime()!=null)
-
this.createCell(row1,7,style,HSSFCell.CELL_TYPE_STRING,
-
model.getRequesttime());
-
if(model.getAnswertime()!=null)
-
this.createCell(row1,8,style,HSSFCell.CELL_TYPE_STRING,
-
model.getAnswertime());
-
if(model.getRequesttow()!=null)
-
this.createCell(row1,9,style,HSSFCell.CELL_TYPE_STRING,
-
model.getRequesttow());
-
-
}
-
}
-
else{
-
this.createCell(sheet.createRow(0),0,style,
-
HSSFCell.CELL_TYPE_STRING,"查无资料");
-
}
-
}catch(Exceptione)
-
{
-
e.printStackTrace();
-
}
-
returnworkbook;
-
-
}
-
privatevoidsetSheetColumnWidth(HSSFSheetsheet)
-
{
-
-
sheet.setColumnWidth((short)0,(short)3000);
-
sheet.setColumnWidth((short)1,(short)3000);
-
sheet.setColumnWidth((short)2,(short)3000);
-
sheet.setColumnWidth((short)3,(short)3000);
-
sheet.setColumnWidth((short)4,(short)5000);
-
sheet.setColumnWidth((short)5,(short)5000);
-
sheet.setColumnWidth((short)6,(short)5000);
-
sheet.setColumnWidth((short)7,(short)5000);
-
sheet.setColumnWidth((short)8,(short)5000);
-
sheet.setColumnWidth((short)9,(short)5000);
-
}
-
-
privateHSSFCellStylecreateTitleStyle(HSSFWorkbookwb){
-
HSSFFontboldFont=wb.createFont();
-
boldFont.setFontHeight((short)200);
-
HSSFCellStylestyle=wb.createCellStyle();
-
style.setFont(boldFont);
-
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
-
returnstyle;
-
}
-
-
privatevoidcreateCell(HSSFRowrow,intcolumn,HSSFCellStylestyle,
-
intcellType,Objectvalue){
-
HSSFCellcell=row.createCell((short)column);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
if(style!=null){
-
cell.setCellStyle(style);
-
}
-
switch(cellType){
-
caseHSSFCell.CELL_TYPE_BLANK:{
-
}
-
break;
-
caseHSSFCell.CELL_TYPE_STRING:{
-
cell.setCellValue(value.toString());
-
}
-
break;
-
caseHSSFCell.CELL_TYPE_NUMERIC:{
-
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
-
-
-
cell.setCellValue(Double.parseDouble(value.toString()));
-
}
-
break;
-
default:
-
break;
-
}
-
}
-
-
publicvoidsetCssTscService(TscServicecssTscService){
-
this.cssTscService=cssTscService;
-
}
-
-
}
- importjava.io.IOException;
-
importjava.io.OutputStream;
-
importjava.util.List;
-
importjavax.servlet.http.HttpServletResponse;
-
importorg.apache.poi.hssf.usermodel.HSSFCell;
-
importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
-
importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
-
importorg.apache.poi.hssf.usermodel.HSSFFont;
-
importorg.apache.poi.hssf.usermodel.HSSFRow;
-
importorg.apache.poi.hssf.usermodel.HSSFSheet;
-
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
-
importorg.apache.struts2.ServletActionContext;
-
importcom.neusoft.dcd.css.common.BaseAction;
-
importcom.neusoft.dcd.css.model.management.user.UserBaseInfo;
-
importcom.neusoft.dcd.css.model.tsc.Tsc;
-
importcom.neusoft.dcd.css.service.tsc.TscService;
-
importcom.opensymphony.xwork2.Preparable;
-
publicclassExportExcel{
-
privatestaticfinallongserialVersionUID=1L;
-
privateTscServicecssTscService;
-
publicvoidprepare()throwsException{
- }
-
-
publicStringinit()throwsException{
- UserBaseInfouserinfo=(UserBaseInfo)ServletActionContext
-
.getRequest().getSession().getAttribute("UserBaseInfo");
-
if(userinfo!=null){
- HttpServletResponseresponse=ServletActionContext.getResponse();
- ListdataList=cssTscService.queryForProTsclb(userinfo.getUserId());
- HSSFWorkbookworkbook=exportExcel(dataList);
-
if(workbook!=null){
-
this.printExcel(workbook,response,"ExcelTsc.xls");
- }
- }
-
returnSUCCESS;
- }
-
-
privatevoidprintExcel(HSSFWorkbookworkbook,HttpServletResponseresponse,Stringstring)throwsIOException{
- OutputStreamout=response.getOutputStream();
-
response.setHeader("Content-disposition","attachment;filename="+"TSC.xls");
-
response.setContentType("application/msexcel;charset=UTF-8");
- workbook.write(out);
- out.flush();
- out.close();
-
- }
-
publicHSSFWorkbookexportExcel(ListdataList)throwsException
- {
-
HSSFWorkbookworkbook=null;
-
try
- {
-
-
-
-
workbook=newHSSFWorkbook();
-
-
HSSFSheetsheet=workbook.createSheet("TscExcel");
-
-
this.setSheetColumnWidth(sheet);
-
-
HSSFCellStylestyle=this.createTitleStyle(workbook);
-
-
-
if(dataList!=null&&dataList.size()>0)
- {
-
-
HSSFRowrow=sheet.createRow((short)0);
-
-
this.createCell(row,0,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.asktime"));
-
this.createCell(row,1,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.productname"));
-
this.createCell(row,2,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.passsort"));
-
this.createCell(row,3,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.askusername"));
-
this.createCell(row,4,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.dtype"));
-
this.createCell(row,5,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.askone"));
-
this.createCell(row,6,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.department"));
-
this.createCell(row,7,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.requesttime"));
-
this.createCell(row,8,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.answertime"));
-
this.createCell(row,9,style,HSSFCell.CELL_TYPE_STRING,
-
this.getText("css.tsc.requesttow"));
-
-
for(inti=0;i<dataList.size();i++)
- {
-
- Tscmodel=(Tsc)dataList.get(i);
-
HSSFRowrow1=sheet.createRow((short)(i+1));
-
if(model.getAsktime()!=null)
-
this.createCell(row1,0,style,HSSFCell.CELL_TYPE_STRING,
- model.getAsktime());
-
if(model.getProductname()!=null)
-
this.createCell(row1,1,style,HSSFCell.CELL_TYPE_STRING,
- model.getProductname());
-
if(model.getPasssort()!=null)
-
this.createCell(row1,2,style,HSSFCell.CELL_TYPE_STRING,
- model.getPasssort());
-
if(model.getAskusername()!=null)
-
this.createCell(row1,3,style,HSSFCell.CELL_TYPE_STRING,
- model.getAskusername());
-
if(model.getDtype()!=null)
-
this.createCell(row1,4,style,HSSFCell.CELL_TYPE_STRING,
- model.getDtype());
-
if(model.getAskone()!=null)
-
this.createCell(row1,5,style,HSSFCell.CELL_TYPE_STRING,
- model.getAskone());
-
if(model.getDepartment()!=null)
-
this.createCell(row1,6,style,HSSFCell.CELL_TYPE_STRING,
- model.getDepartment());
-
if(model.getRequesttime()!=null)
-
this.createCell(row1,7,style,HSSFCell.CELL_TYPE_STRING,
- model.getRequesttime());
-
if(model.getAnswertime()!=null)
-
this.createCell(row1,8,style,HSSFCell.CELL_TYPE_STRING,
- model.getAnswertime());
-
if(model.getRequesttow()!=null)
-
this.createCell(row1,9,style,HSSFCell.CELL_TYPE_STRING,
- model.getRequesttow());
-
- }
- }
-
else{
-
this.createCell(sheet.createRow(0),0,style,
-
HSSFCell.CELL_TYPE_STRING,"查无资料");
- }
-
}catch(Exceptione)
- {
- e.printStackTrace();
- }
-
returnworkbook;
- }
-
privatevoidsetSheetColumnWidth(HSSFSheetsheet)
- {
-
-
sheet.setColumnWidth((short)0,(short)3000);
-
sheet.setColumnWidth((short)1,(short)3000);
-
sheet.setColumnWidth((short)2,(short)3000);
-
sheet.setColumnWidth((short)3,(short)3000);
-
sheet.setColumnWidth((short)4,(short)5000);
-
sheet.setColumnWidth((short)5,(short)5000);
-
sheet.setColumnWidth((short)6,(short)5000);
-
sheet.setColumnWidth((short)7,(short)5000);
-
sheet.setColumnWidth((short)8,(short)5000);
-
sheet.setColumnWidth((short)9,(short)5000);
- }
-
-
privateHSSFCellStylecreateTitleStyle(HSSFWorkbookwb){
- HSSFFontboldFont=wb.createFont();
-
boldFont.setFontHeight((short)200);
- HSSFCellStylestyle=wb.createCellStyle();
- style.setFont(boldFont);
-
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
-
returnstyle;
- }
-
-
privatevoidcreateCell(HSSFRowrow,intcolumn,HSSFCellStylestyle,
-
intcellType,Objectvalue){
-
HSSFCellcell=row.createCell((short)column);
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
if(style!=null){
- cell.setCellStyle(style);
- }
-
switch(cellType){
-
caseHSSFCell.CELL_TYPE_BLANK:{
- }
-
break;
-
caseHSSFCell.CELL_TYPE_STRING:{
- cell.setCellValue(value.toString());
- }
-
break;
-
caseHSSFCell.CELL_TYPE_NUMERIC:{
- cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
-
-
- cell.setCellValue(Double.parseDouble(value.toString()));
- }
-
break;
-
default:
-
break;
- }
- }
-
publicvoidsetCssTscService(TscServicecssTscService){
-
this.cssTscService=cssTscService;
- }
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
struts2导出excel java 导出excel: JXL_excel: 可以直接在elipse下运行的例子 可以直接导出excel文件(包含所需的jar包) CSharpJExcel: 是JXL的官方下载的 包括详细的文档 和jar包
poi struts2 导出Excel 用jdbc连的sql server2000 非常简单 一看就能明白怎么个回事 就一张表 3个字段 适合第一次用poi的朋友
struts2 导出 excel 可以很好的移植到你的项目中去!
struts2实现excel导出struts2实现excel导出struts2实现excel导出struts2实现excel导出
NULL 博文链接:https://wasw100.iteye.com/blog/576494
运用最简单易懂的方式写了导出的步骤,剔除了样式,更直接易懂,加入了POI必须的jar包,还加入了HSSF的帮助文档,很实用的
Struts1.2导出Excel表完美版
struts2中利用poi导出excel的简单例子
利用struts2和poi把数据导出到excel
整合struts2 ssh导出excel,绝对可以用。
struts1.2+extjs+导出excel项目 struts1.2+extjs+导出excel项目
主要是利用struts2导出excel,格式良好,jar包已添加,可以直接导入运行,有什么问题请联系我
使用JSP、Struts2和jquery实现简单查询,分页,导出到Excel并保存到本地计算机。(数据库相关内容在webroot下db文件夹内)
此文档提供全面的poi导出excel,对于有这方面需要的朋友有很大的帮助。
Struts2+Excel导出Demo,测试可用带全部运行所需的JAR包
本文档详细介绍了struts2整合apache的jxl导入导出excel表格的方法。包括代码,可以直接使用。
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet....
poi-3.10-FINAL-20140208.jar,jxls-core-1.0.5.jar,poi-ooxml-3.10-FINAL-20140208.jar,commons-digester-2.1.jar,commons-jexl-2.1.1.jar 博文地址:http://blog.csdn.net/richerg85/article/details/42459471
利用纯struts2.0+poi插件 实现导出excel
Struts2文件流方式导出下载excel、Txt、image图片,直接返回给浏览器提示下载,不生成临时文件,直接保存在客户端