小編給大家分享一下MVC如何用Aspose.Word導(dǎo)出Word報表,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供廣河網(wǎng)站建設(shè)、廣河做網(wǎng)站、廣河網(wǎng)站設(shè)計、廣河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、廣河企業(yè)網(wǎng)站模板建站服務(wù),10多年廣河做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
1.首先就是引用該插件
2.填充Word模版
3.后臺操作
private List<double> QuaterAirPM10AvgVolReport(string stns, DateTime start, DateTime end, Aspose.Words.DocumentBuilder builder, out DataTable dt, out List<double> widthList,string isMax) { dt = QuaterPM10AvgVol (stns, start, end,isMax); widthList = new List<double>(); double[] colWidth = new double[] { 50, 118, 117, 50, 118, 117 }; string[] colName = new string[] { "排序", "城市", start.Year + "年" + start.Month + "~"+end.Month+"月濃度(μg/m3)", "排序", "城市", "較" + start.AddYears(-1).Year + "年同期增幅" }; builder.MoveToBookmark("table3"); Aspose.Words.Tables.Table table = builder.StartTable();//開始畫Table builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.CellFormat.VerticalMerge = CellMerge.First; builder.CellFormat.Width = 285; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.Write("按平均濃度排序"); builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.Width = 285; builder.Write("按" + start.AddYears(-1).Year + "年同期增幅排序"); builder.EndRow(); AsposeCreateCell(builder, colWidth[0], colName[0]); AsposeCreateCell(builder, colWidth[1], colName[1]); AsposeCreateCell(builder, colWidth[2], colName[2]); AsposeCreateCell(builder, colWidth[3], colName[3]); AsposeCreateCell(builder, colWidth[4], colName[4]); AsposeCreateCell(builder, colWidth[5], colName[5]); builder.EndRow(); //開始添加值 for (var i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["CityName"] == "12個考核地市" || dt.Rows[i]["CityName"] == "全省") { builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.CellFormat.VerticalMerge = CellMerge.First; builder.CellFormat.Width = 168; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Write(dt.Rows[i]["CityName"].ToString()); builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.Width = 117; builder.Write(dt.Rows[i]["PM10ATI"].ToString()); builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.Width = 168; builder.Write(dt.Rows[i]["qnCityName"].ToString()); builder.InsertCell(); builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.Width = 117; builder.Write(dt.Rows[i]["tqbh"].ToString() + "%"); } else { AsposeCreateCell(builder, colWidth[0], dt.Rows[i]["Sort"].ToString()); AsposeCreateCell(builder, colWidth[1], dt.Rows[i]["CityName"].ToString()); AsposeCreateCell(builder, colWidth[2], dt.Rows[i]["PM10ATI"].ToString()); AsposeCreateCell(builder, colWidth[3], dt.Rows[i]["qnSort"].ToString()); AsposeCreateCell(builder, colWidth[4], dt.Rows[i]["qnCityName"].ToString()); AsposeCreateCell(builder, colWidth[5], dt.Rows[i]["tqbh"].ToString() + "%"); } builder.EndRow(); } builder.EndTable(); return widthList; }
其中有幾個注意的地方 builder.CellFormat.VerticalMerge = CellMerge.None;CellMerge是枚舉類型,經(jīng)常用到畫復(fù)雜的表格,或者是合并單元格。還有First和Previous。先要得到DataTable數(shù)據(jù),最后對數(shù)據(jù)進行操作就行了。
4.輸出文檔
public JsonResult QuaterResponse() { bool result; string quarter = Request["quarter"].ToString(); string stns = Request["stns"].ToString(); string isMax = Request["ismax"].ToString(); DateTime startTime = Convert.ToDateTime(Request["startdate"]); DateTime endTime = Convert.ToDateTime(Request["enddate"]); string tmppath = Server.MapPath("~/Document/Model/QuaterReport.docx"); string path = Server.MapPath("~/Document/Export/QuaterReport.doc"); Aspose.Words.Document doc = new Document(tmppath); Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc); doc.Range.Bookmarks["title"].Text = startTime.Year+"年"+quarter+"湖北省環(huán)境空氣質(zhì)量監(jiān)測情況綜述"; doc.Range.Bookmarks["title1"].Text = "表1 "+quarter+"空氣質(zhì)量等級"; doc.Range.Bookmarks["title2"].Text = "表2" +quarter+"優(yōu)良天數(shù)達標率情況表"; doc.Range.Bookmarks["title3"].Text = "表3 "+quarter+"空氣可吸入顆粒物(PM10)平均濃度情況表"; doc.Range.Bookmarks["title4"].Text = "表4 "+quarter+"空氣可吸入顆粒物(PM2.5)平均濃度情況表"; doc.Range.Bookmarks["title5"].Text = "表5"+quarter+" 境空氣氣態(tài)污染物平均濃度情況表"; doc.Range.Bookmarks["title6"].Text = "表6 "+quarter+"環(huán)境空氣質(zhì)量綜合指數(shù)情況表"; DataTable dt; List<double> widthList; try { doc.Range.Bookmarks["table1"].Text = ""; // 清掉標示 QuaterAirPerencetReport( stns, startTime, endTime, builder, out dt, out widthList,isMax); doc.Range.Bookmarks["table2"].Text = ""; QuaterAirYldblReport(stns, startTime, endTime, builder,quarter, out dt, out widthList,isMax); doc.Range.Bookmarks["table3"].Text = ""; QuaterAirPM10AvgVolReport(stns, startTime, endTime, builder, out dt, out widthList,isMax); doc.Range.Bookmarks["table4"].Text = ""; QuaterAirPM25AvgVolReport(stns, startTime, endTime, builder, out dt, out widthList,isMax); doc.Range.Bookmarks["table5"].Text = ""; QuaterOtherAvgVolReport(stns, startTime, endTime, builder, out dt, out widthList,isMax); doc.Range.Bookmarks["table6"].Text = ""; QuaterZHIndexReport(stns, startTime, endTime, builder, out dt, out widthList,isMax); doc.Save(path, Aspose.Words.SaveFormat.Doc); // System.Diagnostics.Process.Start(path);//打開文檔 // return View("QuaterReport"); result = true; } catch (Exception) { result = false; } return Json(result); }
以上是“MVC如何用Aspose.Word導(dǎo)出Word報表”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁標題:MVC如何用Aspose.Word導(dǎo)出Word報表
分享網(wǎng)址:http://www.rwnh.cn/article42/igpdec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、標簽優(yōu)化、建站公司、品牌網(wǎng)站設(shè)計、企業(yè)建站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)