精华内容
下载资源
问答
  • I want to create a table in Word with POI-HWPF (e.g. doc format).My example code is:Table table = document.getRange().insertTableBefore((short) 2, 2);The table is inserted, but I can't see it - as if ...

    I want to create a table in Word with POI-HWPF (e.g. doc format).

    My example code is:

    Table table = document.getRange().insertTableBefore((short) 2, 2);

    The table is inserted, but I can't see it - as if the table has the width 0.

    Can anybody help me?

    解决方案

    The file linked in this old bug report should give you an idea how to do it.

    So in essence: You probably need to add some content (i.e. a paragraph in a cell) so that Word has something to render.

    Here is the example code used in the bug report:

    private static void test (int rows, int columns) throws Exception {

    // POI apparently can't create a document from scratch,

    // so we need an existing empty dummy document

    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("empty.doc"));

    HWPFDocument doc = new HWPFDocument(fs);

    Range range = doc.getRange();

    Table table = range.insertBefore(new TableProperties(columns), rows);

    for (int rowIdx=0; rowIdx

    TableRow row = table.getRow(rowIdx);

    System.out.println("row "+rowIdx);

    for (int colIdx=0; colIdx

    TableCell cell = row.getCell(colIdx);

    System.out.println("column "+colIdx+", num paragraphs "+cell.numParagraphs());

    try {

    Paragraph par = cell.getParagraph(0);

    par.insertBefore(""+(rowIdx*row.numCells()+colIdx));

    } catch (Exception ex) {

    ex.printStackTrace();

    }

    }

    }

    展开全文
  • 网上没查到相关能用的经验,于是自己去扒的poi官网查到的,这里做个记录,同时分享给大家,后面有时间会再写篇博客介绍poi替换word模板,涉及文本替换,插入表格,插入图片等。 设置表格边框样式 /** * 设置表格...

    Backgroud

    网上没查到相关能用的经验,于是自己去扒的poi官网查到的,这里做个记录,同时分享给大家,后面有时间会再写篇博客介绍poi替换word模板,涉及文本替换,插入表格,插入图片等。

    先贴张效果图

    在这里插入图片描述

    设置表格边框样式

    /**
     * 设置表格边框样式
     */
    private static void formatTableBolder(XWPFTable table) {
        // 双实线边框
        String bolderType = "double";
        CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
        // 设置内边框样式,默认单实线
    //        CTBorder hBorder = borders.addNewInsideH();
    //        hBorder.setVal(STBorder.Enum.forString(bolderType));
    //        hBorder.setSz(new BigInteger("1")); // 线条大小
    //        hBorder.setColor("000000"); // 设置颜色
    //
    //        CTBorder vBorder = borders.addNewInsideV();
    //        vBorder.setVal(STBorder.Enum.forString(bolderType));
    //        vBorder.setSz(new BigInteger("1"));
    //        vBorder.setColor("000000");
    
        // 设置上下左右外边框
        CTBorder lBorder = borders.addNewLeft();
        lBorder.setVal(STBorder.Enum.forString(bolderType));
        lBorder.setSz(new BigInteger("1"));
        lBorder.setColor("000000");
    
        CTBorder rBorder = borders.addNewRight();
        rBorder.setVal(STBorder.Enum.forString(bolderType));
        rBorder.setSz(new BigInteger("1"));
        rBorder.setColor("000000");
    
        CTBorder tBorder = borders.addNewTop();
        tBorder.setVal(STBorder.Enum.forString(bolderType));
        tBorder.setSz(new BigInteger("1"));
        tBorder.setColor("000000");
    
        CTBorder bBorder = borders.addNewBottom();
        bBorder.setVal(STBorder.Enum.forString(bolderType));
        bBorder.setSz(new BigInteger("1"));
        bBorder.setColor("000000");
    }
    

    设置表格居中

    这里是表格整体居中,不是表格内容居中。

    XWPFTable table = doc.createTable(2,5);
    // 设置表格居中
    table.setTableAlignment(TableRowAlign.CENTER);
    
    展开全文
  • 应公司业务要求,需要在world文档中生成表格,最开始同事使用的freemarker操作xml标签可以实现world中插入表格, 但是每次修改模板需要改xml文件,几万行中去找对应的标签然后修改,容易找错位置并且电脑非常卡,相当麻烦;...
  • 尝试书签左对齐

    尝试书签左对齐

    展开全文
  • I am trying to insert a table in Excel using Java Apache Poi. But when I am opening the xlsx file it is throwing the following error and I could not solve it:Removed Part: /xl/tables/table1.xml part w...

    I am trying to insert a table in Excel using Java Apache Poi. But when I am opening the xlsx file it is throwing the following error and I could not solve it:

    Removed Part: /xl/tables/table1.xml part with XML error. (Table) Load error. Line 2

    repaired records: table from /xl/tables/table1.xml part (table)

    My code is the following:

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import org.apache.poi.xssf.usermodel.XSSFSheet;

    import org.apache.poi.xssf.usermodel.XSSFTable;

    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;

    public class Test {

    public static void main(String[] args) throws FileNotFoundException, IOException {

    XSSFWorkbook workbook = new XSSFWorkbook();

    XSSFSheet sheet = workbook.createSheet("Architecture");

    XSSFTable table = sheet.createTable();

    CTTable cttable = table.getCTTable();

    cttable.setDisplayName("Table1");

    cttable.setId(1);

    cttable.setName("Test");

    cttable.setRef("A1:C11");

    cttable.setTotalsRowShown(false);

    CTTableStyleInfo styleInfo = cttable.addNewTableStyleInfo();

    styleInfo.setShowColumnStripes(false);

    styleInfo.setShowRowStripes(true);

    CTTableColumns columns = cttable.addNewTableColumns();

    columns.setCount(3);

    for (int i = 1; i <= 3; i++) {

    CTTableColumn column = columns.addNewTableColumn();

    column.setId(i);

    column.setName("Column" + i);

    }

    try (FileOutputStream outputStream = new FileOutputStream("C:\\Office\\TimeSheet\\JavaBooks.xlsx")) {

    workbook.write(outputStream);

    }

    }

    }

    How can I insert a table in Microsft Excel using Apache Java Poi?

    解决方案

    There must be at least content in sheet cells for table column names. In your case cells A1:C1 in sheet Architecture must have content. In former versions of apache poi this content had must match the table column names. In current version now the setting the cell content updates the table column names.

    Your code extended to work:

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import org.apache.poi.xssf.usermodel.XSSFSheet;

    import org.apache.poi.xssf.usermodel.XSSFTable;

    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    import org.apache.poi.xssf.usermodel.XSSFRow;

    import org.apache.poi.xssf.usermodel.XSSFCell;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;

    import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;

    public class ExcelTableTest {

    public static void main(String[] args) throws FileNotFoundException, IOException {

    XSSFWorkbook workbook = new XSSFWorkbook();

    XSSFSheet sheet = workbook.createSheet("Architecture");

    XSSFTable table = sheet.createTable();

    //XSSFTable table = sheet.createTable(null); //since apache poi 4.0.0

    CTTable cttable = table.getCTTable();

    cttable.setDisplayName("Table1");

    cttable.setId(1);

    cttable.setName("Test");

    cttable.setRef("A1:C11");

    cttable.setTotalsRowShown(false);

    CTTableStyleInfo styleInfo = cttable.addNewTableStyleInfo();

    styleInfo.setName("TableStyleMedium2");

    styleInfo.setShowColumnStripes(false);

    styleInfo.setShowRowStripes(true);

    CTTableColumns columns = cttable.addNewTableColumns();

    columns.setCount(3);

    for (int i = 1; i <= 3; i++) {

    CTTableColumn column = columns.addNewTableColumn();

    column.setId(i);

    column.setName("Column" + i);

    }

    for (int r = 0; r < 2; r++) {

    XSSFRow row = sheet.createRow(r);

    for(int c = 0; c < 3; c++) {

    XSSFCell cell = row.createCell(c);

    if(r == 0) { //first row is for column headers

    cell.setCellValue("Column"+ (c+1)); //content **must** be here for table column names

    } else {

    //cell.setCellValue("Data R"+ (r+1) + "C" + (c+1));

    }

    }

    }

    try (FileOutputStream outputStream = new FileOutputStream("ExcelTableTest.xlsx")) {

    workbook.write(outputStream);

    }

    }

    }

    展开全文
  • 一、引入相关jar <dependency>...org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> 二...
  • java使用poi操作.doc word模板替换,循环插入表格
  • poi实现表格导入插件

    2017-04-11 16:12:28
    poi实现表格导入插件
  • // 获取到刚刚插入的行 XWPFTableRow row1 = table1.getRow(0); // 设置单元格内容 row1.getCell(0).setText("字段名"); row1.getCell(1).setText("字段说明"); row1.getCell(2).setText("数据类型"); row1.getCell...
  • 特定位置插入表格、段落、图片 思路 在word中做个标记,通常这个标记独自占据一个段落,例如 标记示例 我们想要在标记处插入一个表格,一个段落,一幅图片,其中插入段落可以使用上话提到的文本替换方式,...
  • POI excel插入图表

    千次阅读 2016-12-12 15:37:44
    POI excel插入图表据我所知POI插入表格好像没有对应的API,所以想到一个折中的方法:借用JFreeChart生成表格之后转化为图片,进而插入到excel中。首先定义接口public interface ChartToImgMaker { //用于配置图表...
  • 1.引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version> ...
  • java poi 导出表格

    2020-06-13 17:11:08
    org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <
  • POI替换表格

    2021-08-02 13:39:22
    // 替换多个表格里面的变量并插入数据 this.insertValueToTables(doc, params); OutputStream os = new FileOutputStream(outFile); doc.write(os); this.close(os); this.close(is); } public static boolean ...
  • poi操作表格table

    千次阅读 2017-12-11 17:03:05
    表格对于页面的布局具有重大的意义,正因为其灵活性,所以用模板引擎处理word中的Table时,显得并不是那么简单,本文将讨论如何利用poi-tl(1.2.0版本)提供的工具来简化表格处理。关于如何使用poi-tl,参见中文Wiki
  • poi生成word文档,包含插入表格

    千次阅读 2018-03-26 09:53:05
    import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; ...
  • poi打印表格

    2017-11-03 00:51:43
    POI分页符有BUG,必须在模板文件中插入一个分页符,然后再此处删除预设的分页符;最后在下面重新设置分页符。 // sheet.setAutobreaks(false); // int iRowBreaks[] = sheet.getRowBreaks(); // sheet....
  • POI操作word在标签位置插入表格

    千次阅读 2018-01-31 10:42:16
    XmlCursor cursor = p.getCTP().newCursor(); XWPFParagraph par = p.getDocument().insertNewParagraph(cursor);
  • java poi 创建Excel表格插入数据 首先引入poi jar包 poi.xml <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId&...
  • 使用POI绘制电子表格,从数据库中抽取数据,然后填充到表格中显示
  • 通过word文件模板,可替换模板中变量,同时可以在指定位置插入表格或者图片,生成新的WORD文件;     import java.io.FileInputStream; import java.io.FileNotFoundException; import java.i...
  • java使用poi制作表格-1

    2021-09-05 10:52:27
    2021-09-05poi 使用心得 1、版本 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>...
  • POI 插入图片各种bug解决。

    千次阅读 2018-05-28 11:31:35
    今天测试了POI插入标题,正文,图片等操作,在插图图片的过程中,发现图片并不能正确显示,网上办法一大堆,但是都太麻烦,可能是POI版本的问题,我用的是3.16,添加图片已经可以正常使用了。只不过:XWPFPicture ...
  • import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;...import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apa
  • // 在文档中需要替换插入表格的位置 XWPFDocument doc = null ; File targetFile = null ; try { doc = new XWPFDocument(POIXMLDocument.openPackage(srcPath)); List <XWPFParagraph> paragraphList...
  • poi版本选3.10以上的 要不然插入图片 word会打不开 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11</version&...
  • Apache POI [1] 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,556
精华内容 1,822
关键字:

poi插入表格