apache poi 에서 Excel 또는 Pdf 파일 내 보 내기

5380 단어 JavaJavaEE
package com.infrastructure.project.common.util;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import com.infrastructure.project.common.SysConstant;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class ExportPdfUtil {
	public static void exportPdf(String fileName,String filePath,List listLable ,List listData) {
		Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
		try {
			File file = new File(filePath);
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
			Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
			Font FontChina = new Font(bfChinese, 24, Font.NORMAL);
			if (!file.exists()) {
				file.mkdirs();
			}
			PdfWriter.getInstance(document, new FileOutputStream(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_PDF));
			document.open();
			Table datatable = new Table(listLable.size());
			datatable.setWidth(100);
			datatable.setPadding(3);
			Cell cell = new Cell(new Phrase(fileName,FontChina));
			
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setLeading(30);
			cell.setColspan(listLable.size());
			cell.setBorder(Rectangle.NO_BORDER);
			cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
			datatable.addCell(cell);
			datatable.setBorderWidth(2);
			datatable.setAlignment(1);
			for(int i =0;i 
  
 
  
 
  
 
  
 
  
 
  

package poi.itext;

import java.io.FileOutputStream; import java.io.IOException; import java.awt.Color;

import com.lowagie.text.*; import com.lowagie.text.pdf.*; import com.lowagie.text.pdf.BaseFont;

/**  * Pdf  * @author Administrator  *  */

public class HelloPdf {     public static void main(String[] args)throws Exception     {         BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);         Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

        // , document         Rectangle rectPageSize = new Rectangle(PageSize.A4);                  //         //rectPageSize = rectPageSize.rotate();                  // document         Document doc = new Document(rectPageSize,50,50,50,50);         Document document = new Document();         try         {             // , Document PdfWriter             // Document , PDF             PdfWriter.getInstance(document,new FileOutputStream("src/poi/itext/HelloWorld.pdf"));             // 3 ,             document.open();             // 3 , .             document.add(new Paragraph("Hello World"));

            Paragraph par = new Paragraph(" ",FontChinese);             document.add(par);

            PdfPTable table = new PdfPTable(3);             for(int i=0;i<12;i++)             {                 if (i == 0)                 {                     PdfPCell cell = new PdfPCell();                     cell.setColspan(3);                     cell.setBackgroundColor(new Color(180,180,180));                     cell.addElement(new Paragraph(" " , FontChinese));                     table.addCell(cell);                 }                 else                 {                     PdfPCell cell = new PdfPCell();                     cell.addElement(new Paragraph(" " , FontChinese));                     table.addCell(cell);                 }             }             document.add(table);

        }         catch (DocumentException de)         {             System.err.println(de.getMessage());         }         catch (IOException ioe)         {             System.err.println(ioe.getMessage());         }         // document         document.close();                  System.out.println(" HelloPdf !");      }           }

		

좋은 웹페이지 즐겨찾기