使用POI动态更新导出的EXCEL模板中的列
基本思路:
1.从附件服务器上取得模板的流文件
2.拿到流文件之后再使用workbook.write(outs);方法改变流文件中的数据。
else if (pageContext.getParameter("Export") != null) {
OraclePreparedStatement oraclepreparedstatement;
OracleResultSet oracleresultset;
oraclepreparedstatement = null;
oracleresultset = null;
String attachmentId = null;
int count = 0;
try {
oraclepreparedstatement =
(OraclePreparedStatement)EditImportDataAM.getOADBTransaction().createPreparedStatement("SELECT COUNT(1)\n" +
" FROM mtl_categories_b_kfv\n" +
" WHERE category_id IN (SELECT category_id FROM cux_pon_categorys WHERE auction_header_id = '"+auctionHeaderId+"')",
1);
oracleresultset = (OracleResultSet)oraclepreparedstatement.executeQuery();
if (oracleresultset.next()) {
count = oracleresultset.getInt(1);
}
} catch (SQLException e) {
}
if(count==0){
throw new OAException("CUX","CUX_CATEGORY_DEMAND_NOT_NULL",null,OAException.ERROR,null);
}
try {
oraclepreparedstatement =
(OraclePreparedStatement)EditImportDataAM.getOADBTransaction().createPreparedStatement(" select cat.id from cux_attachment_t cat where cat.source_type='PON' and cat.source_table = 'PON_AUCTION_ITEM_TEMPLETE' and cat.source_id='-99999' and cat.file_status = 'C'",
1);
oraclepreparedstatement.defineColumnType(1, -5);
oracleresultset =
(OracleResultSet)oraclepreparedstatement.executeQuery();
if (oracleresultset.next()) {
attachmentId =
(new StringBuilder()).append("").append(oracleresultset.getLong(1)).toString();
} else {
throw new OAException("不存在定价行导入模板");
}
} catch (Exception exception2) {
throw OAException.wrapperException(exception2);
}
LogUtil.of(attachmentId, pageContext).print(pageContext);
downloadFileFromServer(pageContext, webBean, attachmentId);
// exportModeExcel(pageContext, webBean, attachmentId);
return;
}
public void downloadFileFromServer(OAPageContext pageContext,
OAWebBean webBean,
String attachmentId) {
OAApplicationModule am = pageContext.getApplicationModule(webBean);
AttachementVOImpl AttachementVO =
(AttachementVOImpl)am.findViewObject("AttachementVO");
if (AttachementVO == null) {
AttachementVO =
(AttachementVOImpl)am.createViewObject("AttachementVO",
"cux.oracle.apps.cux.attachement.server.AttachementVO");
}
AttachementVO.setWhereClause(null);
AttachementVO.setWhereClauseParams(null);
String sql = "1=1 and ID='" + attachmentId + "'";
AttachementVO.setWhereClause(sql);
AttachementVO.setMaxFetchSize(-1);
AttachementVO.executeQuery();
AttachementVORowImpl attachementInfo =
(AttachementVORowImpl)AttachementVO.first();
HttpServletResponse response =
(HttpServletResponse)pageContext.getRenderingContext().getServletResponse();
String fileType =
attachementInfo.getFileMimetype();
response.setContentType(fileType);
String fileName = attachementInfo.getFileName();
try {
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("GBK"),
"ISO-8859-1"));
response.setHeader("Content-Transfer-Encoding", "binary");
// response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "public");
} catch (UnsupportedEncodingException e) {
LogUtil.of("downloadFileFromServer from FTP server Error-filename!",
this);
}
InputStream in = null;
ServletOutputStream outs = null;
FtpUtil ftp = this.getFtp(pageContext);
try {
outs = response.getOutputStream();
//获取附件流文件
in = ftp.getFile(attachementInfo.getFilePath(), attachementInfo.getFileName());
// int ch;
// while ((ch = in.read()) != -1) {
// outs.write(ch);
// }
// Workbook workbook = new XSSFWorkbook(in);
// org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(1);
// org.apache.poi.ss.usermodel.Row row = null;
// org.apache.poi.ss.usermodel.Cell cell = null;
//
// String cellValue = "132700002800";
//
// for (int i = 0; i < 5; i++) {
// System.out.println(" i =" + i);
// row = sheet.createRow(i);
// cell = row.createCell(0);
// cell.setCellValue(cellValue);
// }
// workbook.write(outs);
exportExcel(pageContext,in, outs);
} catch (IOException e) {
LogUtil.of("downloadFileFromServer from FTP server Error-!" +
e.getMessage(), this);
} finally {
try {
outs.flush();
outs.close();
if (in != null) {
in.close();
}
ftp.closeClient();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取FTP配制信息,并初始化连接
* @param pageContext
* @return
*/
protected FtpUtil getFtp(OAPageContext pageContext) {
String host = pageContext.getProfile("CUX_SRM_FTP_HOST");
String port = pageContext.getProfile("CUX_SRM_FTP_PORT");
String user = pageContext.getProfile("CUX_SRM_FTP_USER");
String password = pageContext.getProfile("CUX_SRM_FTP_PASSWORD");
return new FtpUtil(host, port, user, password);
}
public void exportExcel(OAPageContext pageContext,InputStream ins,OutputStream outs){
try {
OAApplicationModule am = pageContext.getRootApplicationModule();
OAApplicationModule EditImportDataAM = null;
EditImportDataAM =
(OAApplicationModule)am.findApplicationModule("EditImportDataAM");
OAViewObject AuctionHeadersAllVO =
(OAViewObject)am.findViewObject("AuctionHeadersAllVO");
AuctionHeadersAllVORowImpl AuctionHeadersAllRow =
(AuctionHeadersAllVORowImpl)AuctionHeadersAllVO.first();
Number auctionHeaderId = AuctionHeadersAllRow.getAuctionHeaderId();
OAViewObject itemSegVO =
(OAViewObject)EditImportDataAM.findViewObject("CuxAucItemSegmentsVO1");
itemSegVO.setWhereClause(null);
itemSegVO.setWhereClauseParams(null);
itemSegVO.setWhereClauseParam(0, auctionHeaderId);
itemSegVO.executeQuery();
RowSetIterator itemSegIter =
itemSegVO.findRowSetIterator("itemSegIter") == null ?
itemSegVO.createRowSetIterator("itemSegIter") :
itemSegVO.findRowSetIterator("itemSegIter");
Row itemSegRow = null;
int itemSegCount = itemSegVO.getRowCount();
Workbook workbook = new XSSFWorkbook(ins);
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(1);
org.apache.poi.ss.usermodel.Row row = null;
org.apache.poi.ss.usermodel.Cell cell = null;
if (itemSegCount > 0) {
itemSegIter.setRangeStart(0);
itemSegIter.setRangeSize(itemSegCount);
for (int i = 0; i < itemSegCount; i++) {
itemSegRow = itemSegIter.getRowAtRangeIndex(i);
String itemDescription =
(String)itemSegRow.getAttribute("Description");
String itemNumber =
(String)itemSegRow.getAttribute("ItemNumber");
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellValue(itemDescription);
cell = row.createCell(1);
cell.setCellValue(itemNumber);
}
}
itemSegIter.closeRowSetIterator();
workbook.write(outs);
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outs.flush();
outs.close();
if (ins != null) {
ins.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用POI动态更新导出的EXCEL模板中的列的更多相关文章
- C# 导出数据到Excel模板中(转)
今天做报表的时候遇到了多表头的问题,而且相应的报表的格式都一样.所以就采用了报表模板的方式来进行. 第一步:在开发的当前项目中引入:Microsoft.Office.Interop.Excel:Sys ...
- 替换excel模板中的内容并使用JavaMail发送邮件
由于在公司工作,常年出差,每天都要以日报的形式向公司汇报当天的工作内容.而日报的内容大体上就只有当天工作的主要内容时变化的,其余的都是不变 的. 而我的电脑刚打开excel有点卡,因此决定使用Java ...
- 动态更新Toolbar Menu以及Menu中同时显示文字和图标
动态更新Toolbar Menu以及Menu中同时显示文字和图标 我们经常会有这样的需求,在切换Fragment或者点击某个按钮后动态更新Toolbar上Menu项.但是onCreateOptions ...
- JAVA POI替换EXCEL模板中自定义标签(XLSX版本)满足替换多个SHEET中自定义标签
个人说明:为了简单实现导出数据较少的EXCEL(根据自定义书签模板) 一.替换Excel表格标签方法```/** * 替换Excel模板文件内容 * @param map * 需要替换的标签建筑队形式 ...
- SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text
csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...
- java中的数据导出到Excel表中
整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...
- PHP 导出到Excel表格中
/** * 导出excel * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception * @throws \PHPExcel ...
- 从数据库的表导出到Excel表格中【让客户端下载的Excel】
原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 这个例子是从gridview中导出到Excel,可以举一反三,可以直接从数据库中取值放在DataSet中,然后再从 ...
- 使用python读取京东pdf发票信息导出到excel表格中
代码 #!/usr/bin/env python # -*- coding: utf-8 -*- """ pip install pdfminer3k pip insta ...
随机推荐
- slave have equal MySQL Server UUIDs原因及解决
最近在部署MySQL主从复制架构的时候,碰到了"Last_IO_Error: Fatal error: The slave I/O thread stops because master a ...
- jQuery change事件
定义和用法 当元素的值发生改变时,会发生 change 事件. 该事件仅适用于文本域(text field),以及 textarea 和 select 元素. change() 函数触发 change ...
- HTTP 请求头中的 X-Forwarded-For(转)
原文:https://imququ.com/post/x-forwarded-for-header-in-http.html 我一直认为,对于从事 Web 前端开发的同学来说,HTTP 协议以及其他常 ...
- SQL基础--查询之四--集合查询
SQL基础--查询之四--集合查询
- centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课
centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...
- Openstack(十七)部署快存储cinder
官方部署文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/cinder.html OpenStack的存储组件—Cinder和Sw ...
- PHP生成唯一RequestID类
https://blog.csdn.net/fdipzone/article/details/79939431 本文介绍PHP生成唯一RequestID类,使用session_create_id()与 ...
- hdu5880 Family View
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5880 题目: Family View Time Limit: 3000/1000 MS (Ja ...
- 生成TPC-H数据集
下载tpc-h tool 版本有点老,2.14.3,够用了. 在解压的文件夹下面cd到dbgen下,找到makefile.suite. ~/tpch_2_14_3$ cd dbgen~/tpch_2_ ...
- 【知识总结】CSS中样式覆盖优先顺序
层叠样式类型 类型: 1. 浏览器默认样式 2. 浏览器用户自定义样式 3. 外部样式表 4. 内部样式表 5. 内联样式表 顺序: 浏览器默认样式 < 浏览器用户自定义样式 < 外部样式 ...