1. 程式人生 > >iReport學習一:中文字型顯示

iReport學習一:中文字型顯示

基本的東西請看下面連結:

http://www.open-open.com/doc/list/115

看完上面的文件對於基本iReport都瞭解的差不多了吧!接下來看看中文字型設定

1、先下載需要的字型檔案按如下步驟操作:工具——》選項——》font字型



2、將字型匯出成jar包


3、將jar開啟把裡面的 檔案考到專案中即可如圖:


程式程式碼:

package org.share.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

import org.share.domain.tree.entity.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/CreatePDf")
public class CreatePDf {
	
	@RequestMapping(value="/c",method=RequestMethod.GET)
	public void createFilePdf(HttpServletRequest request){
		
		//路徑動態錄入需要生產的pdf檔案位置
		String fileepath=request.getSession().getServletContext().getRealPath("template/repConReport.pdf");
		 FileOutputStream out=null;
		 InputStream input=null;
		try{
			//判斷檔案是否存在
			  File f=new File(fileepath);
			  if(!f.exists()){
				  f.createNewFile();
			  }
			  
			  Map<String, Object> parameters=new HashMap<String, Object>();
			  parameters.put("title", "張散彈");
			  parameters.put("head", "nininn好好好好");
			  //模板檔案
			  String ctxpathIreport=request.getSession().getServletContext().getRealPath("jasper/report3.jasper");
			  File reportFile=new File(ctxpathIreport);
//			  JasperReport jasperReport=(JasperReport)JRLoader.loadObject(reportFile);	
//		      JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, parameters);
		    input = new FileInputStream(reportFile);
		    out=new FileOutputStream(f);
		    //前面兩個都是流  parameters 設定頁面上的資料 最後一個數據源為空 
		    JasperRunManager.runReportToPdfStream(input, out, parameters, new JREmptyDataSource());
           //JasperExportManager.exportReportToPdfFile(jasperPrint,fileepath);
		  }catch (Exception e) {
			e.printStackTrace();
		  }finally{
			  //關閉流
			  try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			  try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		  }
	}
	
	
	@RequestMapping(value="/cs",method=RequestMethod.GET)
	public void createFilePdfs(HttpServletRequest request){
		
		//路徑動態錄入需要生產的pdf檔案位置
		String fileepath=request.getSession().getServletContext().getRealPath("template/repConReport.pdf");
		//String fileepaths=request.getSession().getServletContext().getRealPath("template/s.html");
		try{
			//判斷檔案是否存在
			  File f=new File(fileepath);
			  if(!f.exists()){
				  f.createNewFile();
			  }
			  //自定義資料來源
			  Map<String, Object> parameters=new HashMap<String, Object>();
			  parameters.put("title", "張散彈");
			  parameters.put("head", "nininn好好好好");
			  List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
			  list.add(parameters);
			  list.add(parameters);
			  list.add(parameters);
 			  JRDataSource data= new JRBeanCollectionDataSource(list);
			  
			// File reportFile=new File(reportFilePath);
			  //模板檔案
			  String ctxpathIreport=request.getSession().getServletContext().getRealPath("jasper/contract.jasper");
			  File reportFile=new File(ctxpathIreport);
			  //實際中編譯報表很耗時,採用Ireport編譯好的報表
			  JasperReport jasperReport=(JasperReport)JRLoader.loadObject(reportFile);	
			  //填充資料
		      JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, parameters, data);
		      //匯出pdf檔案
		      JasperExportManager.exportReportToPdfFile(jasperPrint,fileepath);
		      //JasperExportManager.exportReportToHtmlFile(jasperPrint, "");
		      
		  }catch (Exception e) {
			e.printStackTrace();
		  }
	}
	
	
	@RequestMapping(value="/csd",method=RequestMethod.GET)
	public void createFilePdfsd(HttpServletRequest request){
		//路徑動態錄入需要生產的pdf檔案位置
		String fileepath=request.getSession().getServletContext().getRealPath("template/repConReport.pdf");
		try{
			//判斷檔案是否存在
			  File f=new File(fileepath);
			  if(!f.exists()){
				  f.createNewFile();
			  }
			  //自定義資料來源
			  Map<String, Object> parameters=new HashMap<String, Object>();

			  //獲取Javabean list
			  List<UserInfo> userList= UserInfo.getListUserInfo();
			  //資料來源為javabean
 			  JRDataSource data= new JRBeanCollectionDataSource(userList);
 			  //設定資料
 			  parameters.put("beans", data);
			  //模板檔案
			  String ctxpathIreport=request.getSession().getServletContext().getRealPath("jasper/report5.jasper");
			  File reportFile=new File(ctxpathIreport);
			  //實際中編譯報表很耗時,採用Ireport編譯好的報表
			  JasperReport jasperReport=(JasperReport)JRLoader.loadObject(reportFile);	
			  //填充資料
		      JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
		      //匯出pdf檔案
		      JasperExportManager.exportReportToPdfFile(jasperPrint,fileepath);
		  }catch (Exception e) {
			e.printStackTrace();
		  }
	}
}

這樣中文就設定好了 打印出來中文也有了