日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lowagie.text 表格带复杂表头 导出word

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lowagie.text 表格带复杂表头 导出word 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

lowagie.text 導出word表格帶復雜表頭的內容

使用lowagie.text 導出word表格帶多表頭的時候怎么處理呢?

????????其實蠻簡單的,知道rowspancolspan是什么意思就好操作了。 同理設置表格標題的時候也是一樣。

引入itext

<groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.0.8</version> </dependency>

代碼:

import com.lowagie.text.*; import com.lowagie.text.Font; import com.lowagie.text.rtf.RtfWriter2; import java.awt.*; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List;public class ItextTableMulHeader {/*** colspan 合并列*? rowspan 合并行*/public static void main(String[] args) throws Exception {OutputStream out = new FileOutputStream("d://exportFile//table" + System.currentTimeMillis() + ".doc");Document document = new Document(PageSize.A4);RtfWriter2.getInstance(document, out);document.open();addTable(document);document.close();System.out.println("ok");}private static void addTable(Document document) throws DocumentException {Color lightGray = new Color(232, 232, 232);com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(null, 12, com.lowagie.text.Font.BOLD,Color.black);com.lowagie.text.Font titleChinese = new com.lowagie.text.Font(null, 17, com.lowagie.text.Font.BOLD,Color.black);String[] headFirst = {"1"};String[] headSec = {"11","22"};String[] headThird = {"111","222","333","444"};String[] headFour = {"1111","2222","3333","4444","5555","6666","7777","8888"};int maxSize = headFour.length;Table table = new Table(maxSize);int widths = 100 / maxSize;int widths1[] = setWordWith(maxSize, widths);// 設置每列寬度比例table.setWidths(widths1);table.setWidth(100);// 占頁面寬度比例table.setAlignment(Element.ALIGN_CENTER);//居中table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中table.setAutoFillEmptyCells(true);//自動填滿table.setBorderWidth(1);//邊框寬度table.setPadding(8);// 設置表格表題Paragraph p = new Paragraph("多表頭表格", titleChinese);p.setSpacingAfter(8);p.setSpacingBefore(8);Cell cell = new Cell(p);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setBorder(0);cell.setColspan(maxSize); // 合并列,單獨成一行table.addCell(cell);// 設置表頭setTableHeader(table,maxSize/headFirst.length, headFirst, fontChinese, lightGray);setTableHeader(table,maxSize/headSec.length, headSec, fontChinese, lightGray);setTableHeader(table,maxSize/headThird.length, headThird, fontChinese, lightGray);setTableHeader(table,maxSize/headFour.length, headFour, fontChinese, lightGray);List<List<Integer>> content = getRandom(5, maxSize);for (List<Integer> row : content) {for (Integer column : row) {cell = new Cell(new Paragraph(column.toString(), fontChinese));cell.setHorizontalAlignment(Element.ALIGN_LEFT);table.addCell(cell);}}document.add(table);}private static void setTableHeader(Table table, int colspan, String[] headAttr, Font fontChinese,? Color lightGray) throws BadElementException {Cell cell;for (String column : headAttr) {cell = new Cell(new Paragraph(column, fontChinese));cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setBackgroundColor(lightGray);cell.setColspan(colspan);table.addCell(cell);}}private static List<List<Integer>> getRandom(int rowNum, int columnNum){List<List<Integer>>? allList = new ArrayList<>(rowNum);List<Integer> innerList;int num;for (int i = 0; i < rowNum; i++){innerList = new ArrayList<>(columnNum);for (int j = 0; j < columnNum; j++){num = (int) (Math.random() * 9999);innerList.add(num);}allList.add(innerList);}return allList;}private static int[] setWordWith(int size, int with) {int[] intWidth = new int[size];for (int i = 0; i < size; i++) {intWidth[i] = with;}return intWidth;} }

表格標題,設置的時候,邊框設置為0,單獨占一行,如果不占一行會發生什么呢?

表頭一行一行根據實際的進行設置。

??

結果:

?文件夾要先建好,然后打開對應的文件查看

??

總結:

??使用lowagie.text導出word表格帶復雜表頭的時候,主要是要了解rowspan和colpan,即合并行和合并列,再根據實際的合并進行。不清楚的時候,網上也找不到例子的時候,可以模擬例子各種屬性多試試看看什么含義。

總結

以上是生活随笔為你收集整理的lowagie.text 表格带复杂表头 导出word的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。