excle在线导出打开,l浏览器下载乱码,单元格合并.docx_第1页
excle在线导出打开,l浏览器下载乱码,单元格合并.docx_第2页
excle在线导出打开,l浏览器下载乱码,单元格合并.docx_第3页
excle在线导出打开,l浏览器下载乱码,单元格合并.docx_第4页
excle在线导出打开,l浏览器下载乱码,单元格合并.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package mon.util;import .URLEncoder;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.VerticalAlignment;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import com.rimi.daily.model.OfficeDaily;/* * version 0.1 * author lukin * createDate 2013-10-31 * */public class CommonMethod /* * 导出日报报表数据数据到excel * param list 数据集合 * param title excel导出标题 * param path 文件保存路径 */public static void writeExcelData(List list,String title,HttpServletResponse response,HttpServletRequest request,boolean isOnLine,String fileNames)String valName = ;Map mergeCell = new HashMap();int start = 1,end=1; try onLineDowload(response, request, isOnLine, fileNames); WritableWorkbook wwb = null;/ 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象wwb = Workbook.createWorkbook(response.getOutputStream();if (wwb != null) / 创建一个可写入的工作表/ Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置WritableSheet ws = wwb.createSheet(sheet1, 0);WritableCellFormat wcf = new WritableCellFormat();/单元格格式WritableCellFormat formatLabl2 = new WritableCellFormat();/单元格格式formatLabl2.setVerticalAlignment(VerticalAlignment.CENTRE);formatLabl2.setAlignment(Alignment.CENTRE);for (int i = 0; i 0) for (int i = 0; i =1) flag = list.get(i-1).getProjectName().equals(list.get(i).getProjectName();if (flag) end+;else mergeCell.put(start, end);end+;start = end;if(i=list.size()-1) mergeCell.put(start, end);if(flag)if(.equals(valName)valName =de.getProjectName();ws.addCell(label2);else if(!valName.equals(de.getProjectName()valName =de.getProjectName();ws.addCell(label2);elsews.addCell(label2);valName = ;/项目经理 String projectManager = de.getProjectManager()=null?暂无:de.getProjectManager();Label label3 = new Label(2, (i + 1),+projectManager, wcf);ws.addCell(label3);/里程碑负责人String stepManager = de.getStepManager()=null?暂无:de.getStepManager();Label label4 = new Label(3, (i + 1),+stepManager, wcf);ws.addCell(label4);String userName = de.getUserId()=null?暂无:de.getUserId();Label label5 = new Label(4, (i + 1),+userName, wcf);ws.addCell(label5);String dayWorkDate = DateFormatUtil.dateTimeToStr(de.getCreateDate(), yyyy-MM-dd);Label label6 = new Label(5, (i + 1),+dayWorkDate, wcf);ws.addCell(label6);String pd = DateFormatUtil.dateTimeToStr(de.getPlanDate(), yyyy-MM-dd);Label label7 = new Label(6, (i + 1),+pd, wcf);ws.addCell(label7);String rd =de.getReallyDate()=null?暂无时间: DateFormatUtil.dateTimeToStr(de.getReallyDate(), yyyy-MM-dd);Label label8 = new Label(7, (i + 1),+rd, wcf);ws.addCell(label8);String complate = de.getIsComplete()= 0?未完成:已完成;Label label9= new Label(8, (i + 1),+ complate , wcf);ws.addCell(label9);String status = de.getStatus()= 0?未提交:已提交;Label label10 = new Label(9, (i + 1),+status, wcf);ws.addCell(label10);String desc = de.getJobDesc()=null?暂无描述:de.getJobDesc();Label label11 = new Label(10, (i + 1),+desc, wcf);ws.addCell(label11);String reason = de.getReason()=null?暂无原因:de.getReason();Label label12 = new Label(11, (i + 1),+reason, wcf);ws.addCell(label12);String problem = de.getProblem()=null?暂无遇见问题:de.getProblem();Label label13 = new Label(12, (i + 1),+problem, wcf);ws.addCell(label13);String tomorrowPlan = de.getTomorrowPlan()=null?暂无计划:de.getTomorrowPlan();Label label14 = new Label(13, (i + 1),+tomorrowPlan, wcf);ws.addCell(label14);String note = de.getNote()=null?暂无备注:de.getNote();Label label15 = new Label(14, (i + 1),+note, wcf);ws.addCell(label15);/合并单元格的开始位置到结束位置的(遍历集合)Set keys = mergeCell.keySet();for (Integer integer : keys) start = integer;end = mergeCell.get(integer);if (start!=end) ws.mergeCells(1, start, 1, end);/合并单元格,参数格式(开始列,开始行,结束列,结束行)/ 从内存中写入文件中wwb.write();/ 关闭资源,释放内存wwb.close(); catch (Exception e) e.printStackTrace();SuppressWarnings(unused)/* * 下载excle文件 并设置浏览器编码 */private static void onLineDowload(HttpServletResponse response,HttpServletRequest request,boolean isOnLine,String fileNames)try response.reset(); / 非常重要 if (isOnLine) / 在线打开方式/ URL u = new URL(file:/ + filePath);/ response.setContentType(u.openConnection().getContentType(); response.reset(); if (request.getHeader(User-Agent).toLowerCase().indexOf(firefox) 0)/firefox浏览器response.setHeader(Content-disposition, attachment; filename= + new String(fileNames.getBytes(UTF-8),ISO8859-1) + ); else if (request.getHeader(User-Agent).toUpperCase().indexOf(MSIE) 0) response.setHeader(Content-disposition, attachment;filename=+ URLEncoder.encode(fileNames, UTF-8);/IE浏览器 else if (request.getHeader(User-Agent).toUpperCase().indexOf(AppleWebKit) 0)/google Chrome浏览器 response.setHeader(Content-disposition, attachment; filename= + new String(fileNames.getBytes(UTF-8),UTF-8) + ); else String fileName=template.xls; response.addHeader(Content-Disposition, attachment; filename=+fileName); else / 纯下载方式 response.setContentType(application/x-msdownload); response.reset(); if (request.getHeader(User-Agent).toLowerCase().indexOf(firefox) 0)/firefox浏览器 response.setHeader(Content-disposition, attachment; filename= + new String(fileNames.getBytes(UTF-8),ISO8859-1) + ); else if (request.getHeader(User-Agent).toUpperCase().indexOf(MSIE) 0) response.setHeader(Content-disposition, attachment;filename=+ URLEncoder.encode(fileNames, UTF-8);/IE浏览器 else if (request.getHeader(User-Agent).toUpperCase().indexOf(AppleWebKit) 0)/google Chrome浏览器 response

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论