当前位置:软件学习 > Excel >>

页面列表导出cvs,excel、pdf报表.


        在平常的开发中我们常常遇到不仅仅只是导出excel报表的情况。有时候也需要导出pdf或者CSV报表。其实原理都差不多。刚开始本来不打算也这篇博客介绍这个的。感觉这篇博客和前面的博客有点雷同。原理基本都一样。但想了想。有时候可能有些童鞋遇到这样的需求会无从下手。所以还是记录下来。帮助一下那些需要这个需求的童鞋。如果你对前面几篇博客的原理都搞明白了。这篇博客你完全可以不看了。仅仅只是代码的实现不同而已。好了。下面我们来看一下需求吧。

这个图就是我们的需求
                            


     就像你看到的一样。我们的需求就是列表内容是从数据库中读出来的。而我们想把从数据库得到的这个列表导出pdf、csv、excel报表。也不多说了。看代码吧:

[java] 
<pre name="code" class="java">package com.bzu.csh; 
 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.List; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import jxl.Workbook; 
import jxl.write.Label; 
import jxl.write.WritableFont; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
 
import org.apache.struts2.ServletActionContext; 
 
import com.lowagie.text.Document; 
import com.lowagie.text.Element; 
import com.lowagie.text.Font; 
import com.lowagie.text.PageSize; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.PdfPTable; 
import com.lowagie.text.pdf.PdfWriter; 
import com.opensymphony.xwork2.Action; 
 
public class downloadAction implements Action { 
 
    private String downType; 
 
    public String getDownType() { 
        return downType; 
    } 
 
    public void setDownType(String downType) { 
        this.downType = downType; 
    } 
 
    public String execute() { 
        // TODO Auto-generated method stub 
        HttpServletRequest request = ServletActionContext.getRequest(); 
        //HttpServletResponse response = ServletActionContext.getResponse(); 
        //此处模拟数据库读出的数据。在真正的项目中。我们可以通过在session中保存的前端数据集合替换这里 
        List<Person> list = new ArrayList<Person>(); 
        for (int i = 1; i < 6; i++) { 
            Person person = new Person(); 
            person.setId(String.valueOf(i)); 
            person.setName(String.valueOf((char) (i + 64))); 
            person.setAge(i + 20); 
            person.setSex("man"); 
            list.add(person); 
        } 
        OutputStream os = null; 
        String fname = "personlist"; 
        if ("PDF".equals(downType)) { 
            try { 
            //  response.reset(); 
            //  os = response.getOutputStream(); 
                FileOutputStream out = new FileOutputStream("d://a.pdf"); 
                Document document = new Document(PageSize.A4, 50, 50, 50, 50); 
            //  response.setContentType("application/pdf"); 
            //  response.setHeader("Content-disposition", 
            //          "attachment;filename=" + fname + ".pdf"); 
                ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
                PdfWriter.getInstance(document, out); 
                document.open(); 
                int cols = list.size(); 
                // 创建PDF表格 
                PdfPTable table = new PdfPTable(4); 
                // 设置pdf表格的宽度 
                table.setTotalWidth(500); 
                // 设置是否要固定其宽度 
                table.setLockedWidth(true); 
                // 表头字体 
                Font thfont = new Font(); 
                // 设置表头字体的大小 
                thfont.setSize(7); 
                // 设置表头字体的样式 
                thfont.setStyle(Font.BOLD); 
                Font tdfont = new Font(); 
                tdfont.setSize(7); 
                tdfont.setStyle(Font.NORMAL); 
    &nbs

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,