使用java语言将 word文档转excel表格,首先导入apache的poi依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
然后是java启动类
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class WordToExcel {
public static void main(String[] args) {
try {
// 读取Word文档
InputStream inputStream = new FileInputStream("example.doc");
XWPFDocument document = new XWPFDocument(inputStream);
// 创建Excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 读取Word文档中的表格数据,并写入Excel工作簿
for (XWPFTable table : document.getTables()) {
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
Row excelRow = sheet.createRow(rows.indexOf(row));
List<XWPFTableCell> tableCells = row.getTableCells();
for (XWPFTableCell cell : tableCells) {
Cell excelCell = excelRow.createCell(tableCells.indexOf(cell));
excelCell.setCellValue(cell.getText());
}
}
}
// 将Excel工作簿保存到文件
OutputStream outputStream = new FileOutputStream("example.xls");
workbook.write(outputStream);
// 关闭流
outputStream.close();
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这样就可以使 example.doc文档转换成想要的example.xls表格
如果有其他文件的转换,参考:
免费在线格式转换-zamzarhttps://www.zamzar.com/
免费在线格式转换-online-converthttps://www.online-convert.com/
如果有意见或建议,可以评论区探讨~~~