最近写了一个,Excel 的 写入和导出.   需求是这样的.   在新建合同的时候,会有导出合同的数据,    导出的模板是固定的,,需要在模板里面写入合同的信息.

first   :  下载模板   > 写入数据 > 输出

下载模板  :

 StringBuilder path = new StringBuilder("");
path.append(request.getSession().getServletContext().getRealPath(""));
path.append(File.separator);
path.append("WEB-INF");
path.append(File.separator);
path.append("classes");
path.append(File.separator);
path.append("template");
path.append(File.separator);
String filePath=path.toString()+"\\"+"contractDemo.xlsx"; //这是获取jboss服务器上的模板路径
FileInputStream fis = new FileInputStream(filePath); XSSFWorkbook workBook=new XSSFWorkbook(fis);  // 新建一个workBook  用来新建Excel 的sheet 

//  这个是下载和输出excel      excel写入数据 是在另一个方法里面写着的 ,方法分开来写比较清晰.

     try{       

 StringBuilder path = new StringBuilder("");
path.append(request.getSession().getServletContext().getRealPath(""));
path.append(File.separator);
path.append("WEB-INF");
path.append(File.separator);
path.append("classes");
path.append(File.separator);
path.append("template");
path.append(File.separator);
String filePath=path.toString()+"\\"+"contractDemo.xlsx"; // 服务器上的模板路径
FileInputStream fis = new FileInputStream(filePath); // 输入流
XSSFWorkbook workBook=new XSSFWorkbook(fis);
String fileName="test_"+System.currentTimeMillis()+".xlsx";
OutputStream out=new FileOutputStream("d:/"+fileName);
contractExportTemplate.createSheet(workBook,vo,conf);
workBook.setForceFormulaRecalculation(true);
workBook.write(out);
fis.close();
out.flush();
out.close();
return "success";
}catch(Exception e){
e.printStackTrace();
return "error";
}

  

//    下面的代码其实就是在excel  里面写入数据,我是根据模板来写的,所以sheet 表里面的格式都是固定的.我只要获取具体的单元格然后写入数据就可以了.

//给excel表添加数据
public void excelContractWriteData(XSSFWorkbook workBook,XSSFSheet sheet, List<ContractExcelGroupByStoreVo> conList1,
List<ContractExcelGroupByAreaVo> conList2,List<ContractExcelGroupByStoreVo> conList3) throws Exception{
if(conList1.size()!=0){
XSSFRow row0=sheet.getRow(0); row0.getCell(2).setCellValue(conList1.get(0).getTaskId()+""+
(conList1.get(0).getPrintSeqNo()==null?"1":conList1.get(0).getPrintSeqNo()));//写入打印编号 XSSFRow row=sheet.getRow(2);
//获取sheet表的单元格,写入数据
row.getCell(2).setCellValue(conList1.get(0).getYear());
row.getCell(4).setCellValue(conList1.get(0).getCatlgId());
row.getCell(6).setCellValue(conList1.get(0).getSupNo());
}
if(conList2.size()!=0){
for( ContractExcelGroupByAreaVo vo :conList2){
if(vo.getAreaName()!="" && "华东".equals(vo.getAreaName().toString()))
{
sheet.getRow(6).getCell(2).setCellValue(vo.getStoreNum());
sheet.getRow(6).getCell(3).setCellValue(vo.getTargetNum());
}
if(vo.getAreaName()!="" && "西南".equals(vo.getAreaName().toString()))
{
sheet.getRow(7).getCell(2).setCellValue(vo.getStoreNum());
sheet.getRow(7).getCell(3).setCellValue(vo.getTargetNum());
}
if(vo.getAreaName()!="" && "华北".equals(vo.getAreaName().toString()))
{
sheet.getRow(8).getCell(2).setCellValue(vo.getStoreNum());
sheet.getRow(8).getCell(3).setCellValue(vo.getTargetNum());
}
if(vo.getAreaName()!="" && "华南".equals(vo.getAreaName().toString()))
{
sheet.getRow(9).getCell(2).setCellValue(vo.getStoreNum());
sheet.getRow(9).getCell(3).setCellValue(vo.getTargetNum());
}
if(vo.getAreaName()!="" && "华中".equals(vo.getAreaName().toString()))
{
sheet.getRow(10).getCell(2).setCellValue(vo.getStoreNum());
sheet.getRow(10).getCell(3).setCellValue(vo.getTargetNum());
}
}
}
if(conList3.size()!=0){
int rowIndex=14; //这个数字是根据excel模板定的
for(ContractExcelGroupByStoreVo conExcel : conList3){
sheet.getRow(rowIndex).getCell(1).setCellValue(conExcel.getAreaName());
sheet.getRow(rowIndex).getCell(2).setCellValue(conExcel.getProvinceName());
sheet.getRow(rowIndex).getCell(3).setCellValue(conExcel.getCityName());
sheet.getRow(rowIndex).getCell(4).setCellValue(conExcel.getStoreNum()+"-"+conExcel.getStoreName()); //门店的编号
sheet.getRow(rowIndex).getCell(5).setCellValue(conExcel.getStoreAmount());
sheet.getRow(rowIndex).getCell(6).setCellValue(conExcel.getMemo());
if(conExcel.getServReqd()!=null){
String month= conExcel.getServReqd().toString();
sheet.getRow(rowIndex).getCell(7).setCellValue(this.stringtoIntArray(month));
}else{
sheet.getRow(rowIndex).getCell(7).setCellValue(conExcel.getDateStr());
}
rowIndex++;
}
} }

  

