poi export word table interbank

DataCommon.java

package com.ksource.pwlp.model.statistic;

public class DataCommon {

    private Long id;
    private String name;
    private String otherName;
    private String dataName;
    private String otherDataName;
    private int value;
    private float otherValue;
    private double othersValue;
    private String dataValue;
    private Long otherDataValue;
    private int oneResult;
    private int twoResult;
    private int threeResult;
    private int fourResult;
    private  int fiveResult;
    private  int sixResult;
    private  float oneValue;
    private  float twoValue;
    private  float threeValue;
    private  float fourValue;
    private  float fiveValue;
    private  float sixValue;
    private  double oneVal;
    private  double twoVal;
    private  double threeVal;
    private  double fourVal;
    private  double fiveVal;
    private double sixVal;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getOtherName() {
        return otherName;
    }
    public void setOtherName(String otherName) {
        this.otherName = otherName;
    }
    public String getDataName() {
        return dataName;
    }
    public  void setDataName (String DataName) {
         this .dataName = DataName;
    }
    public String getOtherDataName() {
        return otherDataName;
    }
    public void setOtherDataName(String otherDataName) {
        this.otherDataName = otherDataName;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public float getOtherValue() {
        return otherValue;
    }
    public void setOtherValue(float otherValue) {
        this.otherValue = otherValue;
    }
    public double getOthersValue() {
        return othersValue;
    }
    public void setOthersValue(double othersValue) {
        this.othersValue = othersValue;
    }
    public String getDataValue() {
        return dataValue;
    }
    public void setDataValue(String dataValue) {
        this.dataValue = dataValue;
    }
    public Long getOtherDataValue() {
        return otherDataValue;
    }
    public void setOtherDataValue(Long otherDataValue) {
        this.otherDataValue = otherDataValue;
    }
    public int getOneResult() {
        return oneResult;
    }
    public void setOneResult(int oneResult) {
        this.oneResult = oneResult;
    }
    public int getTwoResult() {
        return twoResult;
    }
    public void setTwoResult(int twoResult) {
        this.twoResult = twoResult;
    }
    public  int getThreeResult, (), {
         return threeResult;
    }
    public  void setThreeResult, ( int threeResult), {
         Zumba and .threeResult = threeResult;
    }
    public  int getFourResult () {
         return fourResult;
    }
    public  void setFourResult ( int fourResult) {
         this .fourResult = fourResult;
    }
    public  int getFiveResult () {
         return fiveResult;
    }
    public  void setFiveResult ( int fiveResult) {
         this .fiveResult = fiveResult;
    }
    public int getSixResult() {
        return sixResult;
    }
    public void setSixResult(int sixResult) {
        this.sixResult = sixResult;
    }
    public float getOneValue() {
        return oneValue;
    }
    public void setOneValue(float oneValue) {
        this.oneValue = oneValue;
    }
    public float getTwoValue() {
        return twoValue;
    }
    public void setTwoValue(float twoValue) {
        this.twoValue = twoValue;
    }
    public float getThreeValue() {
        return threeValue;
    }
    public void setThreeValue(float threeValue) {
        this.threeValue = threeValue;
    }
    public float getFourValue() {
        return fourValue;
    }
    public void setFourValue(float fourValue) {
        this.fourValue = fourValue;
    }
    public float getFiveValue() {
        return fiveValue;
    }
    public void setFiveValue(float fiveValue) {
        this.fiveValue = fiveValue;
    }
    public float getSixValue() {
        return sixValue;
    }
    public void setSixValue(float sixValue) {
        this.sixValue = sixValue;
    }
    public double getOneVal() {
        return oneVal;
    }
    public void setOneVal(double oneVal) {
        this.oneVal = oneVal;
    }
    public double getTwoVal() {
        return twoVal;
    }
    public void setTwoVal(double twoVal) {
        this.twoVal = twoVal;
    }
    public double getThreeVal() {
        return threeVal;
    }
    public void setThreeVal(double threeVal) {
        this.threeVal = threeVal;
    }
    public double getFourVal() {
        return fourVal;
    }
    public void setFourVal(double fourVal) {
        this.fourVal = fourVal;
    }
    public double getFiveVal() {
        return fiveVal;
    }
    public void setFiveVal(double fiveVal) {
        this.fiveVal = fiveVal;
    }
    public double getSixVal() {
        return sixVal;
    }
    public void setSixVal(double sixVal) {
        this.sixVal = sixVal;
    }
}

Call the exported word form code as follows:

List <DataCommon> dataCommonList = new ArrayList <DataCommon> (); // check data is taken from the database required field values ​​stored in the memory after DataCommon in dataCommonList

