免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

Java導(dǎo)出帶格式的Excel數(shù)據(jù)到Word表格

前言在Word中創(chuàng)建報告時,我們經(jīng)常會遇到這樣的情況:我們需要將數(shù)據(jù)從Excel中復(fù)制和粘貼到Word中,這樣讀者就可以直接在Word中瀏覽數(shù)據(jù),而不用打開Excel文檔 。在本文中,您將學習如何使用Spire.Office for Java將Excel數(shù)據(jù)轉(zhuǎn)換為Word表格并保留格式 。
程序環(huán)境安裝Spire.Office for Java首先,你需要在你的Java程序中添加Spire.Office.jar文件作為一個依賴項 。該JAR文件可以從這個鏈接下載 。如果你使用Maven,你可以通過在項目的pom.xml文件中添加以下代碼,在你的應(yīng)用程序中輕松導(dǎo)入該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數(shù)據(jù)導(dǎo)出到Word表格步驟創(chuàng)建一個Workbook對象,并使用Workbook.loadFromFile()方法加載一個Excel樣本文件 。? 使用Workbook.getWorksheets().get()方法獲取一個特定的工作表 。? 創(chuàng)建一個Document對象,并向其添加一個章節(jié) 。? 使用Section.addTable()方法添加一個表格 。? 檢測工作表中的合并單元格,并使用自定義方法mergeCells()合并Word tale中的相應(yīng)單元格 。? 使用CellRange.getValue() 方法獲取特定Excel單元格的值,并使用TableCell.addParagraph().appendText()方法將其添加到Word表中的一個單元格 。? 使用自定義方法copyStyle()將字體樣式和單元格樣式從Excel復(fù)制到Word表格中 。? 使用Document.saveToFile()方法將文檔保存到Word文件中 。
代碼示例
點擊查看代碼【Java導(dǎo)出帶格式的Excel數(shù)據(jù)到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);//創(chuàng)建一個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++) {//設(shè)置行高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復(fù)制字體和單元格樣式到WordcopyStyle(textRange, xCell, wCell);}}//Save the document to a Word file保存文檔為Word文件doc.saveToFile("ExportToWord.docx", FileFormat.Docx);}//如果有合并的區(qū)域,則合并單元格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表格中的對應(yīng)單元格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);}}}}//復(fù)制Excel單元格樣式到Word表格private static void copyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) {//復(fù)制字體樣式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());//復(fù)制背景色wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());//復(fù)制水平對齊方式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;}//復(fù)制垂直對齊方式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;}}}

經(jīng)驗總結(jié)擴展閱讀