Jasper用中文报道pdf

时间:2014-06-28 13:54:25

标签: java database

在我的项目中,我使用jasper报告来获取pdf和excel。当我从数据库导出英文字符时,报告工作正常,但是当我导出中文字符时,报告适用于excel,但pdf中没有显示中文字符。

我为adobe reader 11安装了简体中文和繁体中文的语言包。

这是调用reportfinal.build()函数的代码,用于构建报告:

 public String OrgReport(){
        List<String> a = new ArrayList<String>();
        a.add("ORG_NAME");
        a.add("ORG_CODE");
        a.add("REGISTRATION_NO");
        a.add("CONTACT_PERSON");
        a.add("EMAIL_OFC");
        a.add("PHONE_NO_OFC");
        a.add("AI_STATUS_ID");

        List<String> colname = new ArrayList<String>();
        colname.add("Organisation Name");
        colname.add("Organisation Code");
        colname.add("Registration No");
        colname.add("Contact Person");
        colname.add("Email");
        colname.add("Contact No");
        colname.add("Status");



        String tabname="ORG_MASTER_MSYS";
        List<Map<String,?>> OrgData= new ArrayList<Map<String,?>>();
        OrgData=organisationManager.getOrgdata(a,tabname);


    List<ColumnValuemMapperDTO> columnValuemMapperDTOlist;
        columnValuemMapperDTOlist=new ArrayList<ColumnValuemMapperDTO>();
         for(int i =0;i<OrgData.size();i++){
             ColumnValuemMapperDTO ob=new ColumnValuemMapperDTO();
             ob.setColumnValueMap(OrgData.get(i));
             columnValuemMapperDTOlist.add(ob);
         }
        UserDTO userDto = (UserDTO) getServletRequest().getSession()
                .getAttribute("LoginVO");
        String Username1 = "";

         Username = Username1.concat("" + userDto.getFname() + " "
                    + userDto.getLname() + "");

        AdhocCustomizerReportWeb reportfinal = new AdhocCustomizerReportWeb();

        String reportName = "Organisation_Master_Grid";
        String Layout = "land";
        String beforedate = "Notdefined";
        String currentdate = "Notdefined";

        String reportHead = "Organisation Master";
        String filepath;
        try {


            reportfinal.build( colname, columnValuemMapperDTOlist,
                    reportName, formatId, reportHead, Layout, Username,
                    beforedate, currentdate);
            filepath = reportfinal.getFilepath();
            int index = filepath.lastIndexOf("\\");

            filename = filepath.substring(index + 1);
         fileInputStream = new FileInputStream(new File(filepath));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ERROR;
        }

     return SUCCESS;

接下来我还提供了构建函数的代码: -

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.dynamicreports.adhoc.AdhocManager;
import net.sf.dynamicreports.adhoc.configuration.AdhocColumn;
import net.sf.dynamicreports.adhoc.configuration.AdhocConfiguration;
import net.sf.dynamicreports.adhoc.configuration.AdhocReport;
import net.sf.dynamicreports.adhoc.report.DefaultAdhocReportCustomizer;
import net.sf.dynamicreports.jasper.base.export.JasperPdfExporter;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.jasper.builder.export.Exporters;
import net.sf.dynamicreports.jasper.builder.export.JasperHtmlExporterBuilder;
import net.sf.dynamicreports.jasper.builder.export.JasperPdfExporterBuilder;
import net.sf.dynamicreports.jasper.builder.export.JasperXlsExporterBuilder;
import net.sf.dynamicreports.jasper.builder.export.JasperXlsxExporterBuilder;
import net.sf.dynamicreports.jasper.constant.JasperProperty;
import net.sf.dynamicreports.report.builder.ReportBuilder;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;

import com.vin.mdm.service.report.ColumnValuemMapperDTO;
import com.vin.mdm.web.VinMDMActionSupport;

public class AdhocCustomizerReportWeb extends VinMDMActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public String reportName1 = null;

    public String reportHead1 = null;
    public String username = null;
    public String layout = null;
    public String Beforedate = null;
    public String Currentdate = null;
    String filepath = null;
    String filename = null;

    public void build(List<String> p_columnNamesList,
            List<ColumnValuemMapperDTO> p_columnValueMapperList,
            String reportName, String formatId, String reportHead,
            String pagelayout, String Username, String beforedate,
            String currentdate) throws IOException {

        try {

            AdhocConfiguration configuration = new AdhocConfiguration();
            AdhocReport report = new AdhocReport();

            configuration.setReport(report);

            List<Map<String, ?>> dsFinalMap = new ArrayList<Map<String, ?>>();

            for (String columnName : p_columnNamesList) {

                AdhocColumn column = new AdhocColumn();
                column.setName(columnName);

                report.addColumn(column);
            }

            for (com.vin.mdm.service.report.ColumnValuemMapperDTO columnValueMapper : p_columnValueMapperList) {

                dsFinalMap.add(columnValueMapper.getColumnValueMap());

            }
            JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(
                    dsFinalMap);
            JRBeanCollectionDataSource datahtml = new JRBeanCollectionDataSource(
                    dsFinalMap);



            reportName1 = reportName;
            reportHead1 = reportHead;
            layout = pagelayout;
            username = Username;
            Beforedate = beforedate;
            Currentdate = currentdate;
            if (formatId.equals("Pdf")) {

                SimpleDateFormat sim = new SimpleDateFormat("ddMMyyyyhhmmssaa");

                JasperReportBuilder reportBuilder = null;

                reportBuilder = AdhocManager.createReport(
                        configuration.getReport(), new ReportCustomizer());


                File tempFile = File.createTempFile("Report_" + reportName
                        + "_" + sim.format(new Date()), ".pdf");
                JasperPdfExporterBuilder pdfExporter = Exporters.pdfExporter(
                        tempFile)

                .setIgnorePageMargins(false).setEncrypted(false).setCharacterEncoding("Identity-H");
                reportBuilder


                        .setReportName(reportName)

                        .setDataSource(dataSource).toPdf(pdfExporter);

                filepath = tempFile.getAbsolutePath();

}

0 个答案:

没有答案