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"); }