EXCEL转PDF,JACOB,生成checkbox
2015-08-28 11:19
330 查看
因为项目报表有EXCEL导出,现在要新增PDF导出,我就想用EXCEL转PDF,于是找到JCOB调用COM组件,但是发现JCOB不能在LINUX下运行。同时JCOB也有个坏处。如果EXCEL里面表格不是自动换行,里面的字段也显示不全。必须严格调整EXCEL的格式。(导入jacob.jar (1.9),同时将jacob.dll放入到jdk目录下jre/bin下面)
ActiveXComponent app = new ActiveXComponent("Excel.Application"); String els = “D:\\aaaa.xlsx”; String pdf = “D:\\pdf.pdf”; try{ app.setProperty("Visible",false); Dispatch workbooks = app.getProperty("Workbooks").toDispatch(); Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{els, new Variant(false),new Variant(false)}, new int[3]).toDispatch(); Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { pdf, new Variant(57), new Variant(false),new Variant(57), new Variant(57), new Variant(false),new Variant(true), new Variant(57), new Variant(true),new Variant(true), new Variant(true) }, new int[1]); Variant f = new Variant(false); System.out.println("to pdf" + pdf); Dispatch.call(workbook, "Close", f); }catch(Exception e){ e.printStackTrace(); }finally{ if(null!=app){ app.invoke("Quit", new Variant[] {}); } }
于是找到Itext,决定用读POI读EXCEL方式,再写入生成PDF, 在官网上下载了5.5.6还有对应的中文itext-asian.jar包。对应的jar我放在附件里面。用POI读的方式也有个缺点,要非常精准确定行数读取,报表里面有表格还有段落。有点繁琐。
1. 需使用中文字体
Bas 4000 eFont bfChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); Font f8 = new Font(bfChinese,8, Font.NORMAL); Font f8_bold = new Font(bfChinese,8, Font.BOLD); 加粗:Chunk chunk = new Chunk("显示中文加粗字体",f8_bold); document.add(chunk); 增加下划线:Chunk underline = new Chunk("hello,AJava.org ",f8_bold); underline.setUnderline(0.1f, -1f);
生成table,按钮图片
public class TestTest { public static void main(String[] args) throws Exception{ TestTest test = new TestTest(); Document document = new Document(); PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream("src/test.pdf")); document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font f8 = new Font(bfChinese, 8, Font.NORMAL); Font f8_bold = new Font(bfChinese, 8, Font.BOLD); PdfPTable tab = new PdfPTable(2); tab.setWidthPercentage(100); PdfPCell cell = new PdfPCell(test.createCell(new Phrase("个人客户",f8), new float[]{1,38}, true)); tab.addCell(cell); cell = new PdfPCell(test.createCell(new Phrase("非个人客户",f8), new float[]{1,38}, false)); tab.addCell(cell); document.add(tab); document.close(); } public PdfPTable createCell(Phrase phrase,float[] cells,boolean ischecked)throws Exception{ PdfPTable tab = new PdfPTable(2); tab.setWidths(cells); PdfPCell cell = newPdfPCell(createImage(ischecked)); //居中 cell.setUseAscender(true); cell.setUseDescender(true); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // cell.setPaddingLeft(1f); //左1f cell.setBorder(Rectangle.NO_BORDER); tab.addCell(cell); cell = new PdfPCell(phrase); //居中 cell.setUseAscender(true); cell.setUseDescender(true); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // cell.setPaddingLeft(1f); //左1f cell.setBorder(Rectangle.NO_BORDER); tab.addCell(cell); return tab; } public PdfPTable createImage(boolean ischecked)throws Exception{ PdfPTable table = new PdfPTable(1); Image img = Image.getInstance("src/btn_uncheck.png"); if(ischecked){ img = Image.getInstance("src/btn_checked.png"); } img.scaleToFit(9, 7); PdfPCell cell = new PdfPCell(); cell.addElement(img); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); return table; } }
画图生成checkbox
public void createCheckBox(PdfWriter writer,int llx,int lly,int urx,int ury,String name,boolean ischeck){ try{ RadioCheckField bt = new RadioCheckField(writer, new Rectangle(llx,lly, urx, ury), name, "Yes"); bt.setCheckType(RadioCheckField.TYPE_CHECK); bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN); bt.setBorderColor(BaseColor.BLACK); bt.setBackgroundColor(BaseColor.WHITE); bt.setChecked(ischeck); PdfFormField ck = bt.getCheckField(); writer.addAnnotation(ck); }catch(Exception e){ e.printStackTrace(); } }
用html格式生成pdf,对于html标签有些不能识别,必须严格按要求写html
Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf")); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer,document, new FileInputStream("D:/template.html")); document.close();
相关文章推荐
- Fedora防火墙设置
- Splay树-Codevs 1296 营业额统计
- do.......while(0)
- 深度学习(Deep Learning)算法简介
- 物联网动了谁的奶酪?物联网物品的价值分析
- android 布局xml中 android:fitsSystemWindows="true"
- busybox自带的FTP服务器
- 【笔试】29、数值的整数次方
- [LeetCode] 240 - Search a 2D Matrix II
- 问题:C#发布的项目浏览时出现“Server Application Unavailable”错误;结果:Server Application Unavailable出现的原因及解决方案小结
- 命令创建模型类
- linux下的gdb本地调试及命令解析
- Metasploit(五)--Msfencode命令
- windows下使用curl下载ftp文件(ftp有密码)
- 设置SVN忽略文件和目录(文件夹)
- 一些学习网址记录
- 玩转Bootstrap(基础) -- (8.Bootstrap支持的JavaScript插件)
- 【Material Design视觉设计语言】UI组件设计(五):提示框
- 如何重写EF DBContext 获取链接字符串的方法
- CSS 实现图片灰度效果