Java导出带格式的Excel数据到Word表格

前言在Word中创建报告时,我们经常会遇到这样的情况:我们需要将数据从Excel中复制和粘贴到Word中 , 这样读者就可以直接在Word中浏览数据,而不用打开Excel文档 。在本文中 , 您将学习如何使用Spire.Office for Java将Excel数据转换为Word表格并保留格式 。
程序环境安装Spire.Office for Java首先 , 你需要在你的Java程序中添加Spire.Office.jar文件作为一个依赖项 。该JAR文件可以从这个链接下载 。如果你使用Maven , 你可以通过在项目的pom.xml文件中添加以下代码,在你的应用程序中轻松导入该JAR文件 。

点击查看代码<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url> https://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.office</artifactId><version>7.9.6</version></dependency></dependencies>
小tips:请注意版本号的变化
将带格式的Excel数据导出到Word表格步骤创建一个Workbook对象,并使用Workbook.loadFromFile()方法加载一个Excel样本文件 。? 使用Workbook.getWorksheets().get()方法获取一个特定的工作表 。? 创建一个Document对象,并向其添加一个章节 。? 使用Section.addTable()方法添加一个表格 。? 检测工作表中的合并单元格,并使用自定义方法mergeCells()合并Word tale中的相应单元格 。? 使用CellRange.getValue() 方法获取特定Excel单元格的值,并使用TableCell.addParagraph().appendText()方法将其添加到Word表中的一个单元格 。? 使用自定义方法copyStyle()将字体样式和单元格样式从Excel复制到Word表格中 。? 使用Document.saveToFile()方法将文档保存到Word文件中 。
代码示例
点击查看代码import com.spire.doc.*;import com.spire.doc.FileFormat;import com.spire.doc.documents.HorizontalAlignment;import com.spire.doc.documents.PageOrientation;import com.spire.doc.documents.VerticalAlignment;import com.spire.doc.fields.TextRange;import com.spire.xls.*;public class ExportExcelToWord {public static void main(String[] args) {//下载一个Excel文件Workbook workbook = new Workbook();workbook.loadFromFile("C:/Users/Administrator/Desktop/sample.xlsx");//得到第一张工作表Worksheet sheet = workbook.getWorksheets().get(0);//创建一个Word文档Document doc = new Document();Section section = doc.addSection();section.getPageSetup().setOrientation(PageOrientation.Landscape);//添加一个表格Table table = section.addTable(true);table.resetCells(sheet.getLastRow(), sheet.getLastColumn());//合并单元格mergeCells(sheet, table);for (int r = 1; r <= sheet.getLastRow(); r++) {//设置行高table.getRows().get(r - 1).setHeight((float) sheet.getRowHeight(r));for (int c = 1; c <= sheet.getLastColumn(); c++) {CellRange xCell = sheet.getCellRange(r, c);TableCell wCell = table.get(r - 1, c - 1);//获得特定Excel单元格的值并将其添加到Word表格单元格TextRange textRange = wCell.addParagraph().appendText(xCell.getValue());// 从Excel复制字体和单元格样式到WordcopyStyle(textRange, xCell, wCell);}}//Save the document to a Word file保存文档为Word文件doc.saveToFile("ExportToWord.docx", FileFormat.Docx);}//如果有合并的区域,则合并单元格private static void mergeCells(Worksheet sheet, Table table) {if (sheet.hasMergedCells()) {//从Excel中获取合并的单元格范围CellRange[] ranges = sheet.getMergedCells();for (int i = 0; i < ranges.length; i++) {int startRow = ranges[i].getRow();int startColumn = ranges[i].getColumn();int rowCount = ranges[i].getRowCount();int columnCount = ranges[i].getColumnCount();//合并Word表格中的对应单元格if (rowCount > 1 && columnCount > 1) {for (int j = startRow; j <= startRow + rowCount ; j++) {table.applyHorizontalMerge(j - 1, startColumn - 1, startColumn - 1 + columnCount - 1);}table.applyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1 );}if (rowCount > 1 && columnCount == 1 ) {table.applyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1);}if (columnCount > 1 && rowCount == 1 ) {table.applyHorizontalMerge(startRow - 1, startColumn - 1,startColumn - 1 + columnCount-1);}}}}//复制Excel单元格样式到Word表格private static void copyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) {//复制字体样式wTextRange.getCharacterFormat().setTextColor(xCell.getStyle().getFont().getColor());wTextRange.getCharacterFormat().setFontSize((float) xCell.getStyle().getFont().getSize());wTextRange.getCharacterFormat().setFontName(xCell.getStyle().getFont().getFontName());wTextRange.getCharacterFormat().setBold(xCell.getStyle().getFont().isBold());wTextRange.getCharacterFormat().setItalic(xCell.getStyle().getFont().isItalic());//复制背景色wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());//复制水平对齐方式switch (xCell.getHorizontalAlignment()) {case Left:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Left);break;case Center:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);break;case Right:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Right);break;}//复制垂直对齐方式switch (xCell.getVerticalAlignment()) {case Bottom:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Bottom);break;case Center:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);break;case Top:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Top);break;}}}

推荐阅读