最近、MVCを使用してWordのレポート機能をエクスポートしたいと考えています。情報を確認したところ、Aspose.Word という便利なプラグインがあることがわかりました。このプラグインも非常に有名で使いやすいです。
1. 1つ目はプラグインを引用することです
2. バックグラウンド操作
その中にはビルダーが注意すべき点がいくつかあります。CellFormat.VerticalMerge = CellMerge.None;CellMerge は列挙型であり、複雑なテーブルの描画やセルの結合によく使用されます。 「First」と「Previous」もあります。最初に DataTable データを取得し、最後にデータを操作する必要があります。
4. 出力ドキュメント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; }
ASP.NET MVC の Word レポートのエクスポートに関連するその他の記事については、PHP 中国語 Web サイトに注目してください。