数据生成PDF文件_第1页
数据生成PDF文件_第2页
数据生成PDF文件_第3页
数据生成PDF文件_第4页
数据生成PDF文件_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 1. 数据生成PDF:在父页面中加入如下示例方法function createPDF()var flag = ;jQuery.ajax(url : /interface/pdfAjax.jsp,type : post,processData : false,data : sql_pdf=&sql_pdf2=,dataType : xml,async : false,success: function do4Success(msg)tryflag = msg.getElementsByTagName(flag)0.childNodes0.nodeValue;path= msg.getEleme

2、ntsByTagName(path)0.childNodes0.nodeValue;catch(e) );if(flag=1)window.open(path);elsealert(导出PDF失败);打开的页面就是生成的PDF 可以右击另存为或在文件中选择另存为2. 相关jar包iText.jar3. 实例代码package erfaces.tjjc;import java.io.FileOutputStream;import java.io.IOException;import weaver.conn.RecordSet;import weaver.general.Bas

3、eBean;import weaver.general.Util;import pany.DepartmentComInfo;import weaver.hrm.resource.ResourceComInfo;import erfaces.tjjc.KqDateTimeUtil;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lo

4、wagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfPCell;import com.lowagie.text.pdf.PdfPTable;import com.lowagie.text.pdf.PdfWriter;public class PdfTools /* * * 生成PDF的方法 * * return boolean * */public static boolean createPDF(Str

5、ing pdfPath, String sql, String sql2) Document document = new Document();/ 建立一个Document对象document.setPageSize(PageSize.A4);/ 设置页面大小try PdfWriter.getInstance(document, new FileOutputStream(pdfPath);/ 建立一个PdfWriter对象document.open();BaseFont bfChinese = BaseFont.createFont(c:windowsfontssimsun.ttc,1, B

6、aseFont.IDENTITY_H,BaseFont.EMBEDDED);/ 设置中文字体Font titleFont = new Font(bfChinese, 15, Font.BOLD);/ 设置字体大小Font headFont = new Font(bfChinese, 10, Font.BOLD);/ 设置字体大小Font headFont1 = new Font(bfChinese, 10, Font.BOLD);/ 设置字体大小Font headFont2 = new Font(bfChinese, 10, Font.NORMAL);/ 设置字体大小document.add(

7、new Paragraph(缺勤信息, titleFont);float widths = 80f, 80f, 80f, 80f, 80f, 80f, 80f ;/ 设置表格的列宽PdfPTable table = new PdfPTable(widths);/ 建立一个pdf表格table.setSpacingBefore(20f);/ 设置表格上面空白宽度table.setTotalWidth(widths);/ 设置表格的宽度table.setLockedWidth(true);/ 设置表格的宽度固定table.getDefaultCell().setBorder(1);/ 设置表格默认

8、为边框1PdfPCell cell = null;cell = new PdfPCell(new Paragraph(姓名, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(部门, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfP

9、Cell(new Paragraph(日期, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(签到时间, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(签退时间, headFont);cel

10、l.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(缺勤时段, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(缺勤原因, headFont);cell.setHorizontalAlignment(Element.ALI

11、GN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell.setColspan(7);/ cell.setBorder(0);/设置单元格无边框/ cell.setColspan(7);/ 设置合并单元格的列数/ table.addCell(cell);/ 增加单元格RecordSet rs = new RecordSet();RecordSet RecordSet2 = new RecordSet();ResourceComInfo ResourceComInfo = new ResourceComInfo();DepartmentComInfo Dep

12、artmentComInfo = new DepartmentComInfo();KqDateTimeUtil KqDateTimeUtil = new KqDateTimeUtil();rs.execute(sql);while (rs.next() String resourceid = Util.null2String(rs.getString(resourceid);String lastname = ResourceComInfo.getLastname(resourceid);String deptname = DepartmentComInfo.getDepartmentname

13、(ResourceComInfo.getDepartmentID(resourceid);String carddate = Util.null2String(rs.getString(carddate);String maxtime = Util.null2String(rs.getString(maxtime);String mintime = Util.null2String(rs.getString(mintime);String temptypename = ;String typename = ;if (.equals(maxtime) maxtime = 00:00;if (.e

14、quals(mintime) mintime = 00:00;long timelen = KqDateTimeUtil.timeInterval(carddate + + mintime + :00, carddate + + maxtime + :00);System.out.println(s-+ mintime + :00+-x-+ carddate + + maxtime + :00);System.out.println(timelen-+timelen);if (timelen 530) RecordSet2.execute(select type,xtypename,mark,

15、typename from oa_kq_tnjc where userid=+ resourceid+ and kqdate=+ carddate+ and xtype=1);if (RecordSet2.next() String type = Util.null2String(RecordSet2.getString(type); typename = Util.null2String(RecordSet2.getString(typename);String mark = Util.null2String(rs.getString(mark);if (0.equals(type) tem

16、ptypename = 上午; else if (1.equals(type) temptypename = 下午; else if (2.equals(type) temptypename = 全天; else if (00:00.equals(maxtime) & 00:00.equals(mintime) temptypename = 全天; else if (00:00.equals(maxtime)& !00:00.equals(mintime) temptypename = 下午; else if (!00:00.equals(maxtime)& 00:00.equals(mint

17、ime) temptypename = 上午;if(00:00.equals(maxtime)maxtime = ;if(00:00.equals(mintime)mintime = ;cell = new PdfPCell(new Paragraph(lastname, headFont);/ cell.setBorder(0);cell.setFixedHeight(20);/ cell.setColspan(7);/ 设置合并单元格的列数cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示/ cell.setVert

18、icalAlignment(Element.ALIGN_MIDDLE);table.addCell(cell);cell = new PdfPCell(new Paragraph(deptname, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(carddate, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置

19、内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(maxtime, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(mintime, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);ce

20、ll = new PdfPCell(new Paragraph(temptypename, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(typename, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);/B类人员rs.execute(sql2);whil

21、e (rs.next() String resourceid = Util.null2String(rs.getString(resourceid);String lastname = ResourceComInfo.getLastname(resourceid);String deptname = DepartmentComInfo.getDepartmentname(ResourceComInfo.getDepartmentID(resourceid);String carddate = Util.null2String(rs.getString(carddate);String maxt

22、ime = Util.null2String(rs.getString(maxtime);String mintime = Util.null2String(rs.getString(mintime);String mark = Util.null2String(rs.getString(mark);String temptypename = ;String typename=;if (.equals(maxtime) maxtime = 00:00;if (.equals(mintime) mintime = 00:00;long timelen = KqDateTimeUtil.timeI

23、nterval(carddate + + mintime + :00, carddate + + maxtime + :00);if (timelen 530) RecordSet2.execute(select type,xtypename,mark,typename from oa_kq_tnjc where userid=+ resourceid+ and kqdate=+ carddate+ and xtype=1);if (RecordSet2.next() String type = Util.null2String(RecordSet2.getString(type);typen

24、ame = Util.null2String(RecordSet2.getString(typename);if (0.equals(type) temptypename = 上午; else if (1.equals(type) temptypename = 下午; else if (2.equals(type) temptypename = 全天; else if (00:00.equals(maxtime) & 00:00.equals(mintime) temptypename = 全天; else if (00:00.equals(maxtime)& !00:00.equals(mi

25、ntime) temptypename = 下午; else if (!00:00.equals(maxtime)& 00:00.equals(mintime) temptypename = 上午;if(00:00.equals(maxtime)maxtime = ;if(00:00.equals(mintime)mintime = ;cell = new PdfPCell(new Paragraph(lastname, headFont);/ cell.setBorder(0);cell.setFixedHeight(20);/ cell.setColspan(7);/ 设置合并单元格的列数

26、cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示/ cell.setVerticalAlignment(Element.ALIGN_MIDDLE);table.addCell(cell);cell = new PdfPCell(new Paragraph(deptname, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragraph(carddate, headFont);cell.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示table.addCell(cell);cell = new PdfPCell(new Paragr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论