如何通过Java应用程序将Word转为Excel
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
平时在工作中,很多小伙伴会习惯性地将文件保存为Word文档格式,但有时会发现某些文件如果保存成Excel表格可能会更好地呈现。例如有的文本在Word文本中不如在Excel工作表编辑计算方便,所以要把Word转Excel表格。那么对于之前已经保存成Word格式的文件该怎么转成Excel文件格式呢?下面就给大家分享一个高效又方便的方法,通过编程方式将Word转为Excel。
引入jar包
导入方法1:
手动引入。将 Free Spire.Office for
Java 下载到本地,解压,找到lib文件夹下的Spire.Office.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:
导入方法2:如果您想通过 Maven安装,则可以在 pom.xml 文件中添加以下代码导入 JAR文件。
repositories
repository
idcom.eiceblueid
nameeicebluename
urlhttps:
repository
repositories
dependencies
dependency
groupIdeicebluegroupId
artifactIdspire.office.freeartifactId
versionversion
dependency
dependencies
将Word转为Excel具体步骤
为了使此代码示例易于理解,我们创建了以下三个执行特定功能的自定义方法。
- exportTableInExcel() - 将数据从 Word 表格导出到指定的Excel 单元格。
- copyContentInTable() - 将内容从 Word 中的表格单元格复制到Excel 单元格。
- copyTextAndStyle() - 将带格式的文本从 Word段落复制到 Excel 单元格。
以下步骤演示了如何使用 Spire.Office for Java 将数据从 Word 文档导出到工作表。
- 创建一个 Document 对象以加载 Word 文件。
- 创建一个 Workbook 对象并向其添加一个名为“WordToExcel”的工作表。
- 遍历 Word 文档中的所有节,遍历某个节下的所有文档对象,然后判断一个文档对象是段落还是表格。
- 如果文档对象是一个段落,使用 copyTextAndStyle() 方法将段落写在 Excel 的指定单元格中。
- 如果文档对象是表格,则使用 exportTableInExcel() 方法将表格数据从 Word 导出到 Excel 单元格。
- 自动调整 Excel 中的行高和列宽,使单元格内的数据不会超出单元格的边界。
- 使用 Workbook.saveToFile() 方法将工作簿保存到 Excel 文件。
完整代码
Java
com.spire.doc.;
com.spire.doc.documents.Paragraph;
com.spire.doc.fields.DocPicture;
com.spire.doc.fields.TextRange;
com.spire.xls.;
java.awt.;
{
main([] args) {
Document doc Document();
doc.loadFromFile();
Workbook wb Workbook();
wb.getWorksheets().clear();
Worksheet worksheet wb.createEmptySheet();
row ;
column ;
( i ; i doc.getSections().getCount(); i) {
Section section doc.getSections().get(i);
( j ; j section.getBody().getChildObjects().getCount(); j) {
DocumentObject documentObject section.getBody().getChildObjects().get(j);
(documentObject Paragraph) {
CellRange cell worksheet.getCellRange(row, column);
Paragraph paragraph (Paragraph) documentObject;
copyTextAndStyle(cell, paragraph);
row;
}
(documentObject Table) {
Table table (Table) documentObject;
currentRow exportTableInExcel(worksheet, row, table);
row currentRow;
}
}
}
worksheet.getAllocatedRange().isWrapText();
worksheet.getAllocatedRange().autoFitRows();
worksheet.getAllocatedRange().autoFitColumns();
wb.saveToFile(, ExcelVersion.Version2013);
}
exportTableInExcel(Worksheet worksheet, row, Table table) {
CellRange cell;
column;
( i ; i table.getRows().getCount(); i) {
column ;
TableRow tbRow table.getRows().get(i);
( j ; j tbRow.getCells().getCount(); j) {
TableCell tbCell tbRow.getCells().get(j);
cell worksheet.getCellRange(row, column);
cell.borderAround(LineStyleType.Thin, Color.BLACK);
copyContentInTable(tbCell, cell);
column;
}
row;
}
row;
}
copyContentInTable(TableCell tbCell, CellRange cell) {
Paragraph newPara Paragraph(tbCell.getDocument());
( i ; i tbCell.getChildObjects().getCount(); i) {
DocumentObject documentObject tbCell.getChildObjects().get(i);
(documentObject Paragraph) {
Paragraph paragraph (Paragraph) documentObject;
( j ; j paragraph.getChildObjects().getCount(); j) {
DocumentObject cObj paragraph.getChildObjects().get(j);
newPara.getChildObjects().add(cObj.deepClone());
}
(i tbCell.getChildObjects().getCount() ) {
newPara.appendText();
}
}
}
copyTextAndStyle(cell, newPara);
}
copyTextAndStyle(CellRange cell, Paragraph paragraph) {
RichText richText cell.getRichText();
richText.setText(paragraph.getText());
startIndex ;
( i ; i paragraph.getChildObjects().getCount(); i) {
DocumentObject documentObject paragraph.getChildObjects().get(i);
(documentObject TextRange) {
TextRange textRange (TextRange) documentObject;
fontName textRange.getCharacterFormat().getFontName();
isBold textRange.getCharacterFormat().getBold();
Color textColor textRange.getCharacterFormat().getTextColor();
fontSize textRange.getCharacterFormat().getFontSize();
textRangeText textRange.getText();
strLength textRangeText.length();
ExcelFont font ExcelFont(cell.getWorksheet().getWorkbook().createFont());
font.setColor(textColor);
font.isBold(isBold);
font.setSize(fontSize);
font.setFontName(fontName);
endIndex startIndex strLength;
richText.setFont(startIndex, endIndex, font);
startIndex strLength;
}
(documentObject DocPicture) {
DocPicture picture (DocPicture) documentObject;
cell.getWorksheet().getPictures().add(cell.getRow(), cell.getColumn(), picture.getImage());
cell.getWorksheet().setRowHeightInPixels(cell.getRow(), , picture.getImage().getHeight());
}
}
(paragraph.getFormat().getHorizontalAlignment()) {
Left:
cell.setHorizontalAlignment(HorizontalAlignType.Left);
;
Center:
cell.setHorizontalAlignment(HorizontalAlignType.Center);
;
Right:
cell.setHorizontalAlignment(HorizontalAlignType.Right);
;
}
}
}
效果图
—本文完—
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |