Java導出pdf文件

Java查詢學生成績生成pdf文件導出pdf,導出學生成績

先看結果

package test;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ncast.common.SessionFactory;
import com.sun.org.glassfish.external.statistics.Statistic;
import com.vodsys.mapper.ExpScoreRecordSumMapper;
import com.vodsys.vo.ExpTranscriptVO;
import com.vodsys.vo.TranscriptUserVO;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by huangy on 2018/11/12.
 */
public class WordTest {

    @Test
    public void test1(){

        try {
            //課程編號
            String courseId = "30";
            // 新建文件
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("G://TEST.pdf"));
            document.open();

            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 中文字體
           // Font fontChineseBold = new Font(bfChinese, 14, Font.BOLD);//內容字體特殊加粗
            Font titleChinese = new Font(bfChinese, 18, Font.BOLD);// 標題字體
        //    Font noteChinese = new Font(bfChinese, 12, Font.BOLD);//設置內容加粗的區
            Font contenttitleChinese = new Font(bfChinese, 12, Font.BOLD);// 內容小標題字體

            SqlSession sqlSession = SessionFactory.getInstance("").openSession();
            // try {
            ExpScoreRecordSumMapper expScoreRecordSumMapper = sqlSession.getMapper(ExpScoreRecordSumMapper.class);
            java.util.List<ExpTranscriptVO> expTranscriptVOS = expScoreRecordSumMapper.getExpTranscript(Integer.parseInt(courseId));

//todo 判斷size > 0
            ExpTranscriptVO  expTranscriptVO = expTranscriptVOS.get(0);

            //設置名字
            Paragraph par = new Paragraph(expTranscriptVO.getExamineName(), titleChinese);
            par.setAlignment(Element.ALIGN_CENTER);
            document.add(par);
            // 每行加空白
            par = new Paragraph(" ", titleChinese);
            par.setAlignment(Element.ALIGN_LEFT);
            document.add(par);

            PdfPTable table3 = new PdfPTable(2);
            int width3[] = {40,60};
            table3.setWidths(width3);
            PdfPCell cell31 = new PdfPCell(new Paragraph("考場名稱:"+expTranscriptVO.getSiteName(),contenttitleChinese));
            PdfPCell cell32 = new PdfPCell(new Paragraph("監考人員:"+expTranscriptVO.getTeacherName(),contenttitleChinese));
            cell31.setBorder(0);
            cell32.setBorder(0);

            table3.addCell(cell31);
            table3.addCell(cell32);

            table3.setHorizontalAlignment(Element.ALIGN_LEFT);

            document.add(table3);
            //加入空行

            PdfPTable table4 = new PdfPTable(2);
            int width4[] = {40,60};
            table4.setWidths(width4);
            PdfPCell cell41 = new PdfPCell(new Paragraph("考試名稱:"+expTranscriptVO.getExamineName(),contenttitleChinese));
            PdfPCell cell42 = new PdfPCell(new Paragraph("考試場次:"+expTranscriptVO.getCourseName(), contenttitleChinese));
            cell41.setBorder(0);
            cell42.setBorder(0);
            table4.addCell(cell41);
            table4.addCell(cell42);
            table4.setHorizontalAlignment(Element.ALIGN_LEFT);
            document.add(table4);

            SimpleDateFormat start = new SimpleDateFormat("HH:mm");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
            //String fileName = System.getProperty("user.dir") + File.separator + "南方電網公司交接班記錄" + sdf.format(new Date()) + ".pdf";

            PdfPTable table5 = new PdfPTable(2);
            int width5[] = {80,20};
            table5.setWidths(width5);
            PdfPCell cell51 = new PdfPCell(new Paragraph("考試時間:"+sdf.format(expTranscriptVO.getStartTime())+"-"+sdf.format(expTranscriptVO.getEndTime()),contenttitleChinese));
            PdfPCell cell52 = new PdfPCell(new Paragraph("", contenttitleChinese));
            cell51.setBorder(0);
            cell52.setBorder(0);
            table5.addCell(cell51);
            table5.addCell(cell52);
            table5.setHorizontalAlignment(Element.ALIGN_LEFT);
            document.add(table5);
            //加入空行
            Paragraph blankRow51 = new Paragraph(18f, " ", contenttitleChinese);
            document.add(blankRow51);

            int col = 5;
            //PdfPCell cell = new PdfPCell();
            PdfPTable table = new PdfPTable(col);

            BaseColor bc = new BaseColor(102, 204, 255);

            //設置表格佔PDF文檔100%寬度
            table.setWidthPercentage(100);
            table.setWidths(new int[]{20,20,20,20,20});


            BaseColor lightGrey01 = new BaseColor(0xCC,0xCC,0xCC);

            PdfPCell cell0 =   toPdfPCell("序號",Element.ALIGN_CENTER);
            cell0.setBackgroundColor(lightGrey01);
            table.addCell(cell0);
            PdfPCell cell1 =   toPdfPCell("學生考號",Element.ALIGN_CENTER);
            cell1.setBackgroundColor(lightGrey01);
            table.addCell(cell1);

            PdfPCell cell2 =   toPdfPCell("學生名稱",Element.ALIGN_CENTER);
            cell2.setBackgroundColor(lightGrey01);
            table.addCell(cell2);

            PdfPCell cell3 =   toPdfPCell("成績",Element.ALIGN_CENTER);
            cell3.setBackgroundColor(lightGrey01);
            table.addCell(cell3);

            PdfPCell cell4=   toPdfPCell("學生簽名",Element.ALIGN_CENTER);
            cell4.setBackgroundColor(lightGrey01);
            table.addCell(cell4);
            
            if (expTranscriptVOS.size()>0){
                java.util.List<TranscriptUserVO>  transcriptUserVOS = expTranscriptVOS.get(0).getTranscriptUsers();
                for (int i = 0; i < transcriptUserVOS.size(); i++) {
                    // Map<String, Object> map = (Map<String, Object>) data.get(i);
                    TranscriptUserVO transcriptUserVO  = transcriptUserVOS.get(i);
                    //  table.addCell(toPdfPCell((String.valueOf(i+1)),Element.ALIGN_CENTER));
                    table.addCell(toPdfPCell( transcriptUserVO.getRank().toString(),Element.ALIGN_CENTER));
                    table.addCell(toPdfPCell(transcriptUserVO.getStudentNumber(),Element.ALIGN_CENTER));
                    table.addCell(toPdfPCell(transcriptUserVO.getStudentName(),Element.ALIGN_CENTER));
                    table.addCell(toPdfPCell(transcriptUserVO.getScoreNum().toString(),Element.ALIGN_CENTER));
                    table.addCell(toPdfPCell(null,Element.ALIGN_CENTER));

                }
            }

            document.add(table);


            //(xscj(courseId));


            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }






