1. 程式人生 > >POI 匯出 Word 表格

POI 匯出 Word 表格

package com.yhksxt.util;


import java.io.IOException;
import java.math.BigInteger;

import javax.servlet.http.HttpServletResponse;

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.CTJc;
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.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;


public class ExprotSTUtil {





public  void exportSTToWord(String titleName,String[][] list ,HttpServletResponse response) throws IOException{
//建立document物件
XWPFDocument document = new XWPFDocument();

//新增標題
XWPFParagraph titleParagraph = document.createParagraph();
//設定段落居中  
       titleParagraph.setAlignment(ParagraphAlignment.CENTER); 
       XWPFRun titleParagraphRun = titleParagraph.createRun();  
       titleParagraphRun.setText(titleName);  
       titleParagraphRun.setColor("000000");  
       titleParagraphRun.setFontSize(20);  
//建立表格
          int row = list.length +2;
          int column = list[0].length +3;
          XWPFTable table = document.createTable(row,column);
          setTableWidth(table, "10000"); 
       
          //處理表達合併和資料填充
          
          //合併列
          mergeCellsVertically(table, 0, 0, 1);
          mergeCellsVertically(table, 2, 0, 1);
          mergeCellsVertically(table, column-1, 0, 1);
          
          //合併行
          for(int i=0;i<row-2;i++){
          mergeCellsHorizontal(table, i, 0, 1);
              mergeCellsHorizontal(table, i, 2, 4);
          }
          
          mergeCellsHorizontal(table, 0, 5, column-2);
          mergeCellsHorizontal(table, row-2, 0, 4);
          mergeCellsHorizontal(table, row-1, 0, 4);
          mergeCellsHorizontal(table, row-1, 5, column-1);
          
          //填充資料
          XWPFTableRow rowIndex = table.getRow(0);
          XWPFTableCell cell = rowIndex.getCell(0);
          cell.setText(list[0][0]);
          XWPFTableCell cell1 = rowIndex.getCell(2);
        cell1.setText(list[0][1]);
        XWPFTableCell cell2 = rowIndex.getCell(5);
        cell2.setText("試題型別及題量");
        XWPFTableCell cell3 = rowIndex.getCell(column-1);
        cell3.setText(list[0][list[0].length-1]);
         
        XWPFTableRow rowIndex1 = table.getRow(1);
        for(int j=5,i=2;j<column-1;j++,i++){
        XWPFTableCell cell4 = rowIndex1.getCell(j);
        cell4.setText(list[0][i]);
        }
         
        for(int i = 2,ii=1;i<row-1;i++,ii++){
        XWPFTableRow rowIndex2 = table.getRow(i);
        for(int j = 0,jj=0;j<column-1 ;j++){
        XWPFTableCell cell4 =null;
        if(j==0){
        cell4 =rowIndex2.getCell(j);
        cell4.setText(list[ii][jj]);
        jj++;
        }if(j==2){
        cell4 =rowIndex2.getCell(j);
        cell4.setText(list[ii][jj]);
        jj++;
        }else if(j>=5){
        cell4 =rowIndex2.getCell(j);
        cell4.setText(list[ii][jj]);
          jj++;
        }
        }
        }
         
        XWPFTableRow rowIndex3 = table.getRow(row-1);
        XWPFTableCell cell5 =rowIndex3.getCell(0);
        cell5.setText("其他需要說明的問題");
              
          document.write(response.getOutputStream());
}

 
/***
*  跨行合併  
* @param table
* @param col  合併列
* @param fromRow 起始行
* @param toRow   終止行
*/
private 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);  
           }  
       }  
   }

/***
* 跨列合併 
* @param table
* @param row 所合併的行
* @param fromCell  起始列
* @param toCell   終止列
*/
private  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {  
       for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {  
           XWPFTableCell cell = table.getRow(row).getCell(cellIndex);  
           if ( cellIndex == fromCell ) {  
               // The first merged cell is set with RESTART merge value  
               cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);  
           } else {  
               // Cells which join (merge) the first one, are set with CONTINUE  
               cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);  
           }  
       }  
   }  
   

/***
* 匯出word 設定行寬
* @param table
* @param width
*/
private  void setTableWidth(XWPFTable table,String width){  
        CTTbl ttbl = table.getCTTbl();  
        CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
        CTJc cTJc=tblPr.addNewJc();  
        cTJc.setVal(STJc.Enum.forString("center"));  
        tblWidth.setW(new BigInteger(width));  
        tblWidth.setType(STTblWidth.DXA);  
    }  
}