欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java用jxl包导出Excel的例子

程序员文章站 2022-07-13 11:35:11
...
	/**
	 * 导出信息
	 * @date 2011-6-10
	 */
	public void exportMatchangeExcel(QueryVo queryVo) {
		WritableWorkbook workbook = null;
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream outPutS = response.getOutputStream();
			response.resetBuffer();
			response.setHeader("Content-Type", "application/force-download;");
			//中文名:name = new String(name.getBytes("GBK"),"UTF-8");
			response.setHeader("Content-disposition", "attachment;filename=matchange--"+FDateFormat.GetShortDate()+".xls");
			response.setContentType("application/excel");
			workbook = Workbook.createWorkbook(outPutS);
			
			WritableSheet sheet = workbook.createSheet("仓库物料进出库信息", 0);
			String title[] = {"序号","物料号"};
			     
			Label label = null;
			/** 字体设置 */
			WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					Colour.BLACK);
			/** 格式 */
			WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
			headerFormat.setAlignment(Alignment.CENTRE);
			headerFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
					Colour.BLACK);
			
			WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					Colour.BLACK);
			WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
			titleFormat.setAlignment(Alignment.CENTRE);
			titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
					Colour.BLACK);

			WritableFont columnNameFont = new WritableFont(WritableFont.ARIAL,
					10, WritableFont.NO_BOLD, false,
					UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
			WritableCellFormat columnNameFormat = new WritableCellFormat(
					columnNameFont);
			columnNameFormat.setAlignment(Alignment.CENTRE);
			columnNameFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
					Colour.BLACK);
			
			WritableCellFormat columnNameFormatLeft = new WritableCellFormat(
					columnNameFont);
			columnNameFormatLeft.setAlignment(Alignment.LEFT);
			columnNameFormatLeft.setBorder(Border.ALL, BorderLineStyle.THIN,
					Colour.BLACK);
			
			sheet.mergeCells(0, 0, 13, 0);//第一个参数:开始列,第二个参数:开始行,第三个参数:结束列,第四个参数:结束行,
			label = new Label(0, 0, "[仓库物料进出库信息列表--"+FDateFormat.GetLongDate()+"]", headerFormat);
			sheet.addCell(label);
			
			int index=0;//行索引
			int row = 1;//行数	
			int column = 0;
			for (int i = 0; i < title.length; i++) {
				label = new Label(column++, 1, title[i], titleFormat);
				sheet.addCell(label);
			}	
			
			jxl.write.Number labelNF = null;			
			
			if(dsbfMatchangeList != null){
				for(KdDsbfMatchangeVo dsbfMatchangeVo : dsbfMatchangeList){
					++index;
					row++;
					column = 0;
						
					//序号
					labelNF = new jxl.write.Number(column++, row,index,columnNameFormat);
					sheet.addCell(labelNF);	

					//物料号
					label = new Label(column++, row,物料号,columnNameFormat);
					sheet.addCell(label);	
				}	
			 }
			column = 0;
			
			/* 设置长度 */
			sheet.setColumnView(column++, 5 );//0
			sheet.setColumnView(column++, 12);//1
			
            // 写入数据      
			workbook.write();      
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				workbook.close();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
  • jxl.jar (708.7 KB)
  • 下载次数: 8