2014-1-3 17:32:00Aspose

来源:互联网 发布:明天教室网络课 编辑:程序博客网 时间:2024/05/17 04:07
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyData.Models;
using Aspose.Words;
using System.IO;
using Aspose.Words.Saving;
using System.Data;
using Aspose.Words.Tables;
using Aspose.Words.Reporting;
using Aspose.Words.Drawing;


namespace MvcApplication1.Controllers
{
   //Aspose.Word提供了一个类似Handler的功能,IFieldMergingCallback允许我们动态的处理MergeField
    //void IFieldMergingCallback.FieldMerging(FieldMergingArgs e){} //处理文本
    //void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args){} //处理图片这里我们处理图片写了一个自定义的类实现
    /*class HandleMergeFieldInsertDocument : IFieldMergingCallback
    {
        //文本处理在这里,如果写在这一块,则不起作用
        void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
        {
            
        }
        //图片处理在这里
        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        {
            if (args.DocumentFieldName.Equals("Logo"))
            {
                // 使用DocumentBuilder处理图片的大小
                DocumentBuilder builder = new DocumentBuilder(args.Document);
                builder.MoveToMergeField(args.FieldName);
                
                Shape shape = builder.InsertImage(args.FieldValue.ToString());
 
                // 设置x,y坐标和高宽.
                shape.Left = 0;
                shape.Top = 0;
                shape.Width = 60;
                shape.Height = 80;
            }
        }
    }*/

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }


        public ActionResult About()
        {
            return View();
        }
        public ActionResult Sample() {
            ViewBag.Message = "lalla Hello world";
            return View("Sample");
        }
        public ActionResult MyList() { 
            var albums = new List<Album>();
            for (int i = 0; i < 10; i++) {
                albums.Add(new Album { Title="Product "+i});
                albums.Add(new Album { Context="Context"+i*i});
            }
            ViewBag.Albums = albums;
            Console.Write(albums[0].Title);
            return View("Album");
        }
        public ActionResult GetWord() {
            string wordName = @"D:\myword.doc";
            string outputWord = @"D:\myoutputword.doc";
            Document doc = new Document(wordName);
            
            //自定义数据源
            String[] fieldNames = new String[] { "UserName", "UserPassword", "UserSex" };
            Object[] fieldValues = new Object[] {"张三","123456","男" };


            //合并模版,相当于页面的渲染
            doc.MailMerge.Execute(fieldNames, fieldValues);


            //保存合并后的文档
            doc.Save(outputWord);
            //在MVC中采用,保存文档到流中,使用base.File输出该文件
            var docStream = new MemoryStream();
            doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
            return base.File(docStream.ToArray(), "application/msword", "Template.doc");
        }


        public ActionResult GetStudent() {
            List<Student> mylist = new List<Student>();
            for (int i = 0; i < 10; i++) {
                Student s = new Student();
                s.Name = "student" + i;
                s.No = i.ToString();
                s.Sex = "nan";
                s.Class = "jiek";
                mylist.Add(s);
            }
            String name="";
            foreach(Student li in mylist){
                //Console.Write(li.Name);
                name += li.Name;
            }
            string wordName = @"D:\studentword.doc";
            string outputWord = @"D:\mystudentword.doc";
            
            
            Document doc = new Document(wordName);
            DocumentBuilder builder = new DocumentBuilder(doc);


            //创建数据表,将对象列表转换为数据表
            DataTable dt =new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Class");
            dt.Columns.Add("No");
            dt.Columns.Add("sex");


            foreach(Student sd in mylist){
                DataRow dr = dt.NewRow();//DataRow不能直接new应该调用DataTable的NewRow()方法创建新行。
                dr["Name"] = sd.Name;
                dr["Class"] = sd.Class;
                dr["No"] = sd.No;
                dr["sex"] = sd.Sex;
                dt.Rows.Add(dr);
            }
            List<double> widthlist = new List<double>();
            for (var i = 0; i < dt.Columns.Count; i++) {
                builder.MoveToCell(0,0,i,0);//移动单元格
                double width = builder.CellFormat.Width;//获取单元格宽度
                widthlist.Add(width);
            }
            builder.MoveToBookmark("table");    //开始添加值
            for (var i = 0; i < dt.Rows.Count; i++) {
                for (var j = 0; j < dt.Columns.Count; j++) {
                    builder.InsertCell();//添加一个单元格
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.Width = widthlist[j];
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐
                    builder.Write(dt.Rows[i][j].ToString());
                }
                builder.EndRow();//一行结束,换下一行
            }
            //  doc.Range.Bookmarks["table"].Text = "";    // 清掉标示 //error---Cannot insert a node of this type at this location.


            doc.Save(outputWord);
            //在MVC中采用,保存文档到流中,使用base.File输出该文件
            var docStream = new MemoryStream();
            doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
            return base.File(docStream.ToArray(), "application/msword", "Student.doc");
        }
        public ActionResult GetImage() {
            string imageWord = @"D:\imageword.doc";
            string outputimagePath = @"D:\outputimageword.doc";
            string imagePath = @"D:\Penguin.jpg"; //jpg别总是打成JSP
            
            Document doc = new Document(imageWord);
            DocumentBuilder builder = new DocumentBuilder(doc);
            Shape shape = new Shape(doc,ShapeType.Image);
            shape.ImageData.SetImage(imagePath);//shape.ImageData.SetImage(…)加载图片的方法一共有四个重载,参数分别为:图片路径、字节、流、Image对象。
            shape.Width = 200;
            shape.Height = 100;
            shape.HorizontalAlignment = HorizontalAlignment.Right;//右对齐


            builder.MoveToBookmark("image");
            builder.InsertNode(shape);
            //Bookmark myname = doc.Range.Bookmarks["image"];
            //myname.Text = "";
            //doc.Range.Bookmarks["image"].Remove();
           
            //在MVC中采用,保存文档到流中,使用base.File输出该文件

            var docStream = new MemoryStream();
            doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
            return base.File(docStream.ToArray(), "application/msword", "image.doc"); ;
        }
    }
}

0 0
原创粉丝点击