参考文件:http://blog.csdn.net/youyou_yo/article/details/52259871
https://www.cnblogs.com/mingyue1818/p/6054188.html//本参考中的(short)去除掉
解决2007和2003版本的兼容问题
1.在指定路径下创建excel导入模板
/**
* @param @param filePath Excel文件路径
* @param @param handers Excel列标题(数组)
* @param @param downData 下拉框数据(数组)
* @param @param downRows 下拉列的序号(数组,序号从0开始)
* @return void
* @throws
* @Title: createExcelTemplate
* @Description: 生成全新的Excel导入模板
*/
private static void createExcelTemplate(String filePath, String[] handers,
List<String[]> downData, String[] downRows) {
HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
//表头样式
// HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// //字体样式
// HSSFFont fontStyle = wb.createFont();
// fontStyle.setFontName("微软雅黑");
// fontStyle.setFontHeightInPoints((short) 12);
// fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// style.setFont(fontStyle);
//新建sheet
HSSFSheet sheet1 = wb.createSheet("Sheet1");
//生成sheet1内容
Row rowFirst = sheet1.createRow(0);//第一个sheet的第一行为标题
//写标题
for (int i = 0; i < handers.length; i++) {
Cell cell = rowFirst.createCell(i); //获取第一行的每个单元格
sheet1.setColumnWidth(i, 4000); //设置每列的列宽
// cell.setCellStyle(style); //加样式
cell.setCellValue(handers[i]); //往单元格里写数据
}
for (int r = 0; r < downRows.length; r++) {
String[] dlData = downData.get(r);//获取下拉对象
int rownum = Integer.parseInt(downRows[r]);
sheet1.addValidationData(setDataValidation(sheet1, dlData, 1, 50000, rownum, rownum)); //超过255个报错
}
try {
// File f = new File(filePath); //写文件
// //不存在则新增
// if (!f.getParentFile().exists()) {
// f.getParentFile().mkdirs();
// }
// if (!f.exists()) {
// f.createNewFile();
// }
// FileOutputStream out = new FileOutputStream(f);
FileOutputStream fileOut = new FileOutputStream("testte.xls");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.读取存在的excel,在程序填充处理数据后,将excel导出到指定路径
/**
* @param @param filePath Excel文件路径
* @param @param handers Excel列标题(数组)
* @param @param downData 下拉框数据(数组)
* @param @param downRows 下拉列的序号(数组,序号从0开始)
* @return void
* @throws
* @Title: createExcelTemplate
* @Description: 先读取现有的模板然后在模板中填充数据
*/
private static void createExcel(String filePath, String[] handers,
List<String[]> downData, String[] downRows) throws IOException, InvalidFormatException {
File file = new File(filePath);
FileInputStream in = new FileInputStream(file);
//创建工作薄
Workbook wb = WorkbookFactory.create(in);
//获得sheet
Sheet sheet1 = wb.getSheetAt(0);
for (int r = 0; r < downRows.length; r++) {
String[] dlData = downData.get(r);//获取下拉对象
int rownum = Integer.parseInt(downRows[r]);
sheet1.addValidationData(setDataValidation(sheet1, dlData, 1, 50, rownum, rownum)); //超过255个报错
}
try {
// File f = new File(filePath); //写文件
// //不存在则新增
// if (!f.getParentFile().exists()) {
// f.getParentFile().mkdirs();
// }
// if (!f.exists()) {
// f.createNewFile();
// }
FileOutputStream fileOut = new FileOutputStream("tete.xls");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3.生成导出excel文件并在浏览器中下载
/**
* @param @param url 文件路径
* @param @param fileName 文件名
* @param @param response
* @return void
* @throws
* @Title: getExcel
* @Description: 下载指定路径的Excel文件
*/
public static void downExcelTemplate(String fileName, HttpServletResponse response, HttpServletRequest request) {
try {
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-disposition", "attachment; filename=\""
+ encodeChineseDownloadFileName(request, fileName + ".xls") + "\"");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls"); //中文文件名
HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
//表头样式
// HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// //字体样式
// HSSFFont fontStyle = wb.createFont();
// fontStyle.setFontName("微软雅黑");
// fontStyle.setFontHeightInPoints((short) 12);
// fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// style.setFont(fontStyle);
//新建sheet
HSSFSheet sheet1 = wb.createSheet("new Sheet1");
//通过文件路径获得File对象
//3.通过response获取OutputStream对象(out)
OutputStream out = new BufferedOutputStream(response.getOutputStream());
wb.write(out);
out.flush();
out.close();
} catch (IOException e) {
System.err.println("下载Excel模板异常" + e);
}
}
4.下载指定路径的excel文件
/**
* 下载静态资源文件公共方法
* @param fileName 文件名称
* @param response 响应
* @throws IOException 异常
*/
public static void downStaticFile(String file,String fileName, HttpServletResponse response) throws IOException {
OutputStream out = null;
FileInputStream fis = null;
try{
out = response.getOutputStream();
// 设置response的Header
response.addHeader("Content-Disposition",
"attachment;filename="
+ new String(fileName.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/octet-stream");
String basePath = FileDownUpload.class.getResource("/").getPath().split("WEB-INF")[0]+"app/file/";
fis = new FileInputStream(basePath + file);
int b;
while((b=fis.read())!= -1)
{
out.write(b);
}
fis.close();
out.flush();
}catch (Exception e){
e.printStackTrace();
logger.error("执行文件下载downStaticFile方法失败:" + ExceptionUtils.getErrorInfo(e));
}finally {
if(out != null) out.close();
if(fis != null) fis.close();
}
}