    public PdfPCell toPdfPCell(String name, int align) throws DocumentException, IOException {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 中文字體
        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);// 內容字體
        PdfPCell cell=new PdfPCell(new Paragraph(name,fontChinese));
        cell.setHorizontalAlignment(align);// 設置內容水平居中顯示
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  // 設置垂直居中
        return cell;
    }





    @Test
    public  void testpdf2(/*HttpServletRequest request, HttpServletResponse response*/){



        try

        {


            Document document = new Document();

            PdfWriter.getInstance(document, new FileOutputStream("g://test1.pdf"));
            //設置字體
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

            //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

            com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);

            com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);

            com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);

            com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);

            com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);

            com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);

            com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);



            document.open();

            //test

            Paragraph pg_bt = new Paragraph("深圳市第二中學第七次物理模擬考試",FontChinese18);

            pg_bt.setAlignment(Element.ALIGN_CENTER);

            document.add(pg_bt);



          //  document.add(new Paragraph("報告使用說明:",FontChinese18));



            document.add(new Chunk("考場名稱:", FontChinese11Normal));

            document.add(Chunk.NEWLINE);

            document.add(new Chunk("考試時間:", FontChinese11Normal));

            document.add(Chunk.NEWLINE);

      


            //table7

            PdfPTable pt01 = new PdfPTable(11);

            BaseColor lightGrey01 = new BaseColor(0xCC,0xCC,0xCC);

            int widthpt01[] = {20,20,20,20,20,20,15,20,20,15,15};

            pt01.setWidths(widthpt01);

            PdfPCell cell01 = new PdfPCell(new Paragraph("序號",FontChinese11Bold));

            BaseColor bc = new BaseColor(102, 204, 255);

            cell01.setBackgroundColor(bc);

            PdfPCell cell02 = new PdfPCell(new Paragraph("學生考號",FontChinese11Bold));

            cell02.setBackgroundColor(bc);

            PdfPCell cell03 = new PdfPCell(new Paragraph("學生名稱",FontChinese11Bold));

            cell03.setBackgroundColor(bc);

            PdfPCell cell04 = new PdfPCell(new Paragraph("成績",FontChinese11Bold));

            cell04.setBackgroundColor(bc);

            cell04.setNoWrap(false);

            PdfPCell  cell05= new PdfPCell(new Paragraph("學生簽名",FontChinese11Bold));

            cell05.setBackgroundColor(bc);

            PdfPCell cell06 = new PdfPCell(new Paragraph("學生簽名",FontChinese11Bold));

            cell06.setBackgroundColor(bc);

            PdfPCell cell07 = new PdfPCell(new Paragraph("面積",FontChinese11Bold));

            cell07.setBackgroundColor(bc);

            PdfPCell cell08 = new PdfPCell(new Paragraph("起時間",FontChinese11Bold));

            cell08.setBackgroundColor(bc);

            PdfPCell cell09 = new PdfPCell(new Paragraph("止時間",FontChinese11Bold));

            cell09.setBackgroundColor(bc);

            PdfPCell cell10 = new PdfPCell(new Paragraph("年租金",FontChinese11Bold));

            cell10.setBackgroundColor(bc);

            PdfPCell cell_11 = new PdfPCell(new Paragraph("承租人/出租人",FontChinese11Bold));

            cell_11.setBackgroundColor(bc);





            //表格高度

            //cell01.setFixedHeight(25);



            //水平居中

            cell01.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell02.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell03.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell04.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell05.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell06.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell07.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell08.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell09.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell10.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell_11.setHorizontalAlignment(Element.ALIGN_CENTER);

            //垂直居中

            cell01.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell02.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell03.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell04.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell05.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell06.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell07.setHorizontalAlignment(Element.ALIGN_MIDDLE);
