⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exportinfo.java

📁 一个优秀的干洗店管理系统
💻 JAVA
字号:
package control.export;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExportInfo {
	
	public void writeExcelOfFile(String fileName, Vector vectorData,
		      Vector vectorTitle,String headTitle,String[] head) {
		    WritableWorkbook excelModel = null; // Excle文件写出操作对象
		    try {
		      // 创建Excel文件写出操作对象
		      File file = new File(fileName);
		      excelModel = Workbook.createWorkbook(file);

		      // 调用生成Excel列表方法设置并生成Excel
		      this.writeExcelFile(excelModel, vectorData,headTitle,head);
		    } catch (IOException e) {
		      e.printStackTrace();
		    } catch (Exception e) {
		      e.printStackTrace();
		    } finally {
		      try {
		        excelModel.close(); // 必须关闭创建Excel文档的对象
		      } catch (WriteException e) {
		        e.printStackTrace();
		      } catch (IOException e) {
		        e.printStackTrace();
		      }
		    }
		  }
	
	 /**
	   * 写出Excel文件
	   * 
	   * @param excelModel
	   * @param argVectorData
	   *          要生成excel文件的数据
	   * @throws WriteOutExcelException
	   */
	  private void writeExcelFile(WritableWorkbook excelModel, Vector argVectorData,String headTitle,String[] head)
	      throws Exception {
	    try {
	      /* 设置标题 */
//	      String title = "收取衣物明细表";
	    	String title = headTitle;
	      // 设置页面Sheet
	      WritableSheet ws = excelModel.createSheet(title, 0);

	      // 设置头
	      this.buildExlHead(ws, title,head);

	      // 每一个单元个的样式
	      WritableCellFormat callFormat = new WritableCellFormat();
	      callFormat.setAlignment(Alignment.CENTRE);
	      callFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
	      callFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
	      // 得到所有员工,Vo
	      Iterator dataIterator = argVectorData.iterator();
	      int rowNum = 2; // 值起始行号
	      while (dataIterator.hasNext()) {
	    	 Vector v = new Vector();
	    	 v  = (Vector)dataIterator.next();	       
	        for (int i = 0, n = v.size(); i < n; i++) {
	          if (v.elementAt(i) == null) {
	            v.setElementAt("", i);
	          }
	          ws.addCell(new Label(i, rowNum, v.elementAt(i).toString(),
	                  callFormat));
	        }
	        rowNum++;
	      }

	      // 写出Excel文件
	      excelModel.write(); // 必须调用写文件的方法

	    } catch (FileNotFoundException fne) {
	      fne.printStackTrace();
	    } catch (IOException ex) {
	      ex.printStackTrace();
	    } catch (WriteException wex) {
	      wex.printStackTrace();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	  }
	  
	  /**
	   * 设置Excel报表标题
	   * 
	   * @param sheet
	   *          Excel操作对象的Sheet
	   * @param argTitle
	   *          标题名称
	   * @param colunmUnite
	   *          合并列数
	   * @param rowUnite
	   *          合并行数
	   * @return WritableSheet
	   * @throws WriteException
	   */
	  protected WritableSheet setExcelTitle(WritableSheet sheet, String argTitle,
	      int colunmUnite, int rowUnite) throws WriteException {
	    // 设置合并标题栏的格式
	    sheet.mergeCells(0, 0, colunmUnite, rowUnite);

	    // 定义标题字体等信息
	    WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 20,
	        WritableFont.NO_BOLD);
	    WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
	    // 设置表头居中显示
	    titleFormat.setAlignment(Alignment.CENTRE);
	    titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

	    Label title = new Label(0, 0, argTitle// 设置的标题
	        , titleFormat);
	    sheet.addCell(title);
	    return sheet;
	  }
	  
	  /**
	   * 生成收衣明细Excel报表的报表头部
	   * 
	   * @param sheet
	   *          WritableSheet
	   * @return WritableSheet
	   * @throws WriteException
	   */
	  public WritableSheet buildExlHead(WritableSheet sheet, String argTitle,String[] head)
	      throws Exception {
	    try {
	    	int length = head.length;
	      // 创建标题
	      setExcelTitle(sheet, argTitle, length -1, 0);

	      // 设置列表头信息
	      WritableFont headFont = new WritableFont(WritableFont.ARIAL, 12,
	          WritableFont.NO_BOLD);
	      WritableCellFormat headFormat = new WritableCellFormat(headFont);
	      headFormat.setAlignment(Alignment.CENTRE);
	      headFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
	      headFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

	      // 设置表的样式
	      Vector infoTitle = new Vector();
	     /* "洗衣单号", "添加日期", "取衣日期", "会员编号", "客户名称", "衣服名称",
			"数量", "服务类型", "折后价", "总金额", "扣折", "是否取走", "衣服品牌", "衣服附件",
			"衣服瑕疵", "颜色", "备注", "操作员"*/
	      for(int i = 0;i < head.length;i++){
	    	  infoTitle.add(head[i]);
	      }
	      for (int i = 0; i < head.length; i++) {
	        sheet.mergeCells(i, 1, i, 1);
	        Label headLabel = new Label(i, 1, (String)infoTitle.get(i), headFormat);
	        sheet.addCell(headLabel);
	      }
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	    return sheet;
	  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -