Excel,Pdf,Xml 文件操作(一)

        在实际项目中,经常需要将数据库读出的数据导入到 文件中,而现在用得最多的就是ORM工具,得出的就是一个个对象,而将这些对象中的数据写入文件还需要很多操作.   这里结合java放射技术直接将对象作为参数,然后经过反射解析得出数据然后写入文件。

1。 类设计图(运用工厂设计模式)。

Download

PdfExport

XmlExport

ExcelExport

IExport

ExportFactory

Extends

Extends

Extends

implements

implements

implements

 

 

2 。类实现

Download类提供了通用的文件下载方法并在IExport接口中声明。

/**
  * 文件下载方法
  * @param outfileName,输出显示文件名
  * @param outfile,需要下载的文件名
  * @param response
  */
     public int download(String outfileName,String outfile,HttpServletResponse response);
     /**
   * 文件下载方法
   * @param outfileName,输出显示文件名
   * @param outfile,需要下载的文件名
   * @param response
   */
     public int download(String outfileName,String outfile,WebResponse response);

Download实现:

package org.xiaohongli.common.export;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.tapestry.util.ContentType;
import org.apache.tapestry.web.WebResponse;
import org.xiaohongli.common.Contenttype;

public class Download {
 public  int download(String outfileName, String outfile, HttpServletResponse response) {
  BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        OutputStream fos = null;
        InputStream fis = null;
        try {
            response.setContentType(Contenttype.getContentType(outfileName));
            response.setHeader("Content-disposition", "attachment;filename="
                    + URLEncoder.encode(outfileName, "utf-8"));
            java.io.File file = new java.io.File(outfile);
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            fos = response.getOutputStream();
            bos = new BufferedOutputStream(fos);
            int bytesRead = 0;
            byte[] buffer = new byte[5*1024];
            while ((bytesRead = bis.read(buffer,0,5*1024)) != -1) {
             bos.write(buffer, 0, bytesRead);//将文件发送到客户端
            }
            bos.flush() ;
        }
        catch (IOException e) {
            response.reset();
            return Util.ERROR ;
        }
        finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (bis != null) {
                    bis.close();
                }
            }
            catch (IOException e) {
             return Util.ERROR ;
            }
        }
        return Util.SUCCESS ;
 }

 public  int download(String outfileName, String outfile, WebResponse response) {
  BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        OutputStream fos = null;
        InputStream fis = null;
        try {
          ContentType contentType = new ContentType(Contenttype.getContentType(outfileName));
          response.setHeader("Content-disposition", "attachment;filename="
                     + URLEncoder.encode(outfileName, "utf-8"));
          fos = response.getOutputStream(contentType) ;
            java.io.File file = new java.io.File(outfile);
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            int bytesRead = 0;
            byte[] buffer = new byte[5*1024];
            while ((bytesRead = bis.read(buffer,0,5*1024)) != -1) {
             bos.write(buffer, 0, bytesRead);//将文件发送到客户端
            }
            bos.flush() ;
        }
        catch (IOException e) {
            response.reset();
            return Util.ERROR ;
        }
        finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (bis != null) {
                    bis.close();
                }
            }
            catch (IOException e) {
             return Util.ERROR ;
            }
        }
        return Util.SUCCESS ;
 }
}
Contenttype类提供了解析文件类型,并获得正确的ContentType,实现

package org.xiaohongli.common;

public class Contenttype {
 public static String getContentType(String fileName) {
        String fileNameTmp = fileName.toLowerCase();
        String ret = "";
        if (fileNameTmp.endsWith("txt")) {
            ret = "text/plain";
        }
        if (fileNameTmp.endsWith("gif")) {
            ret = "image/gif";
        }
        if (fileNameTmp.endsWith("jpg")) {
            ret = "image/jpeg";
        }
        if (fileNameTmp.endsWith("jpeg")) {
            ret = "image/jpeg";
        }
        if (fileNameTmp.endsWith("jpe")) {
            ret = "image/jpeg";
        }
        if (fileNameTmp.endsWith("zip")) {
            ret = "application/zip";
        }
        if (fileNameTmp.endsWith("rar")) {
            ret = "application/rar";
        }
        if (fileNameTmp.endsWith("doc")) {
            ret = "application/msword";
        }
        if (fileNameTmp.endsWith("ppt")) {
            ret = "application/vnd.ms-powerpoint";
        }
        if (fileNameTmp.endsWith("xls")) {
            ret = "application/vnd.ms-excel";
        }
        if (fileNameTmp.endsWith("html")) {
            ret = "text/html";
        }
        if (fileNameTmp.endsWith("htm")) {
            ret = "text/html";
        }
        if (fileNameTmp.endsWith("tif")) {
            ret = "image/tiff";
        }
        if (fileNameTmp.endsWith("tiff")) {
            ret = "image/tiff";
        }
        if (fileNameTmp.endsWith("pdf")) {
            ret = "application/pdf";
        }
        return ret;
    }
}
Util.SUCCESS 和Util.ERROR是定义的两个静态常量。标识操作成功和失败。

 

未完待续。。。。。。。。。。。

部分 XML简介 第1 章 XML概览 1.1 什么是XML 1.1.1 XML是元标记语言 1.1.2 XML描述的是结构和语义,而不是格式 1.2 为什么开发人员对XML感到激动 1.2.1 设计与特定领域有关的标记语言 1.2.2 自描述数据 1.2.3 应用间交换数据 1.2.4 结构化和集成的数据 1.3 XML文档的“生命” 1.3.1 编辑器 1.3.2 语法分析程序和处理程序 1.3.3 浏览器和其他工具 1.3.4 处理过程总结 .4 相关技术 1.4.1 超文本标记语言(Hypertext Markup Lan 1.4.2 级联样式单(Cascading Style Sheets) 1.4.3 可扩展的样式语言(Extensible Style Lan 1.4.4 URL和URI 1.4.5 XLink和XPointer 1.4.6 Unicode字符集 1.4.7 如何将这些技术融合在起 1.5 本章小结 第2章 XML应用简介 2.1 什么是XML应用程序 2.1.1 化学标记语言(Chemical Markup Langu 2.1.2 数学标记语言(Mathematical Markup La 2.1.3 频道定义格式 2.1.4 经典文学 2.2 用于XMLXML 2.2.1 XSL 2.2.2 XLL 2.2.3 DCD 2.3 XML的后台应用 2.4 本章小结 第3章 第XML文档 3.1 Hello XML 3.1.1 创建个简单的XML文档 3.1.2 保存XML文件 3.1.3 将XML文件装入Web浏览器 .2 考察简单的XML文档 3.3 赋于XML标记以意义 .4 为XML文档编写样式单 .5 将样式单附加到XML文档上 3.6 本章小结 第4章 数据的结构化 4.1 检查数据 4.1.1 击球手 4.1.2 投球手 4.1.3 XML数据的组织 4.2 数据的XML化 4.2.1 开始编写文档: XML声明和根元素 4.2.2 联赛(League)、(分部) Division和 4.2.3 球员数据的XML化 4.2.4 球员统计数据的XML化 4.2.5 将XML组装在起 4.3 XML格式的优点 4.4 编制样式单以便显示文档 4.4.1 与样式单连接 4.4.2 为根元素指定样式规则 4.4.3 为标题指定样式规则 4.4.4 为球员和统计元素指定样式规则 4.4.5 本节小结 4.5 本章小结 第5章 属性、空标记和XSL 5.1 属性 5.2 属性与元素的对比 5.2.1 结构化的元数据 5.2.2 元元数据 5.2.3 有关元数据的说明 5.2.4 元素更具扩展性 5.2.5 使用属性的最佳时机 5.3 空标记 5.4 XSL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值