//
            cell08.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell09.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell10.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell_11.setHorizontalAlignment(Element.ALIGN_MIDDLE);



            //邊框顏色

            cell01.setBorderColor(lightGrey01);

            cell02.setBorderColor(lightGrey01);

            cell03.setBorderColor(lightGrey01);

            cell04.setBorderColor(lightGrey01);

            cell05.setBorderColor(lightGrey01);

            cell06.setBorderColor(lightGrey01);

            cell07.setBorderColor(lightGrey01);

            cell08.setBorderColor(lightGrey01);

            cell09.setBorderColor(lightGrey01);

            cell10.setBorderColor(lightGrey01);

            cell_11.setBorderColor(lightGrey01);

            //去掉左右邊框

            /**

             cell71.disableBorderSide(8);

             **/

            pt01.addCell(cell01);

            pt01.addCell(cell02);

            pt01.addCell(cell03);

            pt01.addCell(cell04);

            pt01.addCell(cell05);

            pt01.addCell(cell06);

            pt01.addCell(cell07);

            pt01.addCell(cell08);

            pt01.addCell(cell09);

            pt01.addCell(cell10);

            pt01.addCell(cell_11);

            document.add(pt01);



            PdfPTable pt02 = new PdfPTable(11);

            BaseColor lightGrey02 = new BaseColor(0xCC,0xCC,0xCC);

            int widthpt02[] ={20,20,20,20,20,20,15,20,20,15,15};

            pt02.setWidths(widthpt02);

            PdfPCell cell001 = new PdfPCell(new Paragraph("自用房屋登記",FontChinese11Bold));

            PdfPCell cell002 = new PdfPCell(new Paragraph("無產權證",FontChinese11Bold));

            PdfPCell cell003 = new PdfPCell(new Paragraph("秦淮路19號",FontChinese11Bold));

            PdfPCell cell004 = new PdfPCell(new Paragraph("7464583.31",FontChinese11Bold));

            PdfPCell cell005 = new PdfPCell(new Paragraph("7464583.31",FontChinese11Bold));

            PdfPCell cell006 = new PdfPCell(new Paragraph("62702.5",FontChinese11Bold));

            PdfPCell cell007 = new PdfPCell(new Paragraph("1000",FontChinese11Bold));

            PdfPCell cell008 = new PdfPCell(new Paragraph("2005-01-01",FontChinese11Bold));

            PdfPCell cell009 = new PdfPCell(new Paragraph("",FontChinese11Bold));

            PdfPCell cell100 = new PdfPCell(new Paragraph("",FontChinese11Bold));

            PdfPCell cell011 = new PdfPCell(new Paragraph("",FontChinese11Bold));





            //表格高度

            cell001.setFixedHeight(25);

            cell002.setFixedHeight(25);

            cell003.setFixedHeight(25);

            cell004.setFixedHeight(25);

            cell005.setFixedHeight(25);

            cell006.setFixedHeight(25);

            cell007.setFixedHeight(25);

            cell008.setFixedHeight(25);

            cell009.setFixedHeight(25);

            cell100.setFixedHeight(25);

            cell011.setFixedHeight(25);

            //水平居中

            cell001.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell002.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell003.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell004.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell005.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell006.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell007.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell008.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell009.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell100.setHorizontalAlignment(Element.ALIGN_CENTER);

            cell011.setHorizontalAlignment(Element.ALIGN_CENTER);

            //垂直居中

            cell001.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell002.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell003.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell004.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell005.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell006.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell007.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell008.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell009.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell100.setHorizontalAlignment(Element.ALIGN_MIDDLE);

            cell011.setHorizontalAlignment(Element.ALIGN_MIDDLE);



            //邊框顏色

            cell001.setBorderColor(lightGrey02);

            cell002.setBorderColor(lightGrey02);

            cell003.setBorderColor(lightGrey02);

            cell004.setBorderColor(lightGrey02);

            cell005.setBorderColor(lightGrey02);

            cell006.setBorderColor(lightGrey02);

            cell007.setBorderColor(lightGrey02);

            cell008.setBorderColor(lightGrey02);

            cell009.setBorderColor(lightGrey02);

            cell100.setBorderColor(lightGrey02);

            cell011.setBorderColor(lightGrey02);

            pt02.addCell(cell001);

            pt02.addCell(cell002);

            pt02.addCell(cell003);

            pt02.addCell(cell004);

            pt02.addCell(cell005);

            pt02.addCell(cell006);

            pt02.addCell(cell007);

            pt02.addCell(cell008);

            pt02.addCell(cell009);

            pt02.addCell(cell100);

            pt02.addCell(cell011);

            document.add(pt02);







            document.close();

            //os.flush();

            //   os.close();



        } catch (Exception ex)

        {

            ex.printStackTrace();

        }

        //return null;

    }













}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章