在软件开发过程中难免需要批量上传与下载,生成报表保存也是常有之事,最近集团门户开发用到了Excel模版下载,Excel生成,圆满完成,对这一知识点进行整理,资源共享,有不足之处还望批评指正,文章结尾提供了所需jar包的下载,方便大伙使用,下面言归正传!
1.Excel的下载
添加响应事件,通过getRealPath获得工程路径,与jsp中获得request.getContextPath()效果相同,fileName为要下载的文件名,经过拼接filePath是xls文件的绝对路径,调用工具类DownLoadUtil中的downLoadUtil方法;
public ActionForward downLoadExcelModel(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String path = request.getSession().getServletContext().getRealPath(
"/resources");
String fileName = "成员模版.xls";
String filePath = path + "\\" + fileName;
DownLoadUtil.downLoadFile(filePath, response, fileName, "xls");
return null;
}
public static boolean downLoadFile(String filePath,
HttpServletResponse response, String fileName, String fileType)
throws Exception {
File file = new File(filePath); //根据文件路径获得File文件
//设置文件类型(这样设置就不止是下Excel文件了,一举多得)
if("pdf".equals(fileType)){
response.setContentType("application/pdf;charset=GBK");
}else if("xls".equals(fileType)){
response.setContentType("application/msexcel;charset=GBK");
}else if("doc".equals(fileType)){
response.setContentType("application/msword;charset=GBK");
}
//文件名
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes(), "ISO8859-1") + "\"");
response.setContentLength((int) file.length());
byte[] buffer = new byte[4096];// 缓冲区
BufferedOutputStream output = null;
BufferedInputStream input = null;
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = -1;
//遍历,开始下载
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
output.flush(); //不可少
response.flushBuffer();//不可少
} catch (Exception e) {
//异常自己捕捉
} finally {
//关闭流,不可少
if (input != null)
input.close();
if (output != null)
output.close();
}
return false;
}
function submitExcel(){
var excelFile = $("#excelFile").val();
if(excelFile=='') {alert("请选择需上传的文件!");return false;}
if(excelFile.indexOf('.xls')==-1){alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)!");return false;}
$("#fileUpload").submit();
}
public ActionForward exportMemberExcel(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
int maxPostSize = 1000 * 1024 * 1024;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
servletFileUpload.setSizeMax(maxPostSize);
try {
List fileItems = servletFileUpload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {// 是文件
Map map = ExcelUtil.excel2PhoneList(item.getInputStream());
String[][] error = (String[][]) map.get("error");
if (error.length != 0) {
//如果存在错误,存入内存,供用户保存
request.setAttribute("errorFlag", "ture");
request.getSession().setAttribute("exportMap", map);
} else {
request.setAttribute("errorFalg", "false");
// 获取正确的值填写json
String phoneJson = JsonUtil
.phoneArray2Json((String[][]) map
.get("current"));
request.setAttribute("phoneJson", phoneJson);
}
} else { //不是文件,此处不处理,对于text,password等文本框可在此处理
logger.error("wrong file");
}
}
} catch (Exception e) {
logger.error("invoke fileUpload error.", e);
}
return actionMapping.findForward("queryAccounts");
}
public static Map excel2PhoneList(InputStream inputStream) throws Exception {
Map map = new HashMap();
Workbook workbook = Workbook.getWorkbook(inputStream); //处理输入流
Sheet sheet = workbook.getSheet(0);// 获取第一个sheet
int rows = sheet.getRows(); //获取总行号
String[][] curArr = new String[rows][2]; //存放正确心细
String[][] errorArr = new String[rows * 2][4]; //存放错误信息
int curLines = 0;
int errorLines = 0;
for (int i = 1; i < rows; i++) {// 遍历行获得每行信息
String phone = sheet.getCell(0, i).getContents();// 获得第i行第1列信息
String shortNum = sheet.getCell(1, i).getContents();// 短号
StringBuffer errorMsg = new StringBuffer();
if (!isRowEmpty(sheet, i)) { //此行不为空
//对此行信息进行正误判断
}
}// 行
//正误信息存入map,保存
map.put("current", current);
map.put("error", error);
return map;
}
private static boolean isRowEmpty(Sheet sheet, int i) {
String phone = sheet.getCell(0, i).getContents();// 集团编号
String shortNum = sheet.getCell(1, i).getContents();// 集团名称
if (isEmpty(phone) && isEmpty(shortNum))
return true;
return false;
}
public ActionForward downErrorExcel(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map map = (Map) request.getSession().getAttribute("exportMap");
ExcelUtil.createExcel(response, map);
request.getSession.removeAttribure("exportMap");
return null;
}
public static boolean createExcel(HttpServletResponse response, Map map) {
WritableWorkbook wbook = null;
WritableSheet sheet = null;
OutputStream os = null;
try {
response.reset();
//生成文件名
response.setHeader("Content-disposition", "attachment; filename="
+ new String("错误信息".getBytes("GB2312"), "ISO8859-1")
+ ".xls");
response.setContentType("application/msexcel");
os = response.getOutputStream();
wbook = Workbook.createWorkbook(os);
sheet = wbook.createSheet("信息", 0);
int row = 0;
//填写表头
setSheetTitle(sheet, row);
//遍历map,填充数据
setSheetData(sheet, map, row);
wbook.write();
os.flush();
} catch (Exception e) {
//自己处理
}finally {
try {//切记,此处一定要关闭流,否则你会下载一个空文件
if (wbook != null)
wbook.close();
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
return false;
}
private static void setSheetTitle(WritableSheet sheet, int row)
throws RowsExceededException, WriteException {
// 设置excel标题格式
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
//设置每一列宽度
sheet.setColumnView(0,13);
sheet.setColumnView(1,13);
sheet.setColumnView(2,13);
sheet.setColumnView(3,50);
//填写第一行提示信息
sheet.addCell(new Label(0, row, "手机号码", wcfFC));
sheet.addCell(new Label(1, row, "短号", wcfFC));
sheet.addCell(new Label(2, row, "原错误行号", wcfFC));
sheet.addCell(new Label(3, row, "错误原因", wcfFC));
}
private static void setSheetData(WritableSheet sheet, Map map, int row)
throws RowsExceededException, WriteException {
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
//从map中获取数据
String[][] error = (String[][]) map.get("error");
//遍历并填充
for (int i = 0; i < error.length; i++) {
row++;
sheet.addCell(new Label(0, row, error[i][0]));
sheet.addCell(new Label(1, row, error[i][1]));
sheet.addCell(new Label(2, row, error[i][2]));
sheet.addCell(new Label(3, row, error[i][3],wcfFC));
}
}