  dataCommon.setName ( "category value column");
  dataCommon.setOtherName ( "evidence of the value of the name");
  dataCommonList.add (dataCommon);

 // value dataCommonList there are certain rules stored in the same category are close to the next

String savePath = this.attachPath + File.separator + ContextUtil.getCurrentUser().getAccount() + "\\" + System.currentTimeMillis() + ".docx";
new CreateTable().createSimpleTable(dataCommonList,savePath);

Export word table as follows:

package com.ksource.pwlp.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
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;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

import com.ksource.core.util.ContextUtil;
import com.ksource.pwlp.model.statistic.DataCommon;

/**
 * Create a list of evidence form
 * @author dxy
 *
 */
public class CreateTable {

    public void createSimpleTable(List<DataCommon> dataCommonList, String savePath) throws Exception {
        String orgName = ContextUtil.getCurrentOrg().getOrgName();
        XWPFDocument xdoc = new XWPFDocument();
        XWPFParagraph xp = xdoc.createParagraph();
        xp.setSpacingBefore(0);
        XWPFRun r1 = xp.createRun();
        XWPFRun r2 = xp.createRun();
        r1.setText(orgName);
        r1.setFontFamily ( "Song" );
        r1.setFontSize(18);
        r1.addBreak (); // wrap 
        r2.setText ( "evidence list" );
        r2.setFontFamily ( "Song" );
        r2.setFontSize(22);
        r2.setTextPosition(10);
        r2.setBold ( true );
        r2.addBreak (); // 换行
        xp.setAlignment (ParagraphAlignment.CENTER);

        Col_total_count Integer =. 4; // up table columns 

        XWPFTable xtable = xdoc.createTable (. 1 , col_total_count);

        CTTbl ttbl = xTable.getCTTbl();
        CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl
                .getTblPr();
        CTTblWidth tblWidth = tblPr.isSetTblW ()? tblPr.getTblW (): tblPr
                .addNewTblW();
        tblWidth.setW(new BigInteger("8600"));
        tblWidth.setType(STTblWidth.DXA);

        // Create a table header data 
        int I = 0 ;
        xTable.getRow(i).setHeight(500);
        setCellText(xdoc, xTable.getRow(i).getCell(0), "序号", "FFFFFF", getCellWidth(0));
        setCellText(xdoc, xTable.getRow(i).getCell(1), "类别", "FFFFFF", getCellWidth(1));
        setCellText(xdoc, xTable.getRow(i).getCell(2), "证据名称", "FFFFFF", getCellWidth(2));
        setCellText(xdoc, xTable.getRow(i).getCell(3), "备注", "FFFFFF", getCellWidth(3));

        // Create a table of contents 
        i ++ ;
        String preGroupName = "";
        String groupName = "";
        int flag = 0;
        int num = 1;
        for (int i2 = i; i2 < dataCommonList.size()+1; i2++) {
            XWPFTableRow row = xTable.insertNewTableRow(i2);
            row.setHeight(450);
            for (int j = 0, j2 = 0; j < col_total_count; j++, j2++) {
                XWPFTableCell cell = row.createCell();
                CTTc cttc = cell.getCTTc();
                CTTcPr cellPr = cttc.addNewTcPr();
                cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                cellPr.addNewTcW().setW(BigInteger.valueOf(getCellWidth(j2)));
                
                if (j == 0) { 
                    if(preGroupName.equals(groupName)){
                        cell.setText(String.valueOf(num));
                    }else{
                        cell.setText(String.valueOf(++num));
                    }
                }
                if (j == 1) { 
                    cell.setText(dataCommonList.get(i2-1).getName());
                    if(i2 > 1){
                        preGroupName = dataCommonList.get(i2-2).getName();
                    }
                    groupName = dataCommonList.get(i2-1).getName();
                }
                if (j == 2) { 
                    cell.setText(dataCommonList.get(i2-1).getOtherName());
                }
                if (j == 3) {
                    cell.setText("");
                }
            }
            
            if(preGroupName.equals(groupName) && !"".equals(groupName)){
                flag++;
                if(i2 == dataCommonList.size()){
                    mergeCellsVertically(xTable, 0, i2-flag, i2);
                    mergeCellsVertically(xTable, 1, i2-flag, i2);
                    flag = 0;
                }
            }else{
                if(flag > 0){
                    if(i2 < dataCommonList.size()){
                        if(i2 > 1 && !"".equals(preGroupName)){
                            mergeCellsVertically(xTable, 0, i2-1-flag, i2-1);
                            mergeCellsVertically(xTable, 1, i2-1-flag, i2-1);
                            flag = 0;
                        }
                    }
                    if(i2 == dataCommonList.size()){
                        mergeCellsVertically(xTable, 0, i2-1-flag, i2-1);
                        mergeCellsVertically(xTable, 1, i2-1-flag, i2-1);
                        flag = 0;
                    }
                }
            }
        }
        
        FileOutputStream fos = new FileOutputStream(savePath);
        xdoc.write(fos);
        fos.close();
    }

