zoukankan      html  css  js  c++  java
  • C# 将excel表格嵌入到Word中

    C# 将excel表格嵌入到Word

    继续开扒,今天要实现的是使用C#将excel表格嵌入到Word中这个功能,将word表格导入到excel中我已经写过了,如有需要可参考我之前的文章,在开始前还有一点需要指出的是在我的这个示例中是将excel表格转换为图片,然后再嵌入至Word文档中。

    为了展示一下效果,我做了一个简单的excel表格,请看源excel工作表截图:

                           

    下面看看如何使用代码:

    第一步:新建一个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文档的效果图:

    全部代码:

    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文件作为引用。

  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/Yesi/p/5162564.html
Copyright © 2011-2022 走看看