當我接到領導安排“用word導出文檔”的那一刻,我就想到有沒有輕量級的開發組件,不用用戶安裝配套的office也可以使用,之前一直用Microsoft.Office.Interop.Word,雖然能達到效果,但是在使用中,如果用戶安裝多個office版本或安裝了wps,有時就會報“無法將類型爲 excel.applicationclass 的 com 強制轉換爲接口類型”,很煩人如果沒有裝office,就更麻煩了。
幸運的是,我發現了aspose word組件。
第一步,引用dll組件
第二步,建立模板
(1)通過書籤,在模板中,插入|書籤|輸入書籤的Name,點擊“添加”,“確定”。
(2)通過域(懶得寫了,摘錄一圖片):
顯示書籤:office按鈕|Word選項|高級|在“顯示文檔內容”欄選中顯示書籤。
刪除書籤:光標放在書籤上,點擊工具“書籤”,點擊刪除。
編輯域:選中域名,右擊“編輯域”,彈出上圖,直接輸入名稱,點確定。
插入已知DataTable:插入|表|選擇兩行N列:
第一行輸入列的名稱;
第二行第1列加入兩個域:先添加關鍵字《TableStart:xx》xx:表名;再添加第1列的集合中對應的列名,如《ID》
第二行最後1列加入兩個域:先添加集合中對應的列名,如《BZ》,再添加關鍵字《TableEnd:xx》。
插入圖片
(1)通過書籤,程序中利用DocumentBuilder的InsertNode事件,適用於多個圖片或圖片個數不確定的情況,使用時稍微有點複雜。
(2)通過域《Image:xx》xx:圖片的name.程序中,指定name,利用DocumentBuilder的InsertImage事件,簡單,只要指定url即可。
插入未知的DataTable
有可能table的列要用戶自己指定,那麼利用書籤來完成。
第三步,程序開發
1、讀取模板
String tmppath =Server.MapPath("~/UploadFiles/" + _id+"/template.dot");
Document doc = new Document(tmppath);
2、給書籤賦值
if (doc.Range.Bookmarks["headertitle"]!=null)
{
Bookmarkmark = doc.Range.Bookmarks["headertitle"];
mark.Text = “Aspose Word組件開發”;//標題
}
3、給域賦值
DataTabledt = new DataTable();
dt.TableName=”xx”;
doc.MailMerge.ExecuteWithRegions(dt);//dt對應關鍵字中的xx
4、利用域讀取圖片
string picUrl = Server.MapPath("~/UploadFiles/"+ url);
double width = 100, height =100;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.PageSetup.ClearFormatting();
builder.MoveToMergeField(fieldName);
Shape shape = builder.InsertImage(url); //插入圖片:自動控制大小,並不遮擋後面的內容
shape.Width = width;
shape.Height =height;
shape.HorizontalAlignment= HorizontalAlignment.Center;
//Shapeshape = new Shape(doc, ShapeType.Image);
//shape.ImageData.SetImage(url);
//builder.InsertNode(shape); //用書籤和用這個InsertNode都能實現插入圖片,但是在word中,不會預留圖片位置,而是將後面的內容覆蓋掉了,下面介紹怎麼才能不壓後面的內容。
5、利用書籤讀取圖片
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("tablexst");//開始添加tableImage的地方
//builder.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
builder.StartTable();
//清除設置
builder.PageSetup.ClearFormatting();
builder.RowFormat.HeadingFormat= true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//生成Table
for (int i = 0; i< rowCount; i++)
{
for (int j = 0; j < colCount;j++)
{
AsposeCreateCell(builder, width, height);
}
builder.EndRow();
}
builder.EndTable();
//往建好的table中插入指定的圖片,這樣,就能預留出相應的位置。
shape.ImageData.SetImage(url);
shape.Width = width- 2;
shape.Height = height - 2;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.VerticalAlignment= VerticalAlignment.Bottom;
//指定
builder.MoveToCell(7,r, c, 0);
builder.InsertNode(shape);
//最後一步
doc.Range.Bookmarks.Remove("tablexst"); //清掉標示
doc.MailMerge.DeleteFields();//刪除所有未使用的空白
6.顯示
var docStream = newSystem.IO.MemoryStream();
doc.Save(docStream, SaveFormat.Doc);
stringfamc = CommonHelper.GetString(dt.Rows[0]["FAMC"]);
returnbase.File(docStream.ToArray(),"application/msword", famc +".doc");
MVC中必須用base.File方法,且返回的是一個ActionResult,即JSON,所以當有錯誤返回時,也換成相應的JSON信息:
Try
{
//程序主體
}
catch (Exceptionex)
{
returnJson(ex.Message,JsonRequestBehavior.AllowGet);
}
前臺:<iframe scrolling="auto" frameborder="0" src="' + url +'" width=100% height= 100%;"></iframe>
第四步,技術總結
1.Aspose Word在生成DataTable時,如果涉及到行合併或列合併時,請仔細斟酌,悟出他的原理。
2.直接在word中輸入文字,DocumentBuilder.write或writeIn.
在這裏也非常感謝博客園的BirchLee