在java jasperreport 導出pdf excel html 文件到本地

import net.sf.jasperreports.charts.design.JRDesignPieDataset;
import net.sf.jasperreports.components.table.BaseColumn;
import net.sf.jasperreports.components.table.DesignCell;
import net.sf.jasperreports.components.table.StandardColumn;
import net.sf.jasperreports.components.table.StandardTable;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.base.JRBaseLineBox;
import net.sf.jasperreports.engine.component.ComponentKey;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package:
 * User: ljie
 * 2016年12月13日 下午4:25
 */

@Controller
@RequestMapping(value = "/reportGenerate")
public class ReportGenerateContorller {

    @RequestMapping(value = "/getReport")
    public void getReport() {
        String sourceFileName = "/home/ljie/workspace12/gx-backend/src/main/resources/jasper/test/enSimple.jasper";
        String printFileName = null;
        DataBean DataBeanList = new DataBean();
        ArrayList dataList = DataBeanList.getDataBeanList();
        JRBeanCollectionDataSource beanColDataSource =
                new JRBeanCollectionDataSource(dataList);

        Map parameters = new HashMap();
        try {
            printFileName = JasperFillManager.fillReportToFile(sourceFileName,
                    parameters, beanColDataSource);
            //JasperExportManager.exportReportToPdfFile(printFileName,
            //        "/home/ljie/桌面/sample_report1.pdf");
            if (printFileName != null) {
                /**
                 * 1- export to PDF
                 */
                JasperExportManager.exportReportToPdfFile(printFileName,
                        "/home/ljie/桌面/sample_report.pdf");

                /**
                 * 2- export to HTML
                 */
                JasperExportManager.exportReportToHtmlFile(printFileName,
                        "/home/ljie/桌面//sample_report.htmll");

                /**
                 * 3- export to Excel sheet
                 */
                JRXlsExporter exporter = new JRXlsExporter();

                exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME,
                        printFileName);
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
                        "/home/ljie/桌面/sample_report.xls");

                exporter.exportReport();
            }
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索