使用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 ...
随机推荐
- python linux 自动补全 tab.py
#!/usr/bin/env python # python startup file import sys import readline import rlcompleter import ate ...
- Django - 路由层(URLconf)
一.django 静态文件配置 /mysite1/settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_ ...
- Faster R-CNN论文详解 - CSDN博客
废话不多说,上车吧,少年 paper链接:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks ...
- Warm up---hdu4612(缩点,树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥最少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
- 洛谷 P2602 [ZJOI2010]数字计数
洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...
- spring boot由浅入深(二)spring boot基本命令及操作
一 spring常见注解 @RestController和@RequestMapping说明: @RestController.这被称为一个构造型(stereotype)注解.它为阅读代码的人们提供建 ...
- EA类图与代码同步
画了一段时间的图,愈发感觉到EA是一个强大的软件,而不不过一个绘图工具那么简单. . 随着学习时间的延长.如今写程序并不能像曾经一样随心所欲,想到什么就敲什么了,而是要先绘图(也就是理需求和思路的过程 ...
- t分布, 卡方x分布,F分布
T分布:温良宽厚 本文由“医学统计分析精粹”小编“Hiu”原创完成,文章采用知识共享Attribution-NonCommercial-NoDerivatives 4.0国际许可协议(http://c ...
- What do you think the coming adidas NMD Singapore
adidas NMD Singapore is surprising everybody with a lot of completely new NMD choices combined with ...
- NiFi REST API 的使用
一.概述 为方便用户使用 NiFi 进行二次开发,NiFi 为开发者提供了 NIFI REST API. 关于 API 的详细介绍,请参见官网 NIFI REST API.这里仅给出一些注意的小细节. ...