日期:2014-05-17 浏览次数:20648 次
package com.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Vector; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.format.Colour; import jxl.format.UnderlineStyle; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableFont; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import com.xamedical.base.Prescription; import com.xamedical.common.Format; import com.xamedical.common.Globa; import com.xamedical.common.UID; import com.xamedical.system.District; import com.xamedical.system.SysUserUnit; import com.xamedical.system.SysPara; import com.xamedical.system.Unit; public class Excel { private Globa globa; private HttpServletRequest request; private HttpServletResponse response; private HttpSession session; private ServletContext application; public Excel(Globa globa) { this.globa = globa; this.request = globa.request; this.response = globa.response; this.session = globa.session; this.application = globa.application; } public void output() { String strFileId = UID.getID(); //File fExcel = new File(application.getRealPath("") + "\\excel\\" + strFileId + ".xls"); String filename = "方剂.xls" try { //输出 OutputStream os = response.getOutputStream(); response.setHeader( "Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO8859-1")); response.setContentType("application/msexcel"); WritableWorkbook wwb = Workbook.createWorkbook(os); jxl.write.WritableSheet ws = wwb.createSheet("复合查询结果", 0); WritableFont wfSongBlack = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); jxl.write.WritableCellFormat wcfSongBlack = new jxl.write.WritableCellFormat(wfSongBlack); //获取需要导出的方剂记录集合 Vector<Prescription> content = (Vector<Prescription>)session.getAttribute("content"); try { Label l; String composition; String[] array; for(int i=0;i<content.size();i++) { l = new Label(0,i,content.get(i).getStrName(),wcfSongBlack); ws.addCell(l); //分割方剂组成字符串,得到中药的一个字符串数组。 composition = content.get(i).getStrComposition(); composition = composition.replaceAll(",|。", ""); array = composition.split("\\d+g|.钱|.两"); for(int j=0;j<array.length;j++) { l = new Label(j+1,i,array[j],wcfSongBlack); ws.addCell(l); } } } catch(WriteException we) { we.printStackTrace(); } wwb.write();//写入excel对象 wwb.close();//关闭可写