需求:需要导出一个excele,导出包含两部分内容(1.jsp界面table 指定的两个列(非全部) 2.手动绘制的一部分其他内容。)最后要将两部分内容合并成一个新的excele并导出。
框架:SpringMVC(功能跟框架实质没关系,只需要关注一点,将所需要的参数传递到对应方法中或者向需要导出的方法发出请求即可)。
由于是变看POI 文档边做,dome 写的不好,现将优化前dome 贴出作为笔记,供后参考。
前端界面使用标签使用href带参 向Controller发起请求: dome 如下;
模板导出
Controller 后台接收参数做处理,调用模板生成类生成模板:dome 如下;
/***
* 导出配置模板
* **/
@ResponseBody
@RequestMapping(value = "/toExport/{id}/{tempName}",method =RequestMethod.GET)
public void get(@PathVariable("id") String id,@PathVariable("tempName") String tempName,HttpServletRequest request,HttpServletResponse response){
POIExport poi = new POIExport();
MrmCatalogSortAppTemplateBo bo = null;
List descNames = new ArrayList();
List dateTypes = new ArrayList();
try {
//文件设置
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.reset();
response.setContentType("application/x-msexcel");
String fileName = URLEncoder.encode(tempName+"藏品配置模板", "utf-8");
response.addHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xls");
bo = cataSortAppTeService.getByTreeId(id);
for( int j =0 ;j parentNodeOperations = cataSortAppTeService.parentNodeOperations(bo.getParentId());
for(int i=0;i
模板生成类dome;
package com.wonders.framework.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.omg.CORBA.DATA_CONVERSION;
/***
* 配置标本模板导出类
* **/
public class POIExport {
//第一列字段
private static String[] titleOne = {"总登记号","名称","完残情况","尺寸","重量","流传经过","备注"};
private static String[] titleTwo = {"分账类型","登记日期","年代"};//第二列字段
private static String[] titleThr = {"分类号","实际数量","级别","质地"};//第三列字段
private static String[] titleFou = {"件数"};
private static String[] levelList = {"三级","二级","一级","珍贵","一般","未定级","其他"};//级别下拉列表
private static String[] wanCan = {"完整","基本完整","微残","重残","缺","失"};//完残情况
private static String[] chiCun_Type = {"长-宽-高","口径-底径-高","纵-横","其他"}; //尺寸类型 (长-宽-高)
private static String[] chiCun_fixe = {"cm","m"};//尺寸单位(cm)
private static String[] weight_fixe = {"g","kg"};//重量单位
private static String[] count_fixe = {"条","具","颗","只","件","个","盒","套","包"};//件数单位
private static String[] select_type = {"值1","值2","值3","值4"};
public void CreateExcelFile(String id,String tempName,List descNames,List dateTypes,
HttpServletResponse response) throws IOException{
HSSFRow rowsCata = null;//藏品所有行变量
HSSFRow rowsSpen = null;//配置页面所有行
HSSFCell cells1 = null;
HSSFCell cells2 = null;
HSSFCell cells3 = null;
HSSFCell cells4 = null;
HSSFCell cells0 = null;
HSSFCell cells5 = null;
String[] descName = descNames.toArray(new String[descNames.size()]);
String[] dateType = dateTypes.toArray(new String[dateTypes.size()]);
String filePath="d:\\cata_specimen.xls";//文件路径
HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook)
HSSFSheet sheet = workbook.createSheet();//创建工作表(Sheet)对象
HSSFCellStyle style = workbook.createCellStyle(); //创建单元格样式
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)12);//设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
/***表头行设置***/
HSSFRow row_title = sheet.createRow(0); //得到表的行(第一行)
HSSFCell cell_title = row_title.createCell(0);//得到Excel工作表指定行的单元格
//设置单元格的宽 一个字符宽度为256 25个字符宽度
cell_title.setCellValue(tempName + "分账类型模板");
cell_title.setCellStyle(style);
row_title.setHeight((short) ((short)3*256));//设置单元格的高度
/*****添加表格*****/
for(int i=0;i<20;i++) {
rowsCata = sheet.createRow(i+1);
if(i == 5 || i == 6){
sheet.setColumnWidth((short)i,20*256); //设置单元格的宽 一个字符宽度为256 25个字符宽度
rowsCata.setHeight((short) ((short)4*256));//设置单元格的高度
}else{
sheet.setColumnWidth((short)i,20*256); //设置单元格的宽 一个字符宽度为256 25个字符宽度
rowsCata.setHeight((short) ((short)2*256));//设置单元格的高度
}
cells1 = rowsCata.createCell(0);
cells0 = rowsCata.createCell(1);
cells2 = rowsCata.createCell(2);
cells3 = rowsCata.createCell(4);
cells4 = rowsCata.createCell(3);
if(i < titleOne.length ) {
if(i == 4) {
cells1.setCellValue(titleOne[i]);
cells4.setCellValue(titleFou[0]);
cells4.setCellStyle(style);
}else {
cells1.setCellValue(titleOne[i]);
}
cells1.setCellStyle(style);
}
if(i < titleTwo.length) {
if(i == 0){
cells4.setCellValue(tempName);
}
cells2.setCellValue(titleTwo[i]);
cells2.setCellStyle(style);
}
if(i < titleThr.length ) {
cells3.setCellValue(titleThr[i]);
cells3.setCellStyle(style);
}
}
if(descName.length>0){
int rowNum = rowNum(descName.length,3);//获取配置表行数;
int yuShu = descName.length%3; //余数
for( int j = titleOne.length + 1;j < titleOne.length + rowNum + 1;j++){
rowsSpen = sheet.createRow(j);
sheet.setColumnWidth((short)j,20*256); //设置单元格的宽 一个字符宽度为256 25个字符宽度
rowsSpen.setHeight((short) ((short)2*256));//设置单元格的高度
cells0 = rowsSpen.createCell(0);//第一列
cells1 = rowsSpen.createCell(1);//第二列
cells2 = rowsSpen.createCell(2);//第三列
cells3 = rowsSpen.createCell(3);//第四列
cells4 = rowsSpen.createCell(4);//第五列
cells5 = rowsSpen.createCell(5);//第六列
//titleOne.length + rowNum + 1 == descName.length%3+7
if(titleOne.length + rowNum == j && yuShu != 0){
for(int x = 0; x0?count/num+1:count/num;
return rowNum;
}
/***根据数据类型返回对应数据单元*******/
public Object getDateType(String type){
if("select".equals(type)){
}
if("date".equals(type)){
}
if("string".equals(type)){
}
if("number".equals(type)){
}
return null;
}
}
页面内容(红框内为需要生成到excele中的内容):
生成模板展示;