JXL生成excel,从本地图片导入excel等等

<p>public String notifyExcel(List<IllInfo> exportList) throws Exception {</p><p>   System.out.println("in notifyExcel :: ");
   try {
    //
    List<String> pathlst = new ArrayList<String>();
    for (int i = 0; i < exportList.size(); i++) {
     String pushpath = this.illegalInfoMgr.notifyExcel(exportList
       .get(i));
     pathlst.add(pushpath);
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
    String strDatename = sdf.format(new Date());
    DownLoadExeclUtil.downLoadExcel(strDatename, pathlst);
    // DownLoadExeclUtil.downLoadExcel1(pushpath,strDatename);
    return strDatename;
   } catch (Exception e) {
    e.printStackTrace();
    throw e;
   }
  }</p>
<p>----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</p>
 
 
public String notifyExcel(IllInfo illInfo) throws Exception {
		
		String strDatename = Long.toString(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
		try {
			List<VehInfo> lstVehinfo = illInfoDAOExtend.searchNotInfo(illInfo);
			System.out.println("in IllIllegalInfoMgrImpl notifyExcel lstVehinfo: " +  lstVehinfo.size());
			int len = IllIllegalInfoMgrImpl.class.getResource("").getPath().indexOf("WEB-INF");
			String strAtmspath = IllIllegalInfoMgrImpl.class.getResource("").getPath().substring(0, len);
			String strfilepath = strAtmspath + "tempimg/";
			FileOperater fo = new FileOperater();
	        fo.newFolder(strfilepath);

	        String strExcelModle = IllIllegalInfoMgrImpl.class.getResource("").getPath().substring(0, len) + "plugin/Modle.xls" ;
	        System.out.println("原始路径::" + strExcelModle);
	        String strExceltemp = strfilepath + strDatename + ".xls";
	        System.out.println("临时路径::" + strExceltemp);
			Workbook rw = Workbook.getWorkbook(new File(strExcelModle));
			WritableWorkbook wwb = Workbook.createWorkbook(
					new File(strExceltemp), rw);
			WritableSheet ws = wwb.getSheet(0);
			
			//格式化违法时间
			jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd  hh:mm:ss"); 
			jxl.write.DateFormat df2 = new jxl.write.DateFormat("yyyy-MM-dd");
			jxl.write.WritableCellFormat wfsj = new jxl.write.WritableCellFormat(df); 
	        jxl.write.WritableCellFormat notsj = new jxl.write.WritableCellFormat(df2); 
	        wfsj.setAlignment(Alignment.LEFT);
	        wfsj.setBorder(Border.LEFT, BorderLineStyle.THIN);
			
	        WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 
	        		11,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE);
	        //设置边框线
	        WritableCellFormat wc = new WritableCellFormat(font);
	        wc.setAlignment(Alignment.LEFT); // 设置左对齐
//	        wc.setBorder(Border.LEFT, BorderLineStyle.THIN); // 设置边框线
//	        wc.setBorder(Border.RIGHT, BorderLineStyle.THIN); // 设置边框线
	        wc.setWrap(true);//换行
	        wc.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直居中
	        
	        Label labelNAME1 = new Label(1, 1,"" ,wc);
	        Label labelPZ1 =  new Label(3, 2,"",wc); 
	        if(lstVehinfo.size() > 0){
	        	labelNAME1 = new Label(1, 1,lstVehinfo.get(0).getSyr() ,wc); 
				labelPZ1 = new Label(3, 2,lstVehinfo.get(0).getClpp1(),wc); 
	        }
	        Label labelHPHM1 = new Label(6, 2,illInfo.getHphm(),wc); 
//	        jxl.write.DateTime labelWFSJ1 = new jxl.write.DateTime(1, 3, illInfo.getWfsj(), wfsj);
	        Label labelWFSJ1 = new Label(1,3,sdf.format(illInfo.getWfsj()),wc);
	        Label labelLKMC1 = new Label(4, 3,illInfo.getWfddmc(),wc); 
	        Label labelWFLX1 = new Label(9, 3,illInfo.getWfxwmc(),wc); 
	        Label labelJE1 = new Label(2, 4,illInfo.getFkje(),wc); 
	        Label labelQFRQ1 = new Label(9,24,sdf.format(new Date()),wc);
	        
	        String pic1Name = illInfo.getPic1().substring(illInfo.getPic1().lastIndexOf("/") + 1);
	        try {
	        	ImageUtilForExcel.writeFile(illInfo.getPic1(), pic1Name);
	        } catch (IOException e) {
	        	e.printStackTrace();
	        }
	        String imgPath = "d:\\webimg\\"+pic1Name;
	        File imgFile = new File(imgPath);
	        if (imgFile.exists()) {
		        WritableImage image = new WritableImage(1,6,5,16,imgFile);
		        ws.addImage(image);
	        }
	        
	        ws.addCell(labelNAME1); 
	        ws.addCell(labelPZ1); 
	        ws.addCell(labelHPHM1); 
	        ws.addCell(labelWFSJ1); 
	        ws.addCell(labelLKMC1); 
	        ws.addCell(labelWFLX1); 
	        ws.addCell(labelJE1); 
	        ws.addCell(labelQFRQ1);
			
			wwb.write();
			rw.close();
			wwb.close();
		} catch (Exception e) {
			System.out.println("read err:" + e);
		}
		
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties");
        Properties p = new Properties();
        p.load(inputStream);
        String ip = "http://" + p.getProperty("LOCALIP") + ":" + p.getProperty("LOCALPORT") + "/";
		String pushpath = ip + "Atms/tempimg/" + strDatename + ".xls";
        System.out.println("pushpath:::" + pushpath);
		return pushpath;
	}


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.zteits.atms.illegalmgr.util;

import java.io.ByteArrayOutputStream;
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.io.OutputStream;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.misc.BASE64Encoder;

public class ImageUtilForExcel
{
	/**
	 * 日志对象
	 */
	public static final Logger logger = LoggerFactory
			.getLogger(ImageUtilForExcel.class);
    public static void writeFile(String strUrl, String fileName) throws Exception
    {
    	System.out.println("strUrl:"+strUrl+",fileName:"+fileName);
    	
        URL url = null;
        try
        {
            url = new URL(strUrl);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        InputStream is = null;
        try
        {
        	
            is = url.openStream();
        }
        catch(FileNotFoundException fie)
        {
        	return;
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
            throw ie;
        }
        OutputStream os = null;
        File f = new File("d:\\webimg\\");
        if(!f.exists())
        {
            f.mkdirs();
        }
        try
        {
            os = new FileOutputStream("d:\\webimg\\" + fileName);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while((bytesRead = is.read(buffer, 0, 8192)) != -1)
            {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
            os.close();
            is.close();
        }
        catch(FileNotFoundException e)
        {
        }
        catch(IOException e)
        {
            e.printStackTrace();
            throw e;
        }
    }

    public static String getEncodeFromFile(String f)
    {
        if(f == null)
        {
            return null;
        }
        byte[] data = null;
        byte[] temp = null;
        try
        {
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(stream.available());
            data = new byte[stream.available()];
            int n;
            while((n = stream.read(data)) != -1)
            {
                out.write(data, 0, n);
            }
            stream.close();
            out.close();
            temp = out.toByteArray();
        }
        catch(IOException e)
        {
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(temp);
    }

    /**
        * 清除指定路径下的内容
        * @param strPath
        */
    public static void clearImage(String strPath)
    {
        try
        {
            File dir = new File(strPath);
            File[] files = dir.listFiles();
            if(files == null)
            {
                return;
            }
            if(files.length == 0)
            {
                dir.delete();
            }
            for(int i = 0; i < files.length; i++)
            {
                if(files[i].isDirectory())
                {
                    clearImage(files[i].getAbsolutePath());
                }
                else
                {
                    if(getExtensionName(files[i].getName().toLowerCase()).equals("jpg")
                            || getExtensionName(files[i].getName().toLowerCase()).equals("jpeg"))
                    {
                        files[i].delete();
                    }
                }
            }
        }
        catch(Exception e)
        {
        }
    }

    /**
     * 获取最后一个"."之后的字符串数据
     * @param filename
     * @return 最后一个"."之后的字符串数据
     */
    public static String getExtensionName(String filename)
    {
        if((filename != null) && (filename.length() > 0))
        {
            int dot = filename.lastIndexOf('.');
            if((dot > -1) && (dot < (filename.length() - 1)))
            {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值