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"); ;
}
}
}
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
- 2014-1-3 17:32:00Aspose
- 2014-1-7 17:21:19Aspose
- 2014-1-6 16:51:22Aspose
- Aspose.Cells 8.6.3
- Aspose
- aspose
- aspose技巧
- Aspose使用方法
- Aspose.Cells
- Aspose使用方法
- Aspose.cell
- Aspose使用
- itextsharp、Aspose.Words、Aspose.Cells联合使用
- Aspose系列(一) Aspose Words-->DocumentBuilder
- 3D 文件控件Aspose.3D 9月新版V17.9发布 | 附下载
- 3D控件Aspose.3D 12月新版V17.12发布 | 添加支持导出RVM
- 2017年3月Aspose公司产品发布以及相关新闻报道
- Aspose.Words V17.4.0发布,3月更新重点锦集 | 附下载
- 关于天朝的文化和民族的劣根性
- Windows消息机制概述
- 12个有趣的C语言问答
- nagios添加多语言实现
- 百度数据挖掘工程师实习生笔试面试题
- 2014-1-3 17:32:00Aspose
- 反射机制
- 多线程总结
- SQL中on条件与where条件的区别
- 关于Android安全的论坛以及安卓加密壳工具
- qt 使用QtMultimedia模块 录音播放
- 这两天辛苦几位同事把在我微博中发表
- MySQL启用二进制日志
- JavaBean