poi大数据excle文件下载

首先将poi的jar导入到项目中,ssm直接在xml中配置

package com.aspire.birp.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


import com.aspire.birp.modules.smartQuery.base.constant.FileProperties;


/**
 * excel导出功能 描述:利用poi实现,默认第一行第二列开始记录标题, 第二行需模版提供每列的字段名, 数据默认由第三行开始录入
 * 
 * @ClassName ExcelManager
 * @author cuiwl
 * @date Nov 12, 2014
 */
public class ExcelManager {


private static final transient Log log = LogFactory.getLog(ExcelManager.class);


/** 表单标题 */
private String title;


/** 标题记录行位置 默认第1行 */
private int title_position_x = 0;


/** 标题记录列位置 默认第1列 */
private int title_position_y = 0;


/** 数据开始记录行位置 默认第3行(模版约定第二行字段名) */
private int data_index_position_x = 3;


/** 数据字段名行位置(必须与模版约束同步) 模版约束为第2行 */
public static final int FEILD_INDEX_X = 1;


/** 模版文件名(模版位置必须是excelTemplate下xls格式文件,poi版本问题暂只支持xls后缀文件) */
private String templateFileName = "DefaultTmp.xls";


/** 模版文件路径 */
private String templateFilePath = SysConstants.EXCEL_TEMPLATE_PATH;


/**
* 根据excel模版导出数据 注:暂只支持1个sheet,字段名约束写在第二列,其他信息写在第4行
* 模版必须位置为excelTemplate下xls格式文件

* @param request
* @param response
* @author cuiwl
* @throws Exception
* @date Sep 26, 2014
*/
public void exportExcelByTemplate(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map,
String[] paramNs) throws Exception {
 
try {
long beginTime = System.currentTimeMillis();
//** 文件操作 *//*
String path = request.getSession().getServletContext().getRealPath("//") + this.getTemplateFilePath()
+ this.getTemplateFileName(); // excel模板
// TODO cuiwl 路径验证(可扩展)
InputStream in = new FileInputStream(new File(path));
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd_HHmmss");


//** excel元素操作 *//*
HSSFWorkbook work = new HSSFWorkbook(in);
HSSFSheet sheet = work.getSheetAt(0);
HSSFRow feildRow = sheet.getRow(ExcelManager.FEILD_INDEX_X);
HSSFCellStyle columnStyle = null;


// 记录标题,默认在第一行第二列 start 
HSSFRow row = sheet.createRow(this.getTitle_position_x());
HSSFCell cell = row.createCell(this.getTitle_position_y());
if (!StringUtils.isBlank(this.getTitle())) {
cell.setCellValue(title);
}
//记录标题 end 


int i = this.getData_index_position_x() + 1;// 计数器


// 创建每个单元格,添加样式,最后合并
// 循环给cell赋值
String dest = map.get("rows").toString().replace("[", "");
dest = dest.replace("]", "");
String[] arr = dest.split("},");


int sizes = arr.length;
for (int boi = 0; boi < sizes; boi++) {
row = sheet.createRow(i);// 得到行
// 无法控制展示顺序
String[] propNames = paramNs;
String dateStr = null;
for (short propi = 0; propi < propNames.length; propi++) {
cell = row.createCell(propi);// 单元格
if (feildRow != null) { // 防止数据列数超过字段名列数数
if (feildRow.getCell(propi) != null) {
columnStyle = feildRow.getCell(propi).getCellStyle();// 样式
}
}
String propN = propNames[propi];
String derz = arr[boi].toString().replace("{", "");
Map<String, Object> mapresutl1 = (Map<String, Object>) getValue("{" + derz + "}");
Object propValue = mapresutl1.get(propN);

if (propValue != null) {
cell.setCellValue(propValue.toString());
cell.setCellStyle(columnStyle);// 填充样式
}
}
i++;
}
long endTime = System.currentTimeMillis();
if (log.isInfoEnabled()) {
log.info("--------生成Excel耗时:" + (endTime - beginTime));
}
//** 输出流 *//*
String filePath = FileProperties.getTempFilePath() + System.getProperty("file.separator");
FileOutputStream os = new FileOutputStream(filePath+"TEMP_FILE_+"+188741+".xls");
        System.out.println(filePath);
        work.write(os);
        os.flush();
        os.close();
} catch (FileNotFoundException e) {
log.error("文件路径错误! ", e);
} catch (IOException e) {
log.error("文件输入流错误! ", e);
}

}


public static Object getValue(String param) {
Map map = new HashMap();
String str = "";
String key = "";
Object value = "";
char[] charList = param.toCharArray();
boolean valueBegin = false;
for (int i = 0; i < charList.length; i++) {
char c = charList[i];
if (c == '{') {
if (valueBegin == true) {
value = getValue(param.substring(i, param.length()));
i = param.indexOf('}', i) + 1;
map.put(key, value);
}
} else if (c == '=') {
valueBegin = true;
key = str;
str = "";
} else if (c == ',') {
valueBegin = false;
value = str;
str = "";
map.put(key, value);
} else if (c == '}') {
if (str != "") {
value = str;
}
map.put(key, value);
return map;
} else if (c != ' ') {
str += c;
}
}
return map;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public int getTitle_position_x() {
return title_position_x;
}


public void setTitle_position_x(int title_position_x) {
this.title_position_x = title_position_x;
}


public int getTitle_position_y() {
return title_position_y;
}


public void setTitle_position_y(int title_position_y) {
this.title_position_y = title_position_y;
}


public int getData_index_position_x() {
return data_index_position_x;
}


public void setData_index_position_x(int data_index_position_x) {
this.data_index_position_x = data_index_position_x;
}


public String getTemplateFileName() {
return templateFileName;
}


public void setTemplateFileName(String templateFileName) {
this.templateFileName = templateFileName;
}


public String getTemplateFilePath() {
return templateFilePath;
}


}


猜你喜欢

转载自blog.csdn.net/kwmnitw/article/details/79472303