//  这里是一个排序,  月份有 1 2 3 4 5 6 7 8 9 10 11 12      但是每次出现的月份是不固定的   可能中间会有断开的月份.  所以需要先排序然后 判断  第二个数和第一个数相差多少, 如果相差大于2的话就说明 月份中间是有断开的.那就把连续的几个数的头尾  用字符串拼接起来,显示

 //把 月份数组转换为字符串
public String stringtoIntArray(String str) { StringBuffer dateStage= new StringBuffer("");
String strs[] = str.split(",");
int array[] = new int[strs.length];
for(int i=0;i<strs.length;i++){
array[i]=Integer.parseInt(strs[i]);
}
for (int i = 0; i < array.length; i++) {
for(int j = 0; j<array.length-i-1; j++){
if(array[j]>array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
int a=array[0];
for (int j = 1; j < array.length; j++) {
if(array[j] - array[j-1]>=2){
dateStage.append(""+a+"-"+array[j-1]+",");
a=array[j];
}
if(j==(array.length-1)){ dateStage.append(""+a+"-"+array[j]);
}
}
return dateStage.toString(); }

  

java poi 从服务器下载模板写入数据再导出的更多相关文章

  1. NPOI 通过excel模板写入数据并导出

    private void ToExcel(string id) { //模板文件 string TempletFileName = Server.MapPath("template.xls& ...

  2. 02使用java脚本向Pxc集群写入数据

    使用java脚本向Pxc集群写入数据 批量写入pxc集群程序 导入mysql驱动包 # 批量插入数据的java脚本 package pxc_demo; import java.sql.Connecti ...

  3. java io流 创建文件、写入数据、设置输出位置

    java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...

  4. java之初识服务器跨域获取数据

    当一个项目膨大到无法进行整理时,而作为新负责维护的团队是非常苦恼的.对于想实现两个系统的数据访问,使用Ajax数据请求方式获取jsonp格式的数据 需要有前端jquery库文件. 前端代码通过jque ...

  5. java中通过jacob调用dts进行数据导入导出

    在一个项目中需要金蝶软件对接,但是业务服务器和财务服务器相隔很远(中间经过好几台服务器,有内网也有外网),从一个内网向另一个内网中传输时,需要外网辅助,因为不让原始数据受污染,使用了DTS数据同步到另 ...

  6. tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  7. Java实现从服务器下载文件到本地的工具类

    话不多说,直接上代码...... import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServlet ...

  8. java中将list、map对象写入文件

    链接地址:http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html     推荐:凤爪女瓜子男怪象该谁反思伦敦房价为什么持续暴涨 × wvqusrtg个 ...

  9. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

随机推荐

  1. iOS 设置UILable字体,及计算长度和位置

    UILabel *lastLable = [[UILabel alloc] init]; UIFont *font = [UIFont fontWithName:lastLable.font.font ...

  2. Spring 之 配置(Java之负基础实战)

    1.程序加入Spring <?xml version="1.0" encoding="utf-8"?> <web-app xmlns=&quo ...

  3. window 2008+apache2.4.4+php5.5+mysql-5.6.12+phpmyadmin4.0.4.1安装过程(参考他人文章基础上加上自己遇到的问题)

    一.window server2008的安装 1.我用U盘安装的,先用UltraISO把server2008刻录到U盘中,过程我搜了一下,帖个地址: http://wenku.baidu.com/vi ...

  4. IOS开发缓存机制之—本地缓存机制

    功能需求 这个缓存机制满足下面这些功能. 1.可以将数据缓存到本地磁盘. 2.可以判断一个资源是否已经被缓存.如果已经被缓存,在请求相同的资源,先到本地磁盘搜索. 3.可以判断文件缓存什么时候过期.这 ...

  5. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. JavaScript成员属性读取

    var obj = {}; 检索一个不存在的成员属性的值,将返回undefined; 可以使用||运算符来填充默认值: var status = obj.status||'inistatus' 从un ...

  7. P2P直播承载平台与CDN直播承载平台比较

    收看软件不一样:CDN直播收看无需安装第三方收看软件,一般操作系统已带播放器软件:P2P直播收看需要安装厂家自己的播放器软件,每家P2P的软件不兼容,收看者要装多套软件才能收看不同内容. 收看人数不一 ...

  8. 国内值得关注的官方API集合

    项目地址:https://github.com/marktony/Awesome_API 本页仅收集国内部分官方API,如需查看其他版本,请点击这里. 目录 笔记 出行 词典 电商 地图 电影 后端云 ...

  9. redis使用Lua脚本

    最近在看<Redis入门指南>第二版,感觉收获挺大,推荐大家有时间看一看.其中有一章讲Lua脚本,感觉挺实用,把总结整理一下. Redis在2.6中推出了脚本功能,允许开发者使用Lua语言 ...

  10. Web压力测试小工具:webbench、http_load、Siege、ab

    webbench 安装 下载地址:http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz 或者 http://soft.vpser ...