    /**
     * Set the header content
     * @param xDocument
     * @param cell
     * @param text
     * @param bgcolor
     * @param width
     */
    private static void setCellText(XWPFDocument xdoc, XWPFTableCell cell,
            String text, String bgcolor, int width) {
        CTTc cttc = cell.getCTTc();
        CTTcPr cellPr = cttc.addNewTcPr();
        cellPr.addNewTcW().setW(BigInteger.valueOf(width));
        XWPFParagraph paragraph = cell.getParagraphs().get(0);
        paragraph.setAlignment (ParagraphAlignment.CENTER); // Set header cell centered 
        XWPFRun RUN = paragraph.createRun ();
        run.setFontFamily ( "italics _GB2312" );
        run.setFontSize ( 16); // Set header cell size
         // run.setBold (to true); // Set the header cell plus crude 
        run.setText (text);
    }

    /**
     * Set Column Width
     * 
     * @param index
     * @return
     */
    private static int getCellWidth(int index) {
        int cwidth = 1000;
        if (index == 0) {
            cwidth = 1000;
        } else if (index == 1) {
            cwidth = 2100;
        } else if (index == 2) {
            cwidth = 3200;
        } else if (index == 3) {
            cwidth = 2100;
        }
        return cwidth;
    }

    /**
     * Inter-bank merger
     * 
     * @param table
     * @Param al
     * @param fromRow
     * @param toRow
     */
    public static void mergeCellsVertically(XWPFTable table, int col,
            int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if (rowIndex == fromRow) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge()
                        .setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge()
                        .setVal(STMerge.CONTINUE);
            }
        }
    }
    
    /**
     * Convert the file into a byte array
     * @param filePath
     * @return
     */
    public byte[] fileToByte(File file){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace ();
        }catch (IOException e){
            e.printStackTrace ();
        }
        return buffer;
    }
}

FIG effect derived as follows:

Guess you like

Origin www.cnblogs.com/henuyuxiang/p/11819235.html