您的位置:首页 > 其它

excel 填充内容然后下载

2014-02-18 11:14 55 查看

public String downloadBondExcel() throws Exception {
RequestUser ru = RequestContext.getCurrentUser();
//获取查询条件
InvestPoolAdjustLogSearchCondition investPoolAdjustLogSearchCondition = searchConditionRisk.get(ru.getOrgId());
Map <String,String> poolNameMap = new HashMap<String,String>();
String filePath = request.getRealPath("/") + "investpool\\templateconfigxml\\template.xlsx";
WritableWorkbook workbook = null;
//获取选择的日志记录id 多个用,分隔
String objids = request.getParameter("objids");
String pids = "";
Map <Integer,List<IInvestPoolAdjustLog>> maplist = new HashMap<Integer,List<IInvestPoolAdjustLog>>();
if(objids != null ){
for(String objid:objids.split(",")){
IInvestPoolAdjustLog investPoolAdjustLog = InvestPoolServiceFactory.getIInvestPoolAdjustLogService().getInvestPoolAdjustLog(NumberTool.safeToInteger(objid,0));
if(investPoolAdjustLog != null){
List<IInvestPoolAdjustLog> investPoolAdjustLogs = InvestPoolServiceFactory.getIInvestPoolAdjustLogService().findInvestPoolAdjustLogsByBatchCodeToUuid(investPoolAdjustLog.getSCode(),investPoolAdjustLog.getMktCode(),investPoolAdjustLog.getStockType(),investPoolAdjustLog.getBatchCode());
for(IInvestPoolAdjustLog iinvestPoolAdjustLog :investPoolAdjustLogs){
if(iinvestPoolAdjustLog.getAdjustType()==1 || iinvestPoolAdjustLog.getAdjustType() == 2){
if(investPoolAdjustLog.getApproveStatus()< 0){
continue;
}
}
if(maplist.get(iinvestPoolAdjustLog.getPoolId()) == null){
pids += iinvestPoolAdjustLog.getPoolId()+",";
List<IInvestPoolAdjustLog> logList = new ArrayList<IInvestPoolAdjustLog>();
logList.add(iinvestPoolAdjustLog);
maplist.put(iinvestPoolAdjustLog.getPoolId(),logList);
}else{
List<IInvestPoolAdjustLog> logList = maplist.get(iinvestPoolAdjustLog.getPoolId());
logList.add(iinvestPoolAdjustLog);
maplist.put(iinvestPoolAdjustLog.getPoolId(),logList);
}
}
}
}
}else {
return null;
}
// List<IInvestPoolAdjustLog> _IInvestPoolAdjustLogs = InvestPoolServiceFactory.getIInvestPoolAdjustLogService().findInvestPoolAdjustLogsByBatchCodeToUuid();

int sheetNO = 0;
int k= 1;
String poolids[] = pids.split(",");
// if(investPoolAdjustLogSearchCondition != null && investPoolAdjustLogSearchCondition.getPoolMessage()!=null&& investPoolAdjustLogSearchCondition.getPoolMessage().length() > 0){
// poolids = investPoolAdjustLogSearchCondition.getPoolMessage().split(",");
// }
//如果当前没有查询条件
// if(poolids == null){
// List<String> ids = OrgServiceFactory.getRightService().findAuthedObjects(String.valueOf(ru.getOrgId()), IInvestPool.ENTITY_NAME, "READ");
// if (ids != null && ids.size() > 0) {
// poolids = new String[ids.size()];
// int j = 0;
// for (String id : ids) {
// poolids[j++] = id;
// }
// }
// }
//循环当前用户选择的池,如果没有选择的池,就 把当前用户有权限查询的池列出来。
for(String poolid:poolids){
IInvestPool investPool = InvestPoolServiceFactory.getInvestPoolService().getInvestPool(Integer.parseInt(poolid));
if(investPool == null){
continue;
}
// String name = InvestPoolServiceFactory.getInvestPoolService().findFullPathNameTwo("",Integer.parseInt(poolid),"_");

if(maplist != null){
List<IInvestPoolAdjustLog> lists = maplist.get(investPool.getId());
if(lists != null && lists.size()>0){
String dname = investPool.getName();
if(dname.length()>28){
dname = dname.substring(0,28);
}
if(poolNameMap.get(dname) != null){
dname+="_"+(k++);
}else{
poolNameMap.put(dname,"1");
}
if(workbook == null){
OutputStream os = new FileOutputStream(filePath);
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setEncoding("GBK"); //关键代码,解决中文乱码
workbook = Workbook.createWorkbook(os, workbookSettings);
}
WritableSheet sheet = workbook.createSheet(dname, sheetNO++);
Label label;
//Label(列号,行号 ,内容 )
label = new Label(0, 0, "证券名称");
sheet.addCell(label);
label = new Label(1, 0, "证券代码");
sheet.addCell(label);
label = new Label(2, 0, "操作类型");
sheet.addCell(label);
label = new Label(3, 0, "市场名称");
sheet.addCell(label);
label = new Label(4, 0, "备注");
sheet.addCell(label);
label = new Label(5, 0, "有效开始时间");
sheet.addCell(label);
label = new Label(6, 0, "有效截止时间");
sheet.addCell(label);
int m = 1;
ISecurityService iSecurityService = SpringFactory.getBean(ISecurityService.class);
for(int i=1;i <= lists.size();i++){
IInvestPoolAdjustLog investPoolAdjustLog =lists.get(i-1);
String scode = investPoolAdjustLog.getSCode();
int stype = investPoolAdjustLog.getStockType();
int mktcode = investPoolAdjustLog.getMktCode();
int adjustmode = investPoolAdjustLog.getAdjustMode();
Security security = iSecurityService.getSecurity(scode, stype, mktcode);
if(security == null){
continue;
}
label = new Label(0, m, security.getSname());
sheet.addCell(label);
label = new Label(1, m, security.getScode());
sheet.addCell(label);
if(adjustmode == 1){
label = new Label(2, m, "增加");
}else{
label = new Label(2, m, "删除");
}

sheet.addCell(label);
if(security.getMktcode()==4){
label = new Label(3, m, "5");
}else{
label = new Label(3, m, security.getMktcode()+"");
}
sheet.addCell(label);
label = new Label(4, m, "");
sheet.addCell(label);
label = new Label(5, m, "0");
sheet.addCell(label);
label = new Label(6, m++, "0");
sheet.addCell(label);
}
}
}
}
if(workbook != null){
workbook.write();
workbook.close();
InputStream in = null;
//File file = new File(filePath);
in = new FileInputStream(filePath);
HttpUtils.download(this.response, "投资池导出_" + TimeUtil.formatDate(new Date(), "yyyy-MM-dd_HH-mm-ss") + ".xls", in);
return "";
}

return "";
}



