標籤:des style blog color io 使用 ar java for
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"); }
jasperreports-5.6 + jaspersoftstudio-5.6 產生pdf 檔案中文無法正常顯示問題