用户登录
用户注册

分享至

itextword表格

  • 作者: 疯了10
  • 来源: 51数据库
  • 2020-04-21

itext导出word表格,设置表格虚线边框

这个表格去掉了单元格之间的纵向分隔线 第 第 第一 二 三列 列 列这个表格去掉了单元格之间的横向分隔线横 线竖 线都 没 了这个表格去掉了单元格之间的纵向分隔线和横向分隔线 其实上面的三个表格都有三行三列,隐藏分隔线的诀窍在于rules,察看这三个表格的源代码,我们可以看到标签中都有rules。

它有三个参数(cols,rows,none),当rules=cols时,表格会隐藏纵向的分隔线,这样我们就只能看到表格的行;当rules=rows时,则 隐藏了横向的分隔线,这样我们只能看到表格的列;而当rules=none时,纵向分隔线和横向分隔线将全部隐藏。

itext设置表格行高

public static void main(String[] args) throws Exception { // 创建word文档,并设置纸张的大小 Document document = new Document(PageSize.A4); RtfWriter2.getInstance(document, new FileOutputStream( "D:/test/word.doc")); // 打开document document.open(); // 设置字体,字号,加粗,颜色 Font font = new Font(Font.NORMAL, 20, Font.BOLD, new Color(255, 0, 0)); // 设置新的段落,使其字体为font Paragraph p = new Paragraph("出口合同", font); // 设置段落居中,其中1为居中对齐,2为右对齐,3为左对齐 p.setAlignment(1); // 文档中加入该段落 document.add(p); font = new Font(Font.NORMAL, 16, Font.NORMAL, new Color(0, 0, 0)); p = new Paragraph( " 南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院", font); // 设置段落缩进 p.setIndentationLeft(10); // 设置首行缩进 p.setFirstLineIndent(20f); // 设置段后距和段前距 p.setSpacingAfter(10f); p.setSpacingBefore(100f); document.add(p); p = new Paragraph("徐州审委会", font); document.add(p); // 创建有三列的表格 Table table = new Table(2, 3); // 设置table的边框宽度为0 table.setBorderWidth(1f); table.setAbsWidth("120px"); // 其中1为居中对齐,2为右对齐,3为左对齐 table.setAlignment(2); // table.setPadding(0); // table.setSpacing(0); // 读取图片(参数为gif、jpg、png格式的图片都可以),设置图片大小 Image image = Image.getInstance("D:/test/1.jpg"); // Image img = Image.getInstance(new URL("http://xxx.com/logo.jpg)"); // 设置图片的绝对大小,宽和高 image.scaleAbsolute(50f, 50f); // 设置图片居中显示 image.setAlignment(Image.MIDDLE); // 创建单元格,并且将单元格内容设置为图片 Cell cell = new Cell(image); // 设置单元格边框为0 cell.setBorder(0); // cell.setHeader(true); // cell.setColspan(3);// 设置表格为三列 // cell.setRowspan(3);// 设置表格为三行 table.addCell(cell); // table.endHeaders();// 表头结束 table.addCell(cell); table.addCell(cell); cell = new Cell("我是田有朋"); // cell.setWidth("10px"); table.addCell(cell); // cell.setBorder(1); // 设置垂直居中 cell.setVerticalAlignment(1); // 设置水平居中 cell.setHorizontalAlignment(1); // document.add(new Paragraph("用java生成word文件")); document.add(table); // 关闭document document.close(); }。

itext设置表格无边框怎么设置

1. 通过设置表格的边框,可以实现! public static PdfPCell createCellTL(String value,int align, Font font,int colspan) { //上左有边框的表格:1.表格内容;2.字体;3.对齐方式;4.横跨列数PdfPCell cell = new PdfPCell();//cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align); //水平对齐cell.setColspan(colspan);cell.setPhrase(new Phrase(value,textfont)); //设置表格的短语cell.setPadding(5.0f); //设置内间距cell.disableBorderSide(2); //隐藏下右边框cell.disableBorderSide(8);return cell;}。

java导出word表格

首先我用的技术是 poi 这是代码,一个工具类得调用 public class WordUtil { /** * 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效 * * @param templatePath * 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件 * @param destFilePath * 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx * @param data * 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同 */ public static void exportWordByTemplate(String templatePath, String destFilePath, Map data) { FileOutputStream out = null; XWPFDocument doc = null; try { doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath)); List listRun; List listParagraphs = doc.getParagraphs(); for (int i = 0; i < listparagraphs.size();="" i++)="" {="" listrun="listParagraphs.get(i).getRuns();" for="" (int="" j="0;" j="">< listrun.size();="" j++)="" {="" if="" (data.get(listrun.get(j).gettext(0))="" !="null)" {="" string="" val="data.get(listRun.get(j).getText(0));" listrun.get(j).settext(val,="" 0);="" }="" }="" }="" file="" destfile="new" file(destfilepath);="" if="" (!destfile.getparentfile().exists())="" {="" destfile.getparentfile().mkdirs();="" }="" out="new" fileoutputstream(destfilepath);="" doc.write(out);="" }="" catch="" (ioexception="" e)="" {="" e.printstacktrace();="" }="" finally="" {="" try="" {="" if="" (out="" !="null)" out.close();="" }="" catch="" (ioexception="" e)="" {="" e.printstacktrace();="" }="" }="" }="" *="" *="" 基于模板文件导出="" word="" 文档,该方法支持03格式,但是此方法只能保留文档内容,不能保留文档中的样式和图片,建议将模板使用="" 07="" 的格式保存="" *="" *="" @param="" templatepath="" *="" 模板文件的路径="" *="" @param="" destfilepath="" *="" 导出文件的存放路径,包含文件名,例如,e:/test/小区公告.doc="" *="" @param="" data="" *="" 用来替换文档中预定义的字符串,要求预定义的字符串与="" data="" 中的="" key="" 值要相同="" */="" public="" static="" void="" export03wordbytemplate(string="" templatepath,="" string="" destfilepath,=""> data) { try { WordExtractor doc = new WordExtractor(new FileInputStream( templatePath)); String content = doc.getText(); for (String key : data.keySet()) { content = content.replaceAll(key, data.get(key)); } byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(destFilePath); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { Map maps = new HashMap(); maps.put("appellation", "万达公寓业主:"); maps.put( "main_body", "输出的内容"); maps.put("date", "2013年1月23日"); exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps); } }"E:/sss 2.docx 模板存放的地址。

E:/test/test.doc 新生成的地址。

转载请注明出处51数据库 » itextword表格

软件
前端设计
程序设计
Java相关