二、



public String downExcelTree() throws Exception{
String poolids[] = request.getParameterValues("rucheck");
String exportPos = request.getParameter("exportPos");
String error = "";
if(poolids != null){
String filePath = request.getRealPath("/") + "investpool\\templateconfigxml\\template.xlsx";
IOrgService _service = OrgServiceFactory.getOrgService();
if(exportPos != null && exportPos.equals("2")){
for(String poolid:poolids){
IInvestPool investPool =InvestPoolServiceFactory.getInvestPoolService().getInvestPool(Integer.parseInt(poolid));
if(investPool == null || investPool.getExportPath() == null ){
continue;
}

String name = InvestPoolServiceFactory.getInvestPoolService().findFullPathNameTwo("",Integer.parseInt(poolid),"_");
if(investPool.getExportPath().equals("")){
error += name +"导出失败,没有导出路径<br/>";
continue;
}

try{
File file = new File(investPool.getExportPath());
if(file == null && !file.isDirectory()){
error += name +"导出失败,导出路径错误<br/>";
continue;
}
}catch (Exception e){continue;}

IMetaDBQuery iMetaDBQuery =InvestPoolServiceFactory.getIInvestPoolStatusService().queryInvestPoolStatus(Integer.parseInt(poolid));
if(iMetaDBQuery != null ){
List<IInvestPoolStatus>lists = iMetaDBQuery.getResult();
if(lists != null){
WritableWorkbook workbook;
OutputStream os = new FileOutputStream(filePath);
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setEncoding("GBK"); //关键代码,解决中文乱码
workbook = Workbook.createWorkbook(os, workbookSettings);
String dname = name;
if(dname.length()>30){
dname = dname.substring(0,30);
}
WritableSheet sheet = workbook.createSheet(dname, 0);
Label label;
//Label(列号,行号 ,内容 )
label = new Label(0, 0, "证券名称");
sheet.addCell(label);
label = new Label(1, 0, "证券代码");
sheet.addCell(label);
label = new Label(2, 0, "市场代码");
sheet.addCell(label);
label = new Label(3, 0, "证券种类");
sheet.addCell(label);
label = new Label(4, 0, "调整人");
sheet.addCell(label);
label = new Label(5, 0, "调整时间");
sheet.addCell(label);
int i = 1;
ISecurityService _ISecurityService = (ISecurityService) SpringFactory.getBean( ISecurityService.class);
for(IInvestPoolStatus investPoolStatus : lists){
Employee employee = _service.getEmployeeById(investPoolStatus.getInputId()+"");
String _personname = "系统";
if (null != employee) {
_personname = employee.getEmpName();
}
Security _Security = _ISecurityService.getSecurity(investPoolStatus.getSCode(), investPoolStatus.getStockType(), investPoolStatus.getMktCode());
if(_Security != null){
label = new Label(0, i, _Security.getSname());
}else{
label = new Label(0, i, "");
}

sheet.addCell(label);
label = new Label(1, i, investPoolStatus.getSCode());
sheet.addCell(label);
Number number = new Number(2, i, investPoolStatus.getMktCode());
sheet.addCell(number);
number = new Number(3, i, investPoolStatus.getStockType());
sheet.addCell(number);
label = new Label(4, i, _personname);
sheet.addCell(label);
label = new Label(5, i++, TimeUtil.formatDate(investPoolStatus.getSubmitTime(),"yyyyMMddHHmmss"));
sheet.addCell(label);
}
workbook.write();
workbook.close();
try{
FileInputStream in = new FileInputStream(new File(filePath));
String path = investPool.getExportPath()+name+"_"+TimeUtil.formatDate(new Date(),"yyyy-MM-dd_HH-mm-ss")+".xls";
File myFilePath = new File(path);

FileOutputStream fops = new FileOutputStream(myFilePath);
byte[] buffer = new byte[1444];
int byteread = 0;
while ( (byteread = in.read(buffer)) > 0) {
fops.write(buffer, 0, byteread);
}
fops.close();
in.close();
error += name +"导出成功,导出服务器地址:"+path+"<br/>";
}catch (Exception e){}
}
}
}
}else{
//插入
WritableWorkbook workbook;
OutputStream os = new FileOutputStream(filePath);
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setEncoding("GBK"); //关键代码,解决中文乱码
workbook = Workbook.createWorkbook(os, workbookSettings);
int sheetNO = 0;
for(String poolid:poolids){
String name = InvestPoolServiceFactory.getInvestPoolService().findFullPathNameTwo("",Integer.parseInt(poolid),"_");
IMetaDBQuery iMetaDBQuery =InvestPoolServiceFactory.getIInvestPoolStatusService().queryInvestPoolStatus(Integer.parseInt(poolid));
if(iMetaDBQuery != null && iMetaDBQuery.getResult() != null){
List<IInvestPoolStatus>lists = iMetaDBQuery.getResult();
if(lists != null){
String dname = name;
if(dname.length()>30){
dname = dname.substring(0,30);
}
WritableSheet sheet = workbook.createSheet(dname, sheetNO++);
Label label;
//Label(列号,行号 ,内容 )
label = new Label(0, 0, "证券名称");
sheet.addCell(label);
label = new Label(1, 0, "证券代码");
sheet.addCell(label);
label = new Label(2, 0, "市场代码");
sheet.addCell(label);
label = new Label(3, 0, "证券种类");
sheet.addCell(label);
label = new Label(4, 0, "调整人");
sheet.addCell(label);
label = new Label(5, 0, "调整时间");
sheet.addCell(label);
int i = 1;
ISecurityService _ISecurityService = (ISecurityService) SpringFactory.getBean( ISecurityService.class);
for(IInvestPoolStatus investPoolStatus : lists){
Employee employee = _service.getEmployeeById(investPoolStatus.getInputId()+"");
String _personname = "系统";
if (null != employee) {
_personname = employee.getEmpName();
}
Security _Security = _ISecurityService.getSecurity(investPoolStatus.getSCode(), investPoolStatus.getStockType(), investPoolStatus.getMktCode());
if(_Security != null){
label = new Label(0, i, _Security.getSname());
}else{
label = new Label(0, i, "");
}
sheet.addCell(label);
label = new Label(1, i, investPoolStatus.getSCode());
sheet.addCell(label);
Number number = new Number(2, i, investPoolStatus.getMktCode());
sheet.addCell(number);
number = new Number(3, i, investPoolStatus.getStockType());
sheet.addCell(number);
label = new Label(4, i, _personname);
sheet.addCell(label);
label = new Label(5, i++, TimeUtil.formatDate(investPoolStatus.getSubmitTime(),"yyyyMMddHHmmss"));
sheet.addCell(label);
}
}
}
}
workbook.write();
workbook.close();
InputStream in = null;
//File file = new File(filePath);
in = new FileInputStream(filePath);
HttpUtils.download(this.response, "投资池导出_"+TimeUtil.formatDate(new Date(),"yyyy-MM-dd_HH-mm-ss")+".xls", in);
}
}
request.setAttribute("error",error);
return "investpooldownexceltree";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