Java 如何將表格數(shù)據(jù)導(dǎo)入word文檔中
Java 表格數(shù)據(jù)導(dǎo)入word文檔中
個(gè)人覺得這個(gè)功能實(shí)在搞笑,沒什么意義,沒辦法提了需求就要實(shí)現(xiàn),(太好說(shuō)話了把我)
我的實(shí)現(xiàn)是再word中生成一個(gè)與 excel行,列 一樣的一個(gè)表格,然后把從excel拿到的數(shù)據(jù)(exList參數(shù))依次放到word表格中
public static void createFile(HttpServletResponse response, String fileName, List<List<String>> exList) {
try {
setResponseHeader(response, fileName);
//生成一個(gè)word模版文件
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable(exList.size(), exList.get(0).size());
XWPFTableRow row;
for (int i = 0; i < exList.size(); i++) {
List<String> sdf = exList.get(i);
row = table.getRow(i);
for (int j = 0; j < exList.get(i).size(); j++) {
String s =sdf.get(j);
row.getCell(j).setText(s);
row.getCell(j).setWidthType(TableWidthType.AUTO);
}
//將數(shù)據(jù)插入表格中 pos:0 表示 第一個(gè)表格
document.setTable(0,table);
}
ServletOutputStream outputStream = response.getOutputStream();
BufferedOutputStream bufferStream = new BufferedOutputStream(outputStream, 1024);
document.write(bufferStream);
document.close();
bufferStream.close();;
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setResponseHeader(HttpServletResponse response, String name) {
try {
name = new String(name.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("multipart/form-data");
//要保存的文件名
response.setHeader("Content-Disposition", "attachment;filename=" + name + ".docx");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}
Java poi導(dǎo)入word表格數(shù)據(jù)的經(jīng)過(guò)
一、過(guò)程及遇到的問題和解決思路
需要導(dǎo)入的是一個(gè)word文檔,內(nèi)容是以表格的形式保存在word中
1、poi對(duì)word表格的空格處可以自動(dòng)識(shí)別出來(lái)并賦值為 " ",這一點(diǎn)比poi導(dǎo)入excel人性化(excel默認(rèn)是跳過(guò)這個(gè)空格)
2、對(duì)于某些情況下,肉眼無(wú)法看出表格格式問題,但是程序可以識(shí)別出來(lái),懷疑是表格后期人工修改過(guò),導(dǎo)致表格外觀沒問題但是行列屬性不一致,導(dǎo)致讀取時(shí)遇到這些地方報(bào)錯(cuò),解決思路:可以在讀取每一行之前先判斷列數(shù)是否正確,poi中可以獲取每行的列數(shù),不正確的證明此列有問題,舍棄跳過(guò)。
二、代碼
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
package com.example.importtomysql;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ImportWord {
public List<TableColumn> testReadByDoc(String path) throws Exception {
File f = new File(path);
InputStream is = new FileInputStream(f);
HWPFDocument doc = new HWPFDocument(is);
//輸出書簽信息
// this.printInfo(doc.getBookmarks());
//輸出文本
// System.out.println(doc.getDocumentText());
Range range = doc.getRange();
// this.printInfo(range);
//讀表格
List<TableColumn> tableColumns = this.readTable(range);
//讀列表
// this.readList(range);
//把當(dāng)前HWPFDocument寫到輸出流中
// doc.write(new FileOutputStream("D:\\temp\\test.doc"));
is.close();
return tableColumns;
}
/**
* 輸出書簽信息
* @param bookmarks
*/
private void printInfo(Bookmarks bookmarks) {
int count = bookmarks.getBookmarksCount();
System.out.println("書簽數(shù)量:" + count);
Bookmark bookmark;
for (int i=0; i<count; i++) {
bookmark = bookmarks.getBookmark(i);
System.out.println("書簽" + (i+1) + "的名稱是:" + bookmark.getName());
System.out.println("開始位置:" + bookmark.getStart());
System.out.println("結(jié)束位置:" + bookmark.getEnd());
}
}
/**
* 讀表格
* 每一個(gè)回車符代表一個(gè)段落,所以對(duì)于表格而言,每一個(gè)單元格至少包含一個(gè)段落,每行結(jié)束都是一個(gè)段落。
* @param range
*/
private List<TableColumn> readTable(Range range) {
List<TableColumn> tableColumns = new ArrayList<>();
//遍歷range范圍內(nèi)的table。
TableIterator tableIter = new TableIterator(range);
Table table;
TableRow row;
TableCell cell;
int i=0;
int k=0;
while (tableIter.hasNext()&&i<=1) {
table = tableIter.next();
int rowNum = table.numRows();
for (int j=0; j<rowNum; j++) {
TableColumn tableColumn = new TableColumn();
row = table.getRow(j);
int cellNum = row.numCells();
// for (int k=0; k<cellNum; k++) {
// cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//
// //輸出單元格的文本
// System.out.println(cell.text().trim());
// }
k++;
if(12==cellNum){
tableColumn.setId(row.getCell(0).text().trim());
tableColumn.setSscj(row.getCell(1).text().trim());
tableColumn.setQlfl(row.getCell(2).text().trim());
tableColumn.setXmmc(row.getCell(3).text().trim());
tableColumn.setZx(row.getCell(4).text().trim());
tableColumn.setBlx(row.getCell(5).text().trim());
tableColumn.setSsyj(row.getCell(6).text().trim());
tableColumn.setCbjg(row.getCell(7).text().trim());
tableColumn.setZrsx(row.getCell(8).text().trim());
tableColumn.setSxyj(row.getCell(9).text().trim());
tableColumn.setZzqx(row.getCell(10).text().trim());
tableColumn.setZzyj(row.getCell(11).text().trim());
// tableColumn.setBz(row.getCell(12).text().trim());
tableColumns.add(tableColumn);
if(679==k){
System.out.println(k +" " +row.getCell(0).text().trim()+" " +row.getCell(3).text().trim());
}
// System.out.println(k +" " +row.getCell(0).text().trim()+" "+row.getCell(3).text().trim());
}else {
System.out.println(k);
}
}
i++;
}
return tableColumns;
}
/**
* 讀列表
* @param range
*/
private void readList(Range range) {
int num = range.numParagraphs();
Paragraph para;
for (int i=0; i<num; i++) {
para = range.getParagraph(i);
if (para.isInList()) {
System.out.println("list: " + para.text());
}
}
}
/**
* 輸出Range
* @param range
*/
private void printInfo(Range range) {
//獲取段落數(shù)
int paraNum = range.numParagraphs();
System.out.println(paraNum);
for (int i=0; i<paraNum; i++) {
System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text());
}
int secNum = range.numSections();
System.out.println(secNum);
Section section;
for (int i=0; i<secNum; i++) {
section = range.getSection(i);
System.out.println(section.getMarginLeft());
System.out.println(section.getMarginRight());
System.out.println(section.getMarginTop());
System.out.println(section.getMarginBottom());
System.out.println(section.getPageHeight());
System.out.println(section.text());
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JPA如何設(shè)置表名和實(shí)體名,表字段與實(shí)體字段的對(duì)應(yīng)
這篇文章主要介紹了JPA如何設(shè)置表名和實(shí)體名,表字段與實(shí)體字段的對(duì)應(yīng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
聊聊Spring data jpa @query使用原生SQl,需要注意的坑
這篇文章主要介紹了Spring data jpa@query使用原生SQl,需要注意的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
關(guān)于springboot集成swagger及knife4j的增強(qiáng)問題
這篇文章主要介紹了springboot集成swagger以及knife4j的增強(qiáng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java8中Optional的一些常見錯(cuò)誤用法總結(jié)
我們知道 Java 8 增加了一些很有用的 API, 其中一個(gè)就是 Optional,下面這篇文章主要給大家介紹了關(guān)于Java8中Optional的一些常見錯(cuò)誤用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
java向上轉(zhuǎn)型發(fā)生的時(shí)機(jī)知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理分享的是關(guān)于java向上轉(zhuǎn)型發(fā)生的時(shí)機(jī)知識(shí)點(diǎn)內(nèi)容,有興趣的讀者們可以參考下。2021-05-05
Java之SSM中bean相關(guān)知識(shí)匯總案例講解
這篇文章主要介紹了Java之SSM中bean相關(guān)知識(shí)匯總案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
淺談一下maven優(yōu)缺點(diǎn)及使用和特點(diǎn)
這篇文章主要介紹了淺談一下maven優(yōu)缺點(diǎn)及使用和特點(diǎn),一個(gè)項(xiàng)目管理工具軟件,那么maven項(xiàng)目有什么優(yōu)缺點(diǎn)呢,讓我們一起來(lái)看看吧2023-03-03
解決idea2020.2遇到pom.xml文件報(bào)錯(cuò)maven插件tomcat7的問題
這篇文章主要介紹了idea2020.2遇到pom.xml文件報(bào)錯(cuò)maven插件tomcat7的問題,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

