⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readerexcel.txt

📁 Excel是微软公司办公自动化套件中的一个软件
💻 TXT
📖 第 1 页 / 共 2 页
字号:
                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

                //移动焦点并换行
                object count = 14;
                object WdLine = Word.WdUnits.wdLine;//换一行;
                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                 WordApp.Selection.TypeParagraph();//插入段落

                 //文档中创建表格
                 Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
                 //设置表格样式
                 newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                 newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                 newTable.Columns[1].Width = 100f;
                 newTable.Columns[2].Width = 220f;
                 newTable.Columns[3].Width = 105f;

                 //填充表格内容
                 newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                 newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                 //合并单元格
                 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                 WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
                        
                 //填充表格内容
                 newTable.Cell(2, 1).Range.Text = "产品基本信息";
                 newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                 //合并单元格
                 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                  //填充表格内容
                  newTable.Cell(3, 1).Range.Text = "品牌名称:";
                  newTable.Cell(3, 2).Range.Text = BrandName;
                  //纵向合并单元格
                  newTable.Cell(3, 3).Select();//选中一行
                  object moveUnit = Word.WdUnits.wdLine;
                  object moveCount = 5;
                  object moveExtend = Word.WdMovementType.wdExtend;
                   WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                   WordApp.Selection.Cells.Merge();
                   //插入图片
                   string FileName = Picture;//图片所在路径
                   object LinkToFile = false;
                   object SaveWithDocument = true;
                   object Anchor = WordDoc.Application.Selection.Range;
                   WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                    //将图片设置为四周环绕型
                    Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                    s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
                        
                    newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                    newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                     //在表格中增加行
                     WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
                      
                     WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                     WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

                    //文件保存
                    WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                    WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                    WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                    message=name+"文档生成成功,以保存到C:CNSI下";
            }
            catch
            ...{
                message = "文件导出异常!";
            }
            return message;
        }


案例3 
C#向Excel报表中插入图片的2种方法

注意:使用之前需要引用COM:Microsoft Office 11.0 Object Library 如果引用列表中没有,需要自行添加 C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE

调用方法:

<!--StartFragment-->MengXianhui.Utility.ExcelReport.InsertPictureToExcel ipt = new MengXianhui.Utility.ExcelReport.InsertPictureToExcel();
ipt.Open();
ipt.InsertPicture("B2", @"C:\Excellogo.gif");
ipt.InsertPicture("B8", @"C:\Excellogo.gif",120,80);
ipt.SaveFile(@"C:\ExcelTest.xls");
ipt.Dispose(); 


简单包装的类:

<!--StartFragment-->using System;
using System..Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace MengXianhui.Utility.ExcelReport
{
  class InsertPictureToExcel
  {
    /// <summary>
    /// 打开没有模板的操作。
    /// </summary>
    public void Open( )
    {
      this.Open(String.Empty);
    }

    /// <summary>
    /// 功能:实现Excel应用程序的打开
    /// </summary>
    /// <param name="TemplateFilePath">模板文件物理路径</param>
    public void Open( string TemplateFilePath )
    {
      //打开对象
      m_objExcel = new Excel.Application();
      m_objExcel.Visible = false;
      m_objExcel.DisplayAlerts = false;

      if (m_objExcel.Version != "11.0")
      {
        MessageBox.Show("您的 Excel 版本不是 11.0 (Office 2003),操作可能会出现问题。");
        m_objExcel.Quit();
        return;
      }

      m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
      if (TemplateFilePath.Equals(String.Empty))
      {
        m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
      }
      else
      {
        m_objBook = m_objBooks.Open(TemplateFilePath, m_objOpt, m_objOpt, m_objOpt, m_objOpt, 
          m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
      }
      m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
      m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
      m_objExcel.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(m_objExcel_WorkbookBeforeClose);
    }

    private void m_objExcel_WorkbookBeforeClose( Excel.Workbook m_objBooks, ref bool _Cancel )
    {
      MessageBox.Show("保存完毕!");
    }

    /// <summary>
    /// 将图片插入到指定的单元格位置。
    /// 注意:图片必须是绝对物理路径
    /// </summary>
    /// <param name="RangeName">单元格名称,例如:B4</param>
    /// <param name="PicturePath">要插入图片的绝对路径。</param>
    public void InsertPicture( string RangeName, string PicturePath )
    {
      m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
      m_objRange.Select();
      Excel.Pictures pics = (Excel.Pictures)m_objSheet.Pictures(m_objOpt);
      pics.Insert(PicturePath, m_objOpt);
    }

    /// <summary>
    /// 将图片插入到指定的单元格位置,并设置图片的宽度和高度。
    /// 注意:图片必须是绝对物理路径
    /// </summary>
    /// <param name="RangeName">单元格名称,例如:B4</param>
    /// <param name="PicturePath">要插入图片的绝对路径。</param>
    /// <param name="PictuteWidth">插入后,图片在Excel中显示的宽度。</param>
    /// <param name="PictureHeight">插入后,图片在Excel中显示的高度。</param>
    public void InsertPicture( string RangeName, string PicturePath, float PictuteWidth, float PictureHeight )
    {
      m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
      m_objRange.Select();
      float PicLeft, PicTop;
      PicLeft = Convert.ToSingle(m_objRange.Left);
      PicTop = Convert.ToSingle(m_objRange.Top);
      //参数含义:
      //图片路径
      //是否链接到文件
      //图片插入时是否随文档一起保存
      //图片在文档中的坐标位置(单位:points)
      //图片显示的宽度和高度(单位:points)
      //参数详细信息参见:http://msdn2.microsoft.com/zh-cn/library/aa221765(office.11).aspx
      m_objSheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, 
        Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight);
    }

    /// <summary>
    /// 将Excel文件保存到指定的目录,目录必须事先存在,文件名称不一定要存在。
    /// </summary>
    /// <param name="OutputFilePath">要保存成的文件的全路径。</param>
    public void SaveFile( string OutputFilePath )
    {
      m_objBook.SaveAs(OutputFilePath, m_objOpt, m_objOpt,
        m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
        m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

      this.Close();
    }
    /// <summary>
    /// 关闭应用程序
    /// </summary>
    private void Close( )
    {
      m_objBook.Close(false, m_objOpt, m_objOpt);
      m_objExcel.Quit();
    }

    /// <summary>
    /// 释放所引用的COM对象。注意:这个过程一定要执行。
    /// </summary>
    public void Dispose( )
    {
      ReleaseObj(m_objSheets);
      ReleaseObj(m_objBook);
      ReleaseObj(m_objBooks);
      ReleaseObj(m_objExcel);
      System.GC.Collect();
      System.GC.WaitForPendingFinalizers();
    }
    /// <summary>
    /// 释放对象,内部调用
    /// </summary>
    /// <param name="o"></param>
    private void ReleaseObj( object o )
    {
      try
      {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
      }
      catch { }
      finally { o = null; }
    }

    private Excel.Application m_objExcel = null;
    private Excel.Workbooks m_objBooks = null;
    private Excel._Workbook m_objBook = null;
    private Excel.Sheets m_objSheets = null;
    private Excel._Worksheet m_objSheet = null;
    private Excel.Range m_objRange = null;
    private object m_objOpt = System.Reflection.Missing.Value;
  }
}  


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -