利用jxl导出excel
html:
var url = "${base}/risen/oa/supervise/exportTotalCount.action" window.open(url);
xml:
<action name="exportStatisticsExcel" class="oaSuperviseAction" method="exportStatisticsExcel"> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">fileStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> </result> </action>
action:
public String exportTotalCount(){ OaSupervise model = getModel(); String path=ServletActionContext.getServletContext().getRealPath("/public"); String FileName=this.fileName(); try { WritableWorkbook workbook; OutputStream os=new FileOutputStream(path+"/excel/"+FileName+".xls"); workbook=Workbook.createWorkbook(os); long currentTime = System.currentTimeMillis(); this.getOaSuperviseService().exportTotalCount(workbook,model); long time =System.currentTimeMillis() - currentTime; System.out.println(time); workbook.close(); //上面是将内容导入服务器上,然后再将服务器上的下载下来 File file = new File(path+"/excel/"+FileName+".xls");//要下载的文件路径 fileName = file.getName();//文件名 fileStream = new FileInputStream(file);//换流 } catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
service:
public void exportTotalCount(WritableWorkbook workbook, OaSupervise model) { // TODO Auto-generated method stub //添加一张工作表 WritableSheet sheet=workbook.createSheet("督办整改反馈清单汇总", 0); //定义4种字体类型 try { // 定义表头、标题栏 this.buildTotalCount(sheet, "督办整改反馈清单汇总"); // 查找出相关List开始写入内容 this.buildRecordsStreet(sheet, model); workbook.write(); } catch (Exception e) { e.printStackTrace(); } }
private void buildTotalCount(WritableSheet sheet, String title) { try { //表头 WritableFont font = new WritableFont(WritableFont.createFont("方正小标宋简体"), 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat cFormat = new WritableCellFormat(font); cFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE ); cFormat.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); cFormat.setAlignment(Alignment.CENTRE); cFormat.setWrap(true); //标题类 WritableFont font2 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat cFormat2 = new WritableCellFormat(font2); cFormat2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE );//设置文字垂直方向居中 cFormat2.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); //设置单元格边框,且边框为黑色 cFormat2.setAlignment(Alignment.CENTRE); cFormat2.setWrap(true); //定义头 String headValue=title; String[] heads=null; heads=new String[]{"序号","编号","督办情况","督办时间","问题类型","交办基本信息","交办反馈内容","督办事由","督办反馈结果","审核结果","责任单位","配合单位","限办时间","实际反馈日期","交办时照片","交办后照片","督办前照片","督办后照片","反馈情况","意见","反馈单子填写是否规范","是否结案","30日前反馈情况"}; sheet.setColumnView(0,20); sheet.setColumnView(1,20); sheet.setColumnView(2,40); sheet.setColumnView(3,40); sheet.setColumnView(4,40); sheet.setColumnView(5,40); sheet.setColumnView(6,40); sheet.setColumnView(7,40); sheet.setColumnView(8,40); sheet.setColumnView(9,40); sheet.setColumnView(10,40); sheet.setColumnView(11,40); sheet.setColumnView(12,40); sheet.setColumnView(13,40); sheet.setColumnView(14,40); sheet.setColumnView(15,40); sheet.setColumnView(16,40); sheet.setColumnView(17,40); sheet.setColumnView(18,40); sheet.setColumnView(19,40); sheet.setColumnView(20,40); sheet.setColumnView(21,40); sheet.setColumnView(22,40); Label label=new Label(0,0,headValue,cFormat); sheet.addCell(label); sheet.mergeCells(0,0,heads.length-1,0); //将标题栏写入sheet for(int i=0;i<heads.length;i++){ Label hd=new Label(i,1,heads[i],cFormat2); sheet.addCell(hd); } } catch (Exception e) { e.printStackTrace(); } }
import java.awt.image.BufferedImage; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.imageio.ImageIO; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.format.UnderlineStyle; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableImage; import jxl.write.WritableSheet; import org.springframework.util.StringUtils; import com.risen.base.cnd.Cnd; import com.risen.dc.model.OaAssigned; import com.risen.dc.model.OaSupervise; private void buildRecordsStreet(WritableSheet sheet, OaSupervise model) { try { //普通(居中) WritableFont font3 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); WritableCellFormat cFormat3 = new WritableCellFormat(font3); cFormat3.setAlignment(Alignment.CENTRE); cFormat3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE ); cFormat3.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); cFormat3.setWrap(true); //普通(居左) WritableFont font4 = new WritableFont(WritableFont.createFont("宋体"), 12,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); WritableCellFormat cFormat4 = new WritableCellFormat(font4); cFormat4.setAlignment(Alignment.LEFT); cFormat4.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE ); cFormat4.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK); cFormat4.setWrap(true); int d=1; Label label = null; //---------------------------------查询开始---------------------------- List<OaSupervise> selectModelList = this.getMyBatisDaoManager().exeSelectCommand(model, "getTotalCountList"); int xuhao = 1; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sheet.setRowView(1, 700, false); //设置行高 //----------------------------------查询结束---------------------------------- for (int i = 0; i < selectModelList.size(); i++) { d++; sheet.setRowView(d, 700, false); //设置行高 OaSupervise info = selectModelList.get(i); //序号 label=new Label(0,d,String.valueOf(xuhao++),cFormat3); sheet.addCell(label); //编号 label=new Label(1,d,info.getOaspNumber(),cFormat3); sheet.addCell(label); //督办情况 String oaspSupervisenum = info.getOaspSupervisenum(); if (StringUtils.hasText(oaspSupervisenum)) { oaspSupervisenum = StringHelper.digitalToStringForDb(Integer.valueOf(oaspSupervisenum)); } label=new Label(2,d,oaspSupervisenum,cFormat3); sheet.addCell(label); //督办时间 label=new Label(3,d,sdf.format(info.getOaspSupervisedate()),cFormat3); sheet.addCell(label); //问题类型 label=new Label(4,d,info.getOaspQuestiontype1(),cFormat3); sheet.addCell(label); //限办时间 label=new Label(12,d,sdf.format(info.getOaspLimitdate()),cFormat3); sheet.addCell(label); //督办后照片 String picture_after = oaFeedbackService.getPicturePathAfter(info.getOaspUuid(),"2"); if (StringUtils.hasText(picture_after)) { picture_after = "D:/RESOURCE_ROOT/ATTATCHMENT" + picture_after; String geshi_after = picture_after.substring(picture_after.lastIndexOf(".") + 1); File imgFile_after = new File(picture_after); if (imgFile_after.exists() && imgFile_after.length() != 0 && "jpg".equals(geshi_after)) { //图片转码,因为jxl只支持导出png格式 String new_name_after = picture_after.substring(0,picture_after.lastIndexOf(".")) + ".png"; File f = new File(picture_after); if(!(f.exists() && f.length() != 0)) { f.canRead(); BufferedImage src = ImageIO.read(f); ImageIO.write(src, "png", new File(new_name_after)); } WritableImage image = new WritableImage(17,d,1,1,new File(new_name_after)); sheet.setRowView(d, 3650); //设置行高 sheet.addImage(image); } else { if(imgFile_after.exists() && imgFile_after.length() != 0) { WritableImage image = new WritableImage(17,d,1,1,imgFile_after); sheet.setRowView(d, 3650); //设置行高 sheet.addImage(image); } else { label=new Label(17,d,"",cFormat3); sheet.addCell(label); } } } else { label=new Label(17,d,"",cFormat3); sheet.addCell(label); } } } catch (Exception e) { e.printStackTrace(); } }

浙公网安备 33010602011771号