• 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");
        }
  • 相关阅读:
    js实现多物体运动框架并兼容各浏览器
    JAVA-day08 下午-总结、測试
    更改Android应用程序的图标
    codeforces #296 div2 (527C) STL中set的运用
    曼昆微观经济学之征税的市场本质—冰激凌篇
    POJ 1789:Truck History(prim&amp;&amp;最小生成树)
    Android:Activity+Fragment及它们之间的数据交换(一)
    滴滴拉屎之后是什么?
    tmux安装
    IOS开发——Core Graphics &amp; Core Animation
  • 原文地址:https://www.cnblogs.com/101key/p/4003670.html
Copyright © 2020-2023  润新知