java poi 模板 导出_java poi 从服务器下载模板写入数据再导出

这篇博客介绍了如何使用Java的POI库从服务器下载Excel模板,填充数据后再进行导出。首先获取模板文件路径,创建工作簿,然后在另一方法中写入数据。数据写入过程中,根据模板的固定格式定位单元格并写入内容,包括不同区域的门店数量和目标数量,以及门店详细信息。此外,还涉及到了数据排序和月份断开情况的处理,确保导出的Excel文件符合预期。
摘要由CSDN通过智能技术生成

最近写了一个,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=newFileInputStream(filePath); XSSFWorkbook workBook=newXSSFWorkbook(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 conList1,

List conList2,List 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

array[i]=Integer.parseInt(strs[i]);

}

for (int i = 0; i < array.length; i++) {

for(int j = 0; 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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值