天天看點

C# 将excel表格嵌入到Word中

C# 将excel表格嵌入到Word中

繼續開扒,今天要實作的是使用C#将excel表格嵌入到Word中這個功能,将word表格導入到excel中我已經寫過了,如有需要可參考我之前的文章,在開始前還有一點需要指出的是在我的這個示例中是将excel表格轉換為圖檔,然後再嵌入至Word文檔中。

為了展示一下效果,我做了一個簡單的excel表格,請看源excel工作表截圖:

C# 将excel表格嵌入到Word中

下面看看如何使用代碼:

第一步:建立一個Visual C#控制台項目,添加引用并使用如下命名空間:

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls;
      

第二步:建立一個ConvertExcelToImage(string excelFile)方法,将excel的工作表轉換為圖檔。 

static Image ConvertExcelToImage(string excelFile)
{
    //建立一個excel workbook對象
    Workbook workbook = new Workbook();
    //加載excel檔案
    workbook.LoadFromFile(excelFile);
    //擷取第一個工作表
    Worksheet sheet = workbook.Worksheets[0];
    //擷取第一個工作表的最後一行
    int lastRow = sheet.LastRow;
    //擷取第一個工作表的最後一列
    int lastColumn = sheet.LastColumn;

    //将第一個工作表轉換為圖檔
    Image image = workbook.Worksheets[0].SaveToImage(1, 1, lastRow, lastColumn);
    return image;
}
      

第三步:在主函數内,建立一個word文檔對象,并給它添加一個section和段落。

//建立一個word文檔
Document doc = new Document();
//添加一個section
Section section = doc.AddSection();
//添加一個段落
Paragraph paragraph = section.AddParagraph();
      

第四步:擷取excel檔案的路徑,建立一個DocPicture類的對象,調用ConvertExcelToImage()方法将excel工作表轉換為圖檔并加載該圖檔。

string excelfile = "Customers.xlsx";
//建立一個DocPicture類的對象
DocPicture pic = new DocPicture(doc);
//将excel工作表轉換為圖檔并加載
pic.LoadImage(ConvertExcelToImage(excelfile));
      

第五步:将圖檔嵌入到Word文檔的段落中。 

paragraph.AppendOleObject("Customers.xlsx", pic, Spire.Doc.Documents.OleObjectType.ExcelWorksheet);
      

第六步:儲存文檔。

doc.SaveToFile("Output.docx", Spire.Doc.FileFormat.Docx); 
      

嵌入到word文檔的效果圖:

C# 将excel表格嵌入到Word中

全部代碼:

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls; 

namespace Embed_Excel_to_Word
{
    class Program
    {
        static Image ConvertExcelToImage(string excelFile)
        {
            //建立一個excel workbook對象
            Workbook workbook = new Workbook();
           //加載excel檔案
            workbook.LoadFromFile(excelFile);
            //擷取第一個工作表
            Worksheet sheet = workbook.Worksheets[0];
            //擷取第一個工作表的最後一行
            int lastRow = sheet.LastRow;
            //擷取第一個工作表的最後一列
            int lastColumn = sheet.LastColumn;

            //将第一個工作表轉換為圖檔
            Image image = workbook.Worksheets[0].SaveToImage(1, 1, lastRow, lastColumn);
            return image;
        }

        static void Main(string[] args)
        {
            //建立一個word文檔
            Document doc = new Document();
            //添加一個section
            Section section = doc.AddSection();
            //添加一個段落
            Paragraph paragraph = section.AddParagraph();
            //擷取excel檔案的路徑
            string excelfile = "Customers.xlsx";
            //建立一個DocPicture類的對象pic
            DocPicture pic = new DocPicture(doc);
            //将excel工作表轉換為圖檔并加載
            pic.LoadImage(ConvertExcelToImage(excelfile));
            //将圖檔嵌入到word文檔中
            paragraph.AppendOleObject("Customers.xlsx", pic, Spire.Doc.Documents.OleObjectType.ExcelWorksheet);

            //儲存word文檔
            doc.SaveToFile("Output.docx", Spire.Doc.FileFormat.Docx);
        }
    }
}
      

總結:

需要注意的是E-iceblue的excel和word元件是兩個獨立的元件,一起使用會起沖突抛出異常,是以這裡我使用的是

Office

元件,也就是添加office元件裡的excel和word相關的dll檔案作為引用。