C# 將excel表格嵌入到Word中

C# 將excel表格嵌入到Wordhtml

繼續開扒,今天要實現的是使用C#將excel表格嵌入到Word中這個功能,將word表格導入到excel中我已經寫過了,若有須要可參考我以前的文章,在開始前還有一點須要指出的是在個人這個示例中是將excel表格轉換爲圖片,而後再嵌入至Word文檔中。函數

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

                       

下面看看如何使用代碼:excel

第一步:新建一個Visual C#控制檯項目,添加引用並使用以下命名空間:orm

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

 

第二步:建立一個ConvertExcelToImage(string excelFile)方法,將excel的工做錶轉換爲圖片。 htm

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工做錶轉換爲圖片並加載該圖片。blog

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文檔的效果圖:

 

所有代碼:

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文件做爲引用。

相關文章
相關標籤/搜索