使用POI对word文档进行取读
导入POI包
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
word简单表格
public Object wordscience(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String errInfo = "success";
InputStream ins = null;
ins = file.getInputStream();
while (it.hasNext()) {
PageData savePd = new PageData();
XWPFTable table = it.next();
//System.out.println("这是第" + num + "个表的数据");
List<XWPFTableRow> rows = table.getRows();
XWPFTableRow rowOne = rows.get(0);
String stringOne = rowOne.getTableCells().get(0).getText();
if (stringOne.equals("测试")) {
//读取每一行数据
for (int i = 0; i < rows.size(); i++) {
XWPFTableRow row = rows.get(i);
//读取每一列数据
List<XWPFTableCell> cells = row.getTableCells();
for (int j = 0; j < cells.size(); j++) {
XWPFTableCell cell = cells.get(j);
//cell.setText("66666666666666666啊");
//输出当前的单元格的数据
System.out.print(cell.getText() + "\t");
if (cell.getText().equals("测试1")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试2")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试3")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试4")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试5")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试6")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试7")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试8")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试9")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试10")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试11")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试12")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试13")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
if (cell.getText().equals("测试14")) {
savePd.put("projectname1", cells.get(j + 1).getText());
}
}
}
word列表
public Object wordscience(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String errInfo = "success";
InputStream ins = null;
ins = file.getInputStream();
while (it.hasNext()) {
PageData savePd = new PageData();
XWPFTable table = it.next();
//System.out.println("这是第" + num + "个表的数据");
List<XWPFTableRow> rows = table.getRows();
XWPFTableRow rowOne = rows.get(0);
String stringOne = rowOne.getTableCells().get(0).getText();
if (stringOne.equals("测试1")) {
//读取每一行数据
for (int i = 2; i < rows.size(); i++) {
XWPFTableRow row = rows.get(i);
//读取每一列数据
List<XWPFTableCell> cells = row.getTableCells();
PageData newsavePd = new PageData();
newsavePd.put("12", office_id);
newsavePd.put("12", cells.get(0).getText());
newsavePd.put("12", cells.get(1).getText());
newsavePd.put("12", cells.get(2).getText());
newsavePd.put("12", cells.get(3).getText());
newsavePd.put("12", cells.get(4).getText());
newsavePd.put("12", cells.get(5).getText());
newsavePd.put("12", juser.get("ID"));
newsavePd.put("12", juser.get("NAME"));
newsavePd.put("12", newdate);
criticalnodeService.save(newsavePd);
}
}
}
}
杜兰特下赛季加油,篮网冲冲冲!!!