zoukankan      html  css  js  c++  java
  • sell-- wordPOI

    1. http://poi.apache.org/

    2.创建项目,结构如下

    三: 查看效果

    打开:

    测试源码:

    /* ====================================================================
       Licensed to the Apache Software Foundation (ASF) under one or more
       contributor license agreements.  See the NOTICE file distributed with
       this work for additional information regarding copyright ownership.
       The ASF licenses this file to You under the Apache License, Version 2.0
       (the "License"); you may not use this file except in compliance with
       the License.  You may obtain a copy of the License at
    
           http://www.apache.org/licenses/LICENSE-2.0
    
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
    ==================================================================== */
    
    import org.apache.poi.xwpf.usermodel.*;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.math.BigInteger;
    import java.util.List;
    
    public class SimpleTable {
    
        public static void main(String[] args) throws Exception {
        	try {
        		createSimpleTable();
                System.out.println("success");
            } catch(Exception e) {
        		System.out.println("Error trying to create simple table.");
        		throw(e);
        	}
    
        	try {
        		createStyledTable();
        	} catch(Exception e) {
        		System.out.println("Error trying to create styled table.");
        		throw(e);
        	}
        }
    
        public static void createSimpleTable() throws Exception {
            XWPFDocument doc = new XWPFDocument();
            try {
                XWPFTable table = doc.createTable(3, 3);
                table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
                XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
                XWPFRun r1 = p1.createRun();
                r1.setBold(true);
                r1.setText("The quick brown fox");
                r1.setItalic(true);
                r1.setFontFamily("Courier");
                r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
                r1.setTextPosition(100);
                table.getRow(2).getCell(2).setText("only text");
    
                OutputStream out = new FileOutputStream("d:/simpleTable.docx");
                try {
                    doc.write(out);
                } finally {
                    out.close();
                }
            } finally {
                doc.close();
            }
        }
    
        public static void createStyledTable() throws Exception {
            XWPFDocument doc = new XWPFDocument();
            try {
                int nRows = 6;
                int nCols = 3;
                XWPFTable table = doc.createTable(nRows, nCols);
    
                CTTblPr tblPr = table.getCTTbl().getTblPr();
                CTString styleStr = tblPr.addNewTblStyle();
                styleStr.setVal("StyledTable");
    
                // Get a list of the rows in the table
                List<XWPFTableRow> rows = table.getRows();
                int rowCt = 0;
                int colCt = 0;
                for (XWPFTableRow row : rows) {
                    // get table row properties (trPr)
                    CTTrPr trPr = row.getCtRow().addNewTrPr();
                    // set row height; units = twentieth of a point, 360 = 0.25"
                    CTHeight ht = trPr.addNewTrHeight();
                    ht.setVal(BigInteger.valueOf(360));
    
                    // get the cells in this row
                    List<XWPFTableCell> cells = row.getTableCells();
                    // add content to each cell
                    for (XWPFTableCell cell : cells) {
                        // get a table cell properties element (tcPr)
                        CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                        // set vertical alignment to "center"
                        CTVerticalJc va = tcpr.addNewVAlign();
                        va.setVal(STVerticalJc.CENTER);
    
                        // create cell color element
                        CTShd ctshd = tcpr.addNewShd();
                        ctshd.setColor("auto");
                        ctshd.setVal(STShd.CLEAR);
                        if (rowCt == 0) {
                            // header row
                            ctshd.setFill("A7BFDE");
                        } else if (rowCt % 2 == 0) {
                            ctshd.setFill("D3DFEE");
                        } else {
                            ctshd.setFill("EDF2F8");
                        }
    
                        // get 1st paragraph in cell's paragraph list
                        XWPFParagraph para = cell.getParagraphs().get(0);
                        // create a run to contain the content
                        XWPFRun rh = para.createRun();
                        // style cell as desired
                        if (colCt == nCols - 1) {
                            // last column is 10pt Courier
                            rh.setFontSize(10);
                            rh.setFontFamily("Courier");
                        }
                        if (rowCt == 0) {
                            // header row
                            rh.setText("header row, col " + colCt);
                            rh.setBold(true);
                            para.setAlignment(ParagraphAlignment.CENTER);
                        } else {
                            // other rows
                            rh.setText("row " + rowCt + ", col " + colCt);
                            para.setAlignment(ParagraphAlignment.LEFT);
                        }
                        colCt++;
                    } // for cell
                    colCt = 0;
                    rowCt++;
                } // for row
    
                // write the file
                OutputStream out = new FileOutputStream("d:/styledTable.docx");
                try {
                    doc.write(out);
                } finally {
                    out.close();
                }
            } finally {
                doc.close();
            }
        }
    }
    
  • 相关阅读:
    .net 中 数据库的查询参数写法。微软其他开发环境同理
    【开源】女人值钱计算器,C++
    远程桌面的端口修改
    .net 中使用ActiveX控件的自动创建的包装器的问题(自动生成的Interop.Ax*Lib.dll)
    ASP.NET站点的同时部署给不同的客户。通过数据库配置站点的Top、版权、站点名称
    当下常见的十大(现在补充了,是十一大手机操作系统)手机(平板)操作系统
    项目外包,类似QQ这样界面的客户端,要求界面漂亮,功能是帮助客户完成在线业务的功能。
    stdoled.dll 的问题
    dotnet调用外部dll中,参数数据类型的问题
    前两天用VC6做的修改远程桌面端口的命令行小程序,源码。
  • 原文地址:https://www.cnblogs.com/bravolove/p/6148259.html
Copyright © 2011-2022 走看看