poi 导出word 表格文件
2017-02-15 18:56
597 查看
poi导出word文件,其实很简单,下面我为大家写了一个样例
//创建一个doc对象 XWPFDocument xdoc = new XWPFDocument(); //创建段落对象 XWPFParagraph p = xdoc.createParagraph(); //通过段落对象,创建字体对象。该对象设置字体大小,内容,颜色,是否加粗等。 XWPFRun pRun = p.createRun(); //设计标题 pRun.setText("位置信息"); pRun.setFontSize(11); pRun.setBold(true); //水平居中 p.setAlignment(ParagraphAlignment.LEFT); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); saveDocument(xdoc, "f:/sys_" + System.currentTimeMillis()+ ".doc"); // 创建表格10行1列 XWPFTable table = xdoc.createTable(10, 1); //获取第一行对象(标题行) XWPFTableRow row = table.getRow(0); //获取改行某一列的对象,通过该对象设置每个表格的内容等信息 XWPFTableCell cell = row.getCell(0); //通过cell获取对象,通过段落对象获取内容对象 if(cell.getParagraphs().size()>0){ p=cell.getParagraphs().get(0); }else{ p=cell.addParagraph(); } XWPFRun pRun=p.createRun(); pRun.setText(“图片”); //垂直居中 cell.setVerticalAlignment(XWPFVertAlign.CENTER); //水平居中 p.setAlignment(ParagraphAlignment.LEFT); //后台数据 Map<String, List<DeviceOnlineData>> result=getLocationData("161d3703f1764f789197c627c9576782"); //开始写入数据行 for(int i=0;i<table.getRows().size();i++){ //第一行是标题行,跳过 if(i==0){ continue; } cell=table.getRow(i).getCell(0); //设置列宽度 setCellWidth(cell,"2500"); //给文字添加超链接,\n 是表格内换行 appendExternalHyperlink("ht 4000 tps://www.baidu.com","百度"+"\n", cell); } } //导出到F盘 saveDocument(xdoc, "f:/sys_" + System.currentTimeMillis()+ ".doc"); /** * 设置超链接 */ public void appendExternalHyperlink(String url, String text,XWPFTableCell cell) { XWPFParagraph p=getCellFirstParagraph(cell); String id = p.getDocument().getPackagePart().addExternalRelationship(url,XWPFRelation.HYPERLINK.getRelation()).getId(); CTHyperlink link = p.getCTP().addNewHyperlink(); link.setId(id); CTText ctText = CTText.Factory.newInstance(); ctText.setStringValue(text); CTR ctr = CTR.Factory.newInstance(); CTRPr rpr = ctr.addNewRPr(); //设置超链接样式 CTColor color = CTColor.Factory.newInstance(); color.setVal("0000FF"); rpr.setColor(color); rpr.addNewU().setVal(STUnderline.SINGLE); ctr.setTArray(new CTText[]{ctText}); link.setRArray(new CTR[]{ctr}); //垂直居中 cell.setVerticalAlignment(XWPFVertAlign.CENTER); //水平居中 p.setAlignment(ParagraphAlignment.CENTER); //cell.setText( "<a href='http://www.baidu.com'>"+text+"</a>"); /* // Add the link as External relationship String id = xdoc.getPackagePart().addExternalRelationship(url,XWPFRelation.HYPERLINK.getRelation()).getId(); // Append the link and bind it to the relationship CTHyperlink cLink = xdoc.getLastParagraph().getCTP().addNewHyperlink(); cLink.setId(id); // Create the linked text CTText ctText = CTText.Factory.newInstance(); ctText.setStringValue(text); CTR ctr = CTR.Factory.newInstance(); CTRPr rpr = ctr.addNewRPr(); //设置超链接样式 CTColor color = CTColor.Factory.newInstance(); color.setVal("0000FF"); rpr.setColor(color); rpr.addNewU().setVal(STUnderline.SINGLE); ctr.setTArray(new CTText[] { ctText }); // Insert the linked text into the link cLink.setRArray(new CTR[] { ctr }); setCellContext(cell,ctText); */ //设置字体 /*CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii("微软雅黑"); fonts.setEastAsia("微软雅黑"); fonts.setHAnsi("微软雅黑"); //设置字体大小 CTHpsMeasure sz = rpr.isSetSz() ? rpr.getSz() : rpr.addNewSz(); sz.setVal(new BigInteger("24")); //设置段落居中 paragraph.setAlignment(ParagraphAlignment.CENTER); paragraph.setVerticalAlignment(TextAlignment.CENTER);*/ } }
相关文章推荐
- POI读取word文件的表格数据
- JAVASCRIPT将网页表格导出EXCEL或WORD文件
- poi用于写入,导出excel,word等文件
- poi 导出word,导出表格(复杂表格合并行列)解决方法
- POI读取word文件的表格数据
- poi导出word:包括**普通的段落以及表格**。常用的**api**已经以注释的方式写了进去。
- poi导出word表格jar包
- 把GridView的列表数据导出为Word表格文件
- poi导出word表格的问题
- web项目java后台将html表格导出为word表格文件
- poi导出word:包括**普通的段落以及表格**。常用的**api**已经以注释的方式写了进去。
- poi根据模板导出word(包含图片、动态生成表格、合并单元格)
- Springboot 之 POI导出Word文件
- poi导出word 表格 单元格内换行
- 关于poi导出大数据量EXCEL导出缓慢的解决方案--生成xml格式的表格文件(注意xls格式为03 07下格式会不一样)
- 将网页上的表格导出为Excel文件 或 word文件
- JAVASCRIPT将网页表格导出EXCEL或WORD文件
- Java读取并导出Word中的表格(Excel),导出文件为Excel
- ASP.NET中导出excel/word等类型文件的简单方法
- 7.3 使用POI来处理Excel和Word文件格式