C# 如何将Excel表格复制到Word中并保留格式
C# 如何將Excel表格復制到Word中并保留格式
在以前的文章中,我分享了如何使用免費控件將word表格中的數據導入到excel中,在本篇文章中我將介紹如何將Excel表格復制到Word表格中,并同時保留原Excel表格的格式。
這里我們需要使用到一個組件,叫做Spire.Office。它是一個企業級的Office組件,包含了Spire.Doc, Spire XLS, Spire.Spreadsheet, ?Spire.Presentation, Spire.PDF, Spire.DataExport, Spire.OfficeViewer, Spire.PDFViewer, Spire.DocViewer和Spire.BarCode等組件,使用它我們可以實現在.NET應用程序中查看、操作、轉換及打印Office文檔,PDF文檔,創建Barcode以及數據導入導出等工作。這里我們需要使用到的是其中的Spire.Doc和Spire.XLS組件。
首先,下載并安裝Spire.Office,然后打開Visual Studio,創建項目并引用Spire.Doc.dll和Spire.Xls.dll。以下是我們需要用到的命名空間:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Xls;詳細步驟如下:
步驟1:從系統加載Excel文件并獲取它的第一個工作表。
Workbook workbook = new Workbook(); workbook.LoadFromFile("DatatableSample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; 步驟2:創建一個新的Word文檔,添加一個表格并設置它的行列數與Excel表格中的行列數一致。 Document doc = new Document(); //添加表格 Table table = doc.AddSection().AddTable(true); //設置行列數 table.ResetCells(sheet.LastRow, sheet.LastColumn); 步驟3:將Excel表格中的數據寫入到word表格里,并調用自定義的方法CopyStyle()將Excel表格中的格式復制到word表格。 for (int r = 1; r <=sheet.LastRow; r++) {for (int c = 1; c <= sheet.LastColumn; c++){CellRange xCell =sheet.Range[r, c];TableCell wCell =table.Rows[r - 1].Cells[c - 1];//將數據寫入到word表格TextRange textRange =wCell.AddParagraph().AppendText(xCell.NumberText);//復制格式到word表格CopyStyle(textRange,xCell, wCell);} }自定義的方法CopyStyle( TextRange wTextRange, CellRangexCell, TableCell wCell)如下:參數:
wTextRange: Word表格中的文本
?xCell: Excel表格中的單元格
wCell: Word表格中的單元格
private static void CopyStyle(TextRangewTextRange, CellRange xCell, TableCell wCell) {//復制字體格式wTextRange.CharacterFormat.TextColor= xCell.Style.Font.Color;wTextRange.CharacterFormat.FontSize= (float)xCell.Style.Font.Size;wTextRange.CharacterFormat.FontName= xCell.Style.Font.FontName;wTextRange.CharacterFormat.Bold= xCell.Style.Font.IsBold;wTextRange.CharacterFormat.Italic= xCell.Style.Font.IsItalic;//復制背景顏色wCell.CellFormat.BackColor= xCell.Style.Color;//復制文本排列方式switch(xCell.HorizontalAlignment){case HorizontalAlignType.Left:wTextRange.OwnerParagraph.Format.HorizontalAlignment= HorizontalAlignment.Left;break;case HorizontalAlignType.Center:wTextRange.OwnerParagraph.Format.HorizontalAlignment= HorizontalAlignment.Center;break;case HorizontalAlignType.Right:wTextRange.OwnerParagraph.Format.HorizontalAlignment= HorizontalAlignment.Right;break;} }步驟4:保存文檔。
doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);運行結果:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Xls;namespace 復制Excel表格到Word并保留格式 {class Program{static void Main(string[] args){//加載Excel文檔Workbook workbook = newWorkbook();workbook.LoadFromFile("DatatableSample.xlsx");Worksheet sheet = workbook.Worksheets[0];//創建一個新的word文檔Document doc = newDocument();//添加一個表格到word文檔Table table = doc.AddSection().AddTable(true);//設置word表格的行列數table.ResetCells(sheet.LastRow, sheet.LastColumn);for (int r = 1; r<= sheet.LastRow; r++){for (intc = 1; c <= sheet.LastColumn; c++){CellRange xCell = sheet.Range[r, c];TableCell wCell = table.Rows[r -1].Cells[c - 1];//將Excel表格中的數據寫入到word表格TextRangetextRange = wCell.AddParagraph().AppendText(xCell.NumberText);//將Excel表格中的格式復制到word表格CopyStyle(textRange, xCell, wCell);}}//設置word表格的列寬for (int i = 0; i< table.Rows.Count; i++){for (intj = 0; j < table.Rows[i].Cells.Count; j++){table.Rows[i].Cells[j].Width = 60f;}}//保存文檔并打開doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}private static void CopyStyle(TextRangewTextRange, CellRange xCell, TableCell wCell){//復制Excel單元格的字體格式到word表格wTextRange.CharacterFormat.TextColor = xCell.Style.Font.Color;wTextRange.CharacterFormat.FontSize = (float)xCell.Style.Font.Size;wTextRange.CharacterFormat.FontName = xCell.Style.Font.FontName;wTextRange.CharacterFormat.Bold = xCell.Style.Font.IsBold;wTextRange.CharacterFormat.Italic = xCell.Style.Font.IsItalic;//復制Excel單元格的背景顏色到word表格wCell.CellFormat.BackColor =xCell.Style.Color;//復制Excel單元格的字體排列方式到word表格switch (xCell.HorizontalAlignment){case HorizontalAlignType.Left:wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;break;case HorizontalAlignType.Center:wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center;break;case HorizontalAlignType.Right:wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;break;}}} }?
?
?
總結
以上是生活随笔為你收集整理的C# 如何将Excel表格复制到Word中并保留格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据新闻的四大发展特点
- 下一篇: c# char unsigned_dll