Excel 在日常工作中經(jīng)常被用來存儲用例信息,是一種非常便捷的數(shù)據(jù)存儲工具有著眾多的優(yōu)點,我們就不一一介紹了。
今天來講講 Java 操作 Excel,總所周知 Java 是世界上最好的語言(不容反駁),操作一個 Excel 肯定是不在話下,咱們熟知的 POI,Apache 大佬出品的一款非常強大的 office 軟件操作包。雖然 POI 強大,但是代碼相對比較繁瑣,在當(dāng)前 python 引領(lǐng)的大潮下,簡化代碼勢在必行。
那么如何簡化代碼呢?其實這些事情早就已經(jīng)有人幫我們想好和做好了,比如阿里巴巴的 easyexcel,和我們今天的主角 esaypoi 都是非常好的解決方案。那為什么選擇 easypoi 而不是阿里的 easyexcel 呢,當(dāng)然是 easypoi 的讀寫導(dǎo)入和導(dǎo)出更加簡單。接下來大家就跟隨著我一起慢慢揭開 easypoi 的神秘面紗。
easypoi 功能如同名字 easy,主打的功能就是容易,讓一個沒見接觸過 poi 的人員
就可以方便的寫出 Excel 導(dǎo)出,Excel 模板導(dǎo)出,Excel 導(dǎo)入,Word 模板導(dǎo)出,通過簡單的注解和模板。
官網(wǎng):https://opensource.afterturn.cn/doc/easypoi.html
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
完成最簡單的導(dǎo)入,只需要兩步:一使用注解配置實體類,二調(diào)用工具類。
實體類(省略 get/set 方法):
public class API implements Serializable {
@Excel(name="接口名稱")
private String name;
@Excel(name="接口編號")
private String id;
@Excel(name="接口提交方式")
private String type;
@Excel(name="接口地址")
private String url;
@Excel(name="參數(shù)類型")
private String contentType;
}
@Excel(name="接口名稱")這個注解是啥意思呢?name 屬性表示 Excel 表頭。如圖:
當(dāng)我們導(dǎo)入 Excel 時,就會按照@Excel 注解的映射關(guān)系封裝 API 實體類。
工具類:
FileInputStream fis=new FileInputStream(EXCEL_PATH);
//導(dǎo)入?yún)?shù)設(shè)置類
ImportParams params=new ImportParams();
List<API> importExcel=ExcelImportUtil.importExcel(fis, API.class, params);
總共三行代碼,第一行加載 Excel 文件,第二行設(shè)置導(dǎo)入?yún)?shù),第三行根據(jù)導(dǎo)入?yún)?shù)返回對應(yīng)結(jié)果并封裝成 List 集合。這三個代碼中主要講解第二行和第三行,第二行是導(dǎo)入?yún)?shù)設(shè)置,它能給我們提供什么設(shè)置呢?
參考下表:
屬性 | 類型 | 默認(rèn)值 | 功能 |
titleRows | int | 0 | 表格標(biāo)題行數(shù),默認(rèn) 0 |
headRows | int | 1 | 表頭行數(shù),默認(rèn) 1 |
startRows | int | 0 | 字段真正值和列標(biāo)題之間的距離 默認(rèn) 0 |
keyIndex | int | 0 | 主鍵設(shè)置,如何這個 cell 沒有值,就跳過 或者認(rèn)為這個是 list 的下面的值這一列必須有值,不然認(rèn)為這列為無效數(shù)據(jù) |
startSheetIndex | int | 0 | 開始讀取的 sheet 位置,默認(rèn)為 0 |
sheetNum | int | 1 | 上傳表格需要讀取的 sheet 數(shù)量,默認(rèn)為 1 |
needSave | boolean | false | 是否需要保存上傳的 Excel |
needVerfiy | boolean | false | 是否需要校驗上傳的 Excel |
saveUrl | String | "upload/excelUpload" | 保存上傳的 Excel 目錄,默認(rèn)是 如 TestEntity 這個類保存路徑就是 upload/excelUpload/Test/yyyyMMddHHmss****** 保存名稱上傳時間*五位隨機數(shù) |
verifyHanlder | IExcelVerifyHandler | null | 校驗處理接口,自定義校驗 |
lastOfInvalidRow | int | 0 | 最后的無效行數(shù),不讀的行數(shù) |
readRows | int | 0 | 手動控制讀取的行數(shù) |
importFields | String[] | null | 導(dǎo)入時校驗數(shù)據(jù)模板,是不是正確的 Excel |
keyMark | String | ":" | Key-Value 讀取標(biāo)記,以這個為 Key,后面一個 Cell 為 Value,多個改為 ArrayList |
readSingleCell | boolean | false | 按照 Key-Value 規(guī)則讀取全局掃描 Excel,但是跳過 List 讀取范圍提升性能 僅僅支持 titleRows + headRows + startRows 以及 lastOfInvalidRow |
dataHanlder | IExcelDataHandler | null | 數(shù)據(jù)處理接口,以此為主,replace,format 都在這后面 |
對照完這張表之后,你會發(fā)現(xiàn)即使我們不對 ImportParams 做任何設(shè)置,也會有對應(yīng)的默認(rèn)值。那么第二句代碼就能翻譯成:讀取第一個 Sheet 且只讀取第一個,表頭是 Sheet 的第一行且只有一行。最終我們就能得到第一個 Sheet 中每一行數(shù)據(jù),并且每一行被封裝成了 API 對象也就是一個 List。有了這個集合之后我們需要導(dǎo)入的數(shù)據(jù)就能任由我們?nèi)绾翁幚砹耍遣皇呛芎唵巍?/p>
List<API> list=new ArrayList<API>();
ExportParams exportParams=new ExportParams();
Workbook workbook=ExcelExportUtil.exportExcel(exportParams, API.class, list);
workbook.write(new FileOutputStream(EXCEL_PATH));
導(dǎo)出也只有四句代碼。第一句是需要導(dǎo)出的數(shù)據(jù)集合,第二句導(dǎo)出參數(shù),第三句獲取導(dǎo)出 workbook 對象,第四句通過輸出流導(dǎo)出數(shù)據(jù)到 Excel 中。其中第二句也是有很多設(shè)置的,我們就用默認(rèn)設(shè)置也能是導(dǎo)出的。第三句也要用到 API 實體類中的注解映射關(guān)系。
通過 esaypoi 我們能夠使用最少的代碼完成基本的導(dǎo)入和導(dǎo)出,基本上能夠應(yīng)對實際工作中 80% 的需求了,如果需要對 Excel 修改的話,目前來說市面上的工具包都做的不太簡單,所以還是需要通過編寫原生 poi 代碼完成,如果你需要修改 Excel 的代碼可以留言哦~
朋友想知道excel 轉(zhuǎn)html實現(xiàn),安排!于是我在原文的基礎(chǔ)的基礎(chǔ)上新增加了excel轉(zhuǎn)html的實現(xiàn)方式,可是在提交修改時被告知"修改篇幅過大 請減少修改篇幅",不讓提交,無奈只能重新再發(fā)一篇文章了
修改篇幅過大 請減少修改篇幅
實現(xiàn)文檔在線預(yù)覽的方式除了上篇文章《文檔在線預(yù)覽(一)通過將txt、word、pdf轉(zhuǎn)成圖片實現(xiàn)在線預(yù)覽功能》說的將文檔轉(zhuǎn)成圖片的實現(xiàn)方式外,還有轉(zhuǎn)成pdf,前端通過pdf.js、pdfobject.js等插件來實現(xiàn)在線預(yù)覽,以及本文將要說到的將文檔轉(zhuǎn)成html的方式來實現(xiàn)在線預(yù)覽。代碼基于 aspose-words(用于word轉(zhuǎn)html),pdfbox(用于pdf轉(zhuǎn)html),所以事先需要在項目里下面兩個依賴:
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version></dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
public static String wordToHtmlStr(String wordPath) {
try {
Document doc=new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔
String htmlStr=doc.toString();
return htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
驗證結(jié)果:
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document=PDDocument.load(new File(pdfPath));
Writer writer=new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
驗證結(jié)果:
public static String excelToHtmlStr(String excelPath) throws Exception {
FileInputStream fileInputStream=new FileInputStream(excelPath);
Workbook workbook=new XSSFWorkbook(fileInputStream);
DataFormatter dataFormatter=new DataFormatter();
FormulaEvaluator formulaEvaluator=workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet=workbook.getSheetAt(0);
StringBuilder htmlStringBuilder=new StringBuilder();
htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");
htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");
htmlStringBuilder.append("</head><body><table>");
for (Row row : sheet) {
htmlStringBuilder.append("<tr>");
for (Cell cell : row) {
CellType cellType=cell.getCellType();
if (cellType==CellType.FORMULA) {
formulaEvaluator.evaluateFormulaCell(cell);
cellType=cell.getCachedFormulaResultType();
}
String cellValue=dataFormatter.formatCellValue(cell, formulaEvaluator);
htmlStringBuilder.append("<td>").append(cellValue).append("</td>");
}
htmlStringBuilder.append("</tr>");
}
htmlStringBuilder.append("</table></body></html>");
return htmlStringBuilder.toString();
}
返回的html字符串:
<html><head><title>Excel to HTML using Java and POI library</title><style>table, th, td { border: 1px solid black; }</style></head><body><table><tr><td>序號</td><td>姓名</td><td>性別</td><td>聯(lián)系方式</td><td>地址</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區(qū)xx路xx弄xx號</td></tr></table></body></html>
有時我們是需要的不僅僅返回html字符串,而是需要生成一個html文件這時應(yīng)該怎么做呢?一個改動量小的做法就是使用org.apache.commons.io包下的FileUtils工具類寫入目標(biāo)地址:
首先需要引入pom:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
相關(guān)代碼:
String htmlStr=FileConvertUtil.pdfToHtmlStr("D:\\書籍\\電子書\\小說\\歷史小說\\最后的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,還可以對上面的代碼進行一些調(diào)整,已實現(xiàn)生成html文件,代碼調(diào)整如下:
public static void wordToHtml(String wordPath, String htmlPath) {
try {
File sourceFile=new File(wordPath);
String path=htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
File file=new File(path); // 新建一個空白pdf文檔
FileOutputStream os=new FileOutputStream(file);
Document doc=new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔
HtmlSaveOptions options=new HtmlSaveOptions();
options.setExportImagesAsBase64(true);
options.setExportRelativeFontSize(true);
doc.save(os, options);
} catch (Exception e) {
e.printStackTrace();
}
}
word原文件效果:
word文件轉(zhuǎn)換成html效果:
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
File file=new File(pdfPath);
String path=htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
PDDocument document=PDDocument.load(new File(pdfPath));
Writer writer=new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
圖片版PDF文件驗證結(jié)果:
文字版PDF原文件效果:
文字版PDF文件驗證結(jié)果:
public static void excelToHtml(String excelPath, String htmlPath) throws Exception {
String path=FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");
try(FileOutputStream fileOutputStream=new FileOutputStream(path)){
String htmlStr=excelToHtmlStr(excelPath);
byte[] bytes=htmlStr.getBytes();
fileOutputStream.write(bytes);
}
}
excel原文件效果:
excel文件轉(zhuǎn)換成html文件驗證效果:
從上述的效果展示我們可以發(fā)現(xiàn)其實轉(zhuǎn)成html效果不是太理想,很多細節(jié)樣式?jīng)]有還原,這其實是因為這類轉(zhuǎn)換往往都是追求目標(biāo)是通過使用文檔中的語義信息并忽略其他細節(jié)來生成簡單干凈的 HTML,所以在轉(zhuǎn)換過程中復(fù)雜樣式被忽略,比如居中、首行縮進、字體,文本大小,顏色。舉個例子在轉(zhuǎn)換是 會將應(yīng)用標(biāo)題 1 樣式的任何段落轉(zhuǎn)換為 h1 元素,而不是嘗試完全復(fù)制標(biāo)題的樣式。所以轉(zhuǎn)成html的顯示效果往往和原文檔不太一樣。這意味著對于較復(fù)雜的文檔而言,這種轉(zhuǎn)換不太可能是完美的。但如果都是只使用簡單樣式文檔或者對文檔樣式不太關(guān)心的這種方式也不妨一試。
Java編程生成word文檔這種操作一般是常規(guī)操作比較常見,主要采用Apache的POI Word 這個庫操作的比較多,還有的用Spire.Doc,但是這個庫有些稍微難點的功能要收費,看了下費用還不低,周末朋友問起是否有用java操作word加上下標(biāo)的經(jīng)驗,我是沒有的,不過剛好借機研究下。 本文主要聊下POI,畢竟是免費的,更值得研究。
word 格式說明和POI支持情況: HWPF: MS-Word 97-2003(.doc),基于BIFF8格式的JAVA接口。只支持.doc文件簡單的操作,讀寫能力有限。本API為POI項目早期開發(fā),很不幸的 是主要負(fù)責(zé)HWPF模塊開發(fā)的工程師-"Ryan Ackley"已經(jīng)離開Apache組織,現(xiàn)在該模塊沒有人維護、更新、完善。 XWPF:MS-Word 2007+(.docx),基于OOXML格式的JAVA接口。較HWPF功能完善。
都是利用的jar,不存在什么安裝,直接配置pom文件即可。 添加POI的word的jar:
<!-- 操作excel的庫 注意版本保持一致 poi poi-ooxml poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
<scope>compile</scope>
</dependency>
<!--poi-ooxml和*poi-ooxml-schemas*是poi對2007及以上版本的擴充。-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
<scope>compile</scope>
</dependency>
<!--poi-ooxml和*poi-ooxml-schemas*是poi對2007及以上版本的擴充。-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
<scope>compile</scope>
</dependency>
<!-- WordToHtml .doc .odcx poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
最核心的三個類: XWPFDocument : 文檔本身的抽象、代表整個文檔; XWPFParagraph: 段落、整個文檔可以看做一個個段落組成、簡單對應(yīng)我們文章的段落; XWPFRun: 可以看做一個片段,整個段落由多個Run組成、每個Run可以有自己的文字內(nèi)容、樣式等。
XWPFTable: 表格,和段落平級;
創(chuàng)建文檔
XWPFDocument doc=new XWPFDocument();
或
XWPFDocument doc=new XWPFDocument(new FileInputStream("./deepoove.docx"));
獲取文檔上的元素、比如段落、表格、圖片等。
// 段落
List<XWPFParagraph> paragraphs=doc.getParagraphs();
// 表格
List<XWPFTable> tables=doc.getTables();
// 圖片
List<XWPFPictureData> allPictures=doc.getAllPictures();
// 頁眉
List<XWPFHeader> headerList=doc.getHeaderList();
// 頁腳
List<XWPFFooter> footerList=doc.getFooterList();
生成文檔
try (FileOutputStream out=new FileOutputStream("simple.docx")) {
doc.write(out);
}
創(chuàng)建段落
XWPFParagraph p1=doc.createParagraph();
段落樣式設(shè)置:
// 對齊方式
p1.setAlignment(ParagraphAlignment.CENTER);
// 邊框
p1.setBorderBottom(Borders.DOUBLE);
p1.setBorderTop(Borders.DOUBLE);
p1.setBorderRight(Borders.DOUBLE);
p1.setBorderLeft(Borders.DOUBLE);
p1.setBorderBetween(Borders.SINGLE);
Run是段落的基本組成單元,可以是一段文字、也可以是一張圖片、可以設(shè)置自己的不同樣式風(fēng)格。
獲取段落內(nèi)容
// 獲取文字
String text=paragraph.getText();
// 獲取段落內(nèi)所有XWPFRun
List<XWPFRun> runs=paragraph.getRuns();
創(chuàng)建run
// 段落末尾創(chuàng)建XWPFRun
XWPFRun run=paragraph.createRun();
run.setText("為這個段落追加文本");
// 顏色
run.setColor("00ff00");
// 斜體
run.setItalic(true);
// 粗體
run.setBold(true);
// 字體
run.setFontFamily("Courier");
// 下劃線
run.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
獲取段落中run和修改run
// 獲取文字
String text=paragraph.getText();
// 獲取段落內(nèi)所有XWPFRun
List<XWPFRun> runs=paragraph.getRuns();
// 段落起始插入XWPFRun
XWPFRun insertNewRun=paragraph.insertNewRun(0);
insertNewRun.setText("在段落起始位置插入這段文本");
修改run
List<XWPFRun> runs=paragraph.getRuns();
// setText默認(rèn)為追加文本,參數(shù)0表示設(shè)置第0個位置的文本,覆蓋上一次設(shè)置
runs.get(0).setText("追加文本", 0);
runs.get(0).setText("修改文本", 0);
文本換行
run.addCarriageReturn();
提取文檔圖片
List<XWPFPictureData> allPictures=doc.getAllPictures();
XWPFPicture pciture=allPictures.get(0);
byte[] data=pciture.getPictureData().getData();
// 接下來就可以將圖片字節(jié)數(shù)組寫入輸出流
利用XWPFRun創(chuàng)建圖片
List<XWPFPictureData> allPictures=doc.getAllPictures();
XWPFPicture pciture=allPictures.get(0);
byte[] data=pciture.getPictureData().getData();
// 接下來就可以將圖片字節(jié)數(shù)組寫入輸出流
創(chuàng)建三行三列的表格
XWPFTable table=doc.createTable(3, 3);
設(shè)置單元格的文本 表格是由表格行XWPFRow構(gòu)成,每行是由單元格XWPFCell構(gòu)成,每個單元格內(nèi)部又是由許多XWPFParagraph段落構(gòu)成。
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
上面代碼和下面代碼等價:
XWPFParagraph p1=table.getRow(0).getCell(0).addParagraph();
XWPFRun r1=p1.createRun();
r1.setText("EXAMPLE OF TABLE")
設(shè)置單元格里面的圖片,可以按照上述辦法獲取到單元格的XWPFRun然后按照run添加圖片的辦法進行。
設(shè)置單元格的樣式
XWPFParagraph p1=table.getRow(0).getCell(0).addParagraph();
XWPFRun r1=p1.createRun();
r1.setText("EXAMPLE OF TABLE")
public class TestFirstDoc {
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument document=new XWPFDocument();
FileOutputStream out=new FileOutputStream(new File("測試文檔.docx"));
XWPFParagraph paragraph=document.createParagraph();
// 基本測試
XWPFRun paragraphOneRunOne=paragraph.createRun();
paragraphOneRunOne.setText("測試第一個Run");
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.addBreak();
// 樣式測試
XWPFRun paragraphRun2=paragraph.createRun();
paragraphRun2.setText("為這個段落追加文本");
paragraphRun2.setFontSize(20);
paragraphRun2.setColor("00ff00");
paragraphRun2.setFontFamily("Courier");
paragraphRun2.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
// 上下標(biāo)測試
XWPFRun paragraphRun3=paragraph.createRun();
paragraphRun3.setText("設(shè)置正常文字");
XWPFRun paragraphRun4=paragraph.createRun();
paragraphRun4.setText("上標(biāo)");
paragraphRun4.setItalic(true);
paragraphRun4.setSubscript(VerticalAlign.SUPERSCRIPT);
XWPFRun paragraphRun5=paragraph.createRun();
paragraphRun5.setText("下標(biāo)");
paragraphRun5.setItalic(true);
paragraphRun5.setSubscript(VerticalAlign.SUBSCRIPT);
// 設(shè)置換頁,表格和上面內(nèi)容分開來
XWPFParagraph paragraph2=document.createParagraph();
paragraph2.setPageBreak(true);
XWPFTable table=document.createTable(3, 3);
table.getRow(0).getCell(0).setText("Head1");
table.getRow(0).getCell(1).setText("Head2");
table.getRow(0).getCell(2).setText("Head3");
table.getRow(1).getCell(0).setText("col1");
table.getRow(1).getCell(1).setText("col2");
table.getRow(1).getCell(2).setText("col3");
XWPFParagraph p1=table.getRow(2).getCell(0).addParagraph();
XWPFRun r1=p1.createRun();
InputStream stream=new FileInputStream("D:\\1.jpg");
r1.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated", Units.toEMU(256), Units.toEMU(256));
document.write(out);
out.close();
System.out.println("測試完成");
}
}
效果圖:
第二頁:
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。