EAS BOS 自定义导出(含Excel样式设置、多页签导出、导出文件目录校验及备份)

今天给大家分享一段关于EAS BOS自定义导出的开发。

功能背景:

叙事界面调用此类:

    /**
     * 调用案例
     * 在需要调用的界面  onload方法中添加
     * ExcelExport.getExcelExport(tblMain, toolBar);
     * 
     */

相关常用功能:

1:常用Excel样式设置

2:Excel多页签导出,后续补上对应多线程导出

2:Excel导出目录检查

3:导出备份

 

/**
 * 
 */
package com.kingdee.eas.custom.test.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFileChooser;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

import com.ibm.as400.util.commtrace.Data;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.swing.KDToolBar;
import com.kingdee.bos.ctrl.swing.KDWorkButton;
import com.kingdee.bos.ui.face.UIRuleUtil;
import com.kingdee.eas.cp.odm.web.ChooseRedHeadBean;
import com.kingdee.eas.util.client.EASResource;

/**
 * @author gectan
 * 2018.12.10
 * excel导出
 */
public class ExcelExport {
	
	
	
	/*	常用组件:

	HSSFWorkbook                      excel的文档对象

	HSSFSheet                         excel的表单

	HSSFRow                           excel的行

	HSSFCell                          excel的格子单元

	HSSFFont                          excel字体

	HSSFDataFormat                    日期格式

	HSSFHeader                        sheet头

	HSSFFooter                        sheet尾(只有打印的时候才能看到效果)

	样式:

	HSSFCellStyle                       cell样式

	辅助操作包括:

	HSSFDateUtil                        日期

	HSSFPrintSetup                      打印

	HSSFErrorConstants                  错误信息表*/
	
	
	
	
	/**
	 * 调用案例
	 * 在需要调用的界面  onload方法中添加
	 * ExcelExport.getExcelExport(tblMain, toolBar);
	 * 
	 */
	
	
	
	
	/**
	 * @author gectan 
	 * 2018-12-11
	 * 参数 tblMain  表编辑器名称
	 * 参数 toolBar  工具栏
	 * 
	 */
	public static void getExcelExport(final KDTable tblMain, KDToolBar toolBar){
		
		KDWorkButton btnExport = new KDWorkButton();
		btnExport.setIcon(EASResource.getIcon("imgTbtn_output"));
		btnExport.setToolTipText("自定义导出");
		btnExport.setText("自定义导出");
		toolBar.addRightComponent(btnExport, 20);
		
		btnExport.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				//setExcelExporter(tblMain);
				setExcelExporter1(tblMain);
			}
			
		});
	}

	/**   
	 * @Title: setExcelExporter1   
	 * @Description: TODO(个人Excel导出测试类,勿用)   
	 * @param: @param tblMain      
	 * @return: void      
	 * @throws   
	 */
	protected static void setExcelExporter1(KDTable tblMain) {
		Date date = new Date();
		//注意日期格式是HH-mm-SS,不是HH:mm:SS   Excel文件名不允许有":"
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-SS");  
		String now = sdf.format(date);
		
		
		//获取用户选择的导出路径
		String url = null ;
		String dec = null ;
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只能选择目录
		int returnVal = chooser.showOpenDialog(null);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			url =chooser.getSelectedFile().getPath() ;
		}
		
		String  srcfile = url + "\\Excel导出测试根目录";
		String  fileLog = url + "\\Excel导出测试根目录\\导出备份";
		File fileParent = new File(srcfile);
		File exportLog = new File(fileLog);
		if(!fileParent.exists()){
			fileParent.mkdirs();   //创建文件夹  
			exportLog.mkdirs();
		}
	
		url = srcfile + "\\Excel导出测试.xls";
		dec = fileLog + "\\Excel导出测试("+now+").xls";
		//创建HSSFWorkbook对象(excel的文档对象)
		HSSFWorkbook workBook = new HSSFWorkbook();
		//设置字体样式
		HSSFFont font = workBook.createFont();
		font.setColor(HSSFColor.RED.index);                 // 颜色
		font.setFontHeightInPoints((short) 15);             // 字体大小
		font.setFontName("仿宋_GB2312");                     // 字体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);     // 加粗
		font.setItalic(true);                               // 设置斜体
		font.setUnderline(HSSFFont.U_SINGLE);               // 设置下划线
		
		//设置单元格样式
		HSSFCellStyle cellStyle = workBook.createCellStyle();
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);   //左边框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);    //上边框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  //右边框
		
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);   
		//cellStyle.setFillForegroundColor(HSSFColor.RED.index);           //设置前景色 
		cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);    //设置背景颜色    
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
		//设置日期格式  其他类型的字段,也在cellStyle下设置
		//cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(“m/d/yy h:mm”));  
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
		
		//建立新的sheet对象(excel的表单)
		HSSFSheet sheet = workBook.createSheet("页签1");       //创建工作表(Excel页签)
		sheet.setDefaultRowHeightInPoints(10);                //设置行高
		HSSFRow row = sheet.createRow(0); //创建Excel的第一行
		row.createCell(0).setCellValue("Excel导出测试");        //给第一行的单元格赋值       
		
		
		row.getCell(0).setCellStyle(cellStyle);              //设置单元格样式
		row.getCell(0).getCellStyle().setFont(font);         //设置字体格式   (注意:字体样式写在单元格样式后)
		//row1.setRowStyle(cellStyle);                       //将样式应用到行,但有些样式只对单元格起作用
		
		
		//合并单元格          构造参数依次为起始行,截至行,起始列, 截至列
		sheet.addMergedRegion(new CellRangeAddress(0,0,0,5));
		
		HSSFRow row0 = sheet.createRow(1);
		row0.createCell(0).setCellValue("列1");
		row0.createCell(1).setCellValue("列2");
		row0.createCell(2).setCellValue("列3");
		row0.createCell(3).setCellValue("列4");
		row0.createCell(4).setCellValue("列5");
		row0.createCell(5).setCellValue("列6");
		
		HSSFRow row1 = sheet.createRow(2);
		row1.createCell(0).setCellValue("值1");
		row1.createCell(1).setCellValue("值2");
		row1.createCell(2).setCellValue("值3");
		row1.createCell(3).setCellValue("值4");
		row1.createCell(4).setCellValue("值5");
		row1.createCell(5).setCellValue("值6");
		
		HSSFSheet sheet1 = workBook.createSheet("页签2");
		HSSFRow row10 = sheet1.createRow(0);
		row10.createCell(0).setCellValue("列11");
		row10.createCell(1).setCellValue("列12");
		row10.createCell(2).setCellValue("列13");
		
		HSSFRow row11 = sheet1.createRow(1);
		row11.createCell(0).setCellValue("值1");
		row11.createCell(1).setCellValue("值2");
		row11.createCell(2).setCellValue("值3");


		
		FileOutputStream fileOut = null ;
		
		try {
			File srcFile = new File(url);
			File decFile = new File(dec);
			if(srcFile.exists()){  //当文件已存在时,
				//处理方式二:
				//将Excel复制到指定地方在创建新的Excel。
				cutFile(srcFile, decFile);
				
				//处理方式一:
				//删除原有 。导出时,最好关闭原有的Excel,并且打卡新导出的Excel时,最好刷新下当前文件夹,以为重复操作有时会存在缓存。
				srcFile.delete();   
			}
			fileOut = new FileOutputStream(srcFile);
			workBook.write(fileOut);
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				fileOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}
	
	
	/**
	 * 
	 * @Title: cutFile   
	 * @Description: TODO(文件剪切)   
	 * @param: @param src   源文件
	 * @param: @param dec   目标文件  
	 * @return: void      
	 * @throws
	 */
	public static void cutFile(File src, File dec){
		FileOutputStream fileOutputStream = null ;
		InputStream inputStream = null;
		byte[] bytes = new byte[1024];
		int temp = 0;
		
		
		try {
			inputStream = new FileInputStream(src); 
			fileOutputStream = new FileOutputStream(dec);
			
			while((temp = inputStream.read(bytes)) != -1){
				
				fileOutputStream.write(bytes, 0, temp);
				fileOutputStream.flush();
				
			} 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{

			if(inputStream != null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}


	
}

 

 


 

 


 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值