zoukankan      html  css  js  c++  java
  • jasperreports-5.6 + jaspersoftstudio-5.6 生成pdf 文件中文无法正常显示问题

    jrxml字段属性设置:

    <textElement>
       <font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
    </textElement>

    在工程lib中引入itextasian-1.5.2.jar文件,注意该文件的目录结构为com/lowagie/text/pdf/fonts/*,最新的itext-asian.jar包不是该结构无法正常使用,

    JAVA示例代码:

        /****数据库连接****/
        public static Connection getConnection() {
            Connection conn = null;
            String driver = "net.sourceforge.jtds.jdbc.Driver";
            String url = "jdbc:jtds:sqlserver://127.0.0.1:1433/CODE-WH";
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url,"sa", "123");
                return conn;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) throws Exception {
            String fileName = "D:/Coffee.jrxml";
            long startTime = System.currentTimeMillis();
            // 将报表的定义文件*.jrxml编译成*.jasper文件
            String jasperFile = JasperCompileManager.compileReportToFile(fileName);
            // 向*.jasper文件中填充数据,这一步将生产出*.jrprint文件
            String jrprintFile = JasperFillManager.fillReportToFile(jasperFile,
                    null, getConnection());
            System.out.println("jrprintFile=="+jrprintFile);
            // 将.jrprint文件转换成HTML格式
            JasperExportManager.exportReportToHtmlFile(jrprintFile);
            // 将.jrprint文件转换成PDF格式
            JasperExportManager.exportReportToPdfFile(jrprintFile);
            // 将.jrprint文件转换成XML格式
            //JasperExportManager.exportReportToXmlFile(jrprintFile, false);
            // 将.jrprint文件转换成XLS格式(即Excel文件),需要用到POI类库.
            
            File sourceFile = new File(jrprintFile);
            JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
            File destFile = new File(sourceFile.getParent(), jasperPrint.getName()
                    + ".xls");
            JRXlsExporter exporter = new JRXlsExporter();
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
            SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
            configuration.setOnePagePerSheet(false);
            exporter.setConfiguration(configuration);
            exporter.exportReport();
            
            long endTime = System.currentTimeMillis();
            long time = (endTime - startTime) / 1000;
            System.out.println("success with " + time + " s");
        }
  • 相关阅读:
    spring execution表达式用法
    spring2.0中使用Aspect方式进行AOP如何得到method对象
    mybatis if标签判断字符串相等
    Date和Timestamp区别
    正向代理与反向代理[转载]
    oracle中的对象创建及删除语句【原创】
    使用IDEA打war包并导出war包详细步骤
    Java面试题
    电脑上怎么登录几个微信(微信多开)
    批处理快速合并多分Excel文件并将指定列的数据去重复
  • 原文地址:https://www.cnblogs.com/101key/p/4003670.html
Copyright © 2011-2